I’m trying to teach myself some Python by adapting an Excel csv file plus a ps1 file combo that services a chart. That csv file has 3 blocks of data:
Block 1 labeled “intro” has two lines for physical location data.
Block 2 labeled “temperature” has 337 lines with headers “time” and “temperature_2m (°F)”. Time data includes time of day, e.g, “2026-04-23T00:00” and temperature column is the temp at that hour. The entire block covers 14 days of hourly time and temperature.
Block 3 labeled “sunset” has 15 lines, with headers “time” and “sunset (iso8601)”. The time line contains a day, e.g., “2026-04-23”. The “sunset (iso8601)” column contains the time of sunset of that day.
The Excel version looks up the temperature in Block 2 for each day in Block 3.
My code, so far, is:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import requests
data_url = "https://api.open-meteo.com/v1/forecast?latitude=39.49&longitude=-119.9&daily=sunset&hourly=temperature_2m&timezone=America%2FLos_Angeles&past_days=7&forecast_days=7&temperature_unit=fahrenheit&format=csv"
response = requests.get(data_url)
temp_download = "G:/workspace/apples/pythons/downloaded_data.csv"
with open(temp_download, "wb") as f:
f.write(response.content)
df = pd.read_csv(temp_download)
df = df.replace(to_replace="sunset (iso8601)", value = "sunset")
intro = pd.read_csv(temp_download, skiprows=0, nrows=2)
# temp: time, temperature_2m (°F)
temperature = pd.read_csv(temp_download, skiprows=3, nrows=337)
# sunset: time, sunset (iso8601)
sunset = pd.read_csv(temp_download, skiprows=341, nrows=15)
result = pd.merge_asof(sunset, temperature, left_on=pd.to_datetime("sunset"), right_on=pd.to_datetime("time"))
# result = pd.merge_asof(temperature, sunset, on='time', direction='backward')
print(result)
This version (pd.merge_asof(sunset, temperature, left_on…) results in the error:
Unknown datetime string format, unable to parse: sunset
The other version (pd.merge_asof(temperature, sunset, on='time'…) results in the error:
…both sides must have numeric dtype
How do I avoid either of those errors?