I have a duckdb database named weather-4.duckdb
There is a table named weather and fields called station, report date, hour, and temperature_F.
Can you plot temperature over time for a given station?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Plotting Temperature Over Time for a Station in DuckDB
I can help you create a plot of temperature over time for a specific station from your weather database. Let me walk you through the process:
Step 1: Connect to the Database and Explore
First, we'll need to connect to your DuckDB database and check the available stations:
python
import duckdb
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Connect to the database
conn = duckdb.connect('weather-4.duckdb')
# Get a list of available stations
stations = conn.execute("SELECT DISTINCT station FROM weather ORDER BY station").fetchall()
stations_list = [station[0] for station in stations]
print(f"Available stations: {stations_list}")
Step 2: Create a Function to Plot Temperature for a Station
python
def plot_temperature_for_station(station_id):
# Query to get temperature data for the specified station
query = f"""
SELECT
report_date,
hour,
temperature_F
FROM
weather
WHERE
station = '{station_id}'
ORDER BY
report_date, hour
"""
# Execute the query and convert to pandas DataFrame
df = conn.execute(query).fetchdf()
# Create a datetime column by combining date and hour
df['datetime'] = pd.to_datetime(df['report_date']) + pd.to_timedelta(df['hour'], unit='h')
# Create the plot
plt.figure(figsize=(12, 6))
sns.lineplot(x='datetime', y='temperature_F', data=df)
plt.title(f'Temperature Over Time for Station: {station_id}')
plt.xlabel('Date and Time')
plt.ylabel('Temperature (°F)')
plt.grid(True)
plt.tight_layout()
plt.show()
return df
Step 3: Use the Function to Plot a Station's Temperature
To use this function, you would select a station from the available stations list:
python
# Example: Plot temperature for the first station in the list
if stations_list:
selected_station = stations_list[0]
print(f"Plotting temperature for station: {selected_station}")
temperature_data = plot_temperature_for_station(selected_station)
else:
print("No stations found in the database.")
Step 4: Close the Connection
python
# Close the database connection when done
conn.close()
You can modify the selected_station variable to plot data for any station in your database. The plot will show temperature variations over time, with the x-axis representing the date and time, and the y-axis showing the temperature in Fahrenheit.