WebSocket获取的Timestamp转Pandas datetime对象及重采样问题求助
Hey there! I totally get where you're coming from—getting timestamps from WebSocket data into a format Pandas can work with can feel tricky at first, but let's break this down step by step.
First, let's clarify: your timestamp format (2018-02-04T07:49:36.867Z) is ISO 8601 with UTC timezone (the Z stands for Zulu time, which is UTC). Pandas has built-in tools to handle this perfectly—we just need to make sure we convert the column properly and set it as the index (since resampling requires a datetime index).
Step 1: Load your CSV and convert the timestamp column
Assuming you've already loaded your CSV into a DataFrame like this:
import pandas as pd df = pd.read_csv('your_websocket_data.csv')
Now, convert the timestamp column to a Pandas datetime object. We'll use pd.to_datetime() and specify utc=True to preserve the UTC timezone:
df['timestamp'] = pd.to_datetime(df['timestamp'], utc=True)
If you notice any parsing errors (unlikely with this format), you can explicitly define the format to be safe:
df['timestamp'] = pd.to_datetime(df['timestamp'], format='%Y-%m-%dT%H:%M:%S.%fZ', utc=True)
Step 2: Set the timestamp as the DataFrame index
Resampling in Pandas relies on having a datetime index, so let's set that up:
df = df.set_index('timestamp')
Alternatively, you can do this in one line when loading the CSV—super clean:
df = pd.read_csv('your_websocket_data.csv', parse_dates=['timestamp'], index_col='timestamp', utc=True)
This parses the timestamps and sets the index all at once!
Step 3: Perform resampling
Now you're ready to resample your data to 1-minute, 3-minute, or any interval you need. Here are some common examples:
- Resample to 1-minute intervals and calculate the mean of other columns:
df_1min = df.resample('1T').mean()
- Resample to 3-minute intervals and take the sum:
df_3min = df.resample('3T').sum()
- If you want to keep the first value in each interval:
df_3min_first = df.resample('3T').first()
Common frequency codes to use: 'T' for minutes, 'H' for hours, 'D' for days.
Troubleshooting common issues
- If you get an error like
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, it means your index isn't a datetime index—double-check Step 2 to make sure you set it correctly. - If timestamps are stored as list-like strings (e.g.,
["2018-02-04T07:49:36.867Z", ...]in each cell), you'll need to extract individual timestamps first:
df['timestamp'] = df['timestamp'].str.strip('[]').str.split(',').explode() df['timestamp'] = df['timestamp'].str.strip()
Then proceed with the datetime conversion as before.
That should get you up and running with resampling your WebSocket data in no time!
内容的提问来源于stack exchange,提问作者Mr. Confused

