按日期计算平均价格:基于TimeAndSale数据集的技术问询
Got it, let's walk through how to compute the average Price per date using your TimeAndSale dataset. I'll assume you're using pandas (the standard tool for this kind of tabular/time-series data work) — here's a straightforward, step-by-step approach:
Step 1: Load and Prepare Your Data
First, make sure your timestamp column (#=TimeAndSale) is properly parsed as a datetime type. If it's stored as plain text right now, convert it:
import pandas as pd # Load your data (adjust the read method to match your source: CSV, Excel, etc.) df = pd.read_csv("your_time_and_sale_data.csv") # Convert the timestamp column to datetime df["#=TimeAndSale"] = pd.to_datetime(df["#=TimeAndSale"])
Step 2: Extract Date from Timestamp
Create a new column that isolates just the date part (we'll use this to group our data):
df["trade_date"] = df["#=TimeAndSale"].dt.date
Step 3: Group by Date and Calculate Average Price
Use pandas' groupby() function to aggregate the Price values by date, then compute the mean:
# Calculate daily average price, reset index to make it a clean DataFrame daily_avg_price = df.groupby("trade_date")["Price"].mean().reset_index() # Optional: Round the average to 2 decimal places for readability daily_avg_price["Price"] = daily_avg_price["Price"].round(2)
Notes to Keep in Mind
- If your dataset has missing
Pricevalues, pandas'mean()will automatically skip them by default. If you want to change this behavior, usemean(skipna=False)(though this will return NaN for dates with any missing prices). - If you need to filter by a specific
EventSymbolfirst (e.g., only calculate averages for a particular stock), add a filter before grouping:filtered_df = df[df["EventSymbol"] == "YOUR_SYMBOL"] daily_avg_price = filtered_df.groupby("trade_date")["Price"].mean().reset_index()
That's it! The daily_avg_price DataFrame will have two columns: trade_date (the date) and Price (the average price for that day).
内容的提问来源于stack exchange,提问作者Jeremie




