我的数据框如下:
price timestamp amount side
0 0.001728 2019-05-01 22:48:49.082 29.0 buy
1 0.001729 2019-05-01 21:26:22.078 31.0 sell
2 0.001730 2019-05-01 18:05:30.953 32.0 buy
3 0.001726 2019-05-01 18:05:40.412 32.0 sell
4 0.001730 2019-05-01 16:59:51.127 32.0 sell
5 0.001741 2019-05-01 12:23:19.957 32.0 buy
我想要的是以下内容:
price timestamp amount side
0 0.001728 2019-05-01 22:48:00 29.0 buy
1 0.001729 2019-05-01 21:26:00 31.0 sell
2 0.001730 2019-05-01 18:05:00 32.0 buy
3 0.001726 2019-05-01 18:05:00 32.0 sell
4 0.001730 2019-05-01 16:59:00 32.0 sell
5 0.001741 2019-05-01 12:23:00 32.0 buy
我尝试过的:
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='%Y%m%d %H:%M')
但是它并没有改变我的数据框...为什么知道呢?谢谢!
答案 0 :(得分:2)
尝试使用dt.floor
进行前一分钟的发言:
pd.to_datetime(df['timestamp'], errors='coerce').dt.floor('min')
0 2019-05-01 22:48:00
1 2019-05-01 21:26:00
2 2019-05-01 18:05:00
3 2019-05-01 18:05:00
4 2019-05-01 16:59:00
5 2019-05-01 12:23:00
Name: timestamp, dtype: datetime64[ns]
df.assign(
timestamp=pd.to_datetime(df['timestamp'], errors='coerce').dt.floor('min'))
price timestamp amount side
0 0.001728 2019-05-01 22:48:00 29.0 buy
1 0.001729 2019-05-01 21:26:00 31.0 sell
2 0.001730 2019-05-01 18:05:00 32.0 buy
3 0.001726 2019-05-01 18:05:00 32.0 sell
4 0.001730 2019-05-01 16:59:00 32.0 sell
5 0.001741 2019-05-01 12:23:00 32.0 buy
答案 1 :(得分:1)
astype
因为@ cs95可以让我更快地给出答案(-:
df.assign(timestamp=df.timestamp.astype('datetime64[m]'))
price timestamp amount side
0 0.001728 2019-05-01 22:48:00 29.0 buy
1 0.001729 2019-05-01 21:26:00 31.0 sell
2 0.001730 2019-05-01 18:05:00 32.0 buy
3 0.001726 2019-05-01 18:05:00 32.0 sell
4 0.001730 2019-05-01 16:59:00 32.0 sell
5 0.001741 2019-05-01 12:23:00 32.0 buy