我有一个datetime
对象,如下所示:
df.iloc[:10]
0 2019-03-05 00:45:36.503277422+08:00
1 2019-03-05 00:46:36.404034571+08:00
2 2019-03-05 00:47:36.434888822+08:00
3 2019-03-05 00:48:36.535496247+08:00
4 2019-03-05 00:49:36.512082457+08:00
5 2019-03-05 00:50:36.515718466+08:00
6 2019-03-05 00:51:36.520325894+08:00
7 2019-03-05 00:52:36.523945647+08:00
8 2019-03-05 00:53:36.548567617+08:00
9 2019-03-05 00:54:36.740268213+08:00
Name: Date-Time, dtype: datetime64[ns, Asia/Shanghai]
如果行时间晚于08:00:00 Asia/Shanghai
时间(这意味着晚于00:00:00
UTC时间),我将检索所有行。我有两个问题:
如何以当地时间(上海)而不是UTC时间写条件。
只有df[df>'2019-03-05 00:00:00']
返回True
。如果我使用df[df>'2019-03-05 08:00:00']
,它们都将是False
。
如何仅使用时间,而不必在时间之前加上日期。我只想写df[df>'2019-03-05 00:00:00']
时间,而不是写df[df>'00:00:00']
。
非常感谢!
答案 0 :(得分:0)
您可以将时区信息添加到标量日期时间并进行比较:
date = pd.to_datetime('2015-02-24').tz_localize('UTC').tz_convert('Asia/Shanghai')
print (date)
2015-02-24 08:00:00+08:00
或者:
date = pd.Timestamp('2015-02-24 08:00:00+08:00')
print (df[df > date])
0 2019-03-05 00:45:36.503277422+08:00
1 2019-03-05 00:46:36.404034571+08:00
2 2019-03-05 00:47:36.434888822+08:00
3 2019-03-05 00:48:36.535496247+08:00
4 2019-03-05 00:49:36.512082457+08:00
5 2019-03-05 00:50:36.515718466+08:00
6 2019-03-05 00:51:36.520325894+08:00
7 2019-03-05 00:52:36.523945647+08:00
8 2019-03-05 00:53:36.548567617+08:00
9 2019-03-05 00:54:36.740268213+08:00
Name: Date-Time, dtype: datetime64[ns, Asia/Shanghai]
第二次按时间比较:
from datetime import time
print (df[df.dt.time > time(0,0,0)])
0 2019-03-05 00:45:36.503277422+08:00
1 2019-03-05 00:46:36.404034571+08:00
2 2019-03-05 00:47:36.434888822+08:00
3 2019-03-05 00:48:36.535496247+08:00
4 2019-03-05 00:49:36.512082457+08:00
5 2019-03-05 00:50:36.515718466+08:00
6 2019-03-05 00:51:36.520325894+08:00
7 2019-03-05 00:52:36.523945647+08:00
8 2019-03-05 00:53:36.548567617+08:00
9 2019-03-05 00:54:36.740268213+08:00
Name: Date-Time, dtype: datetime64[ns, Asia/Shanghai]
或按时间增量:
print (df[pd.to_timedelta(df.dt.strftime('%H:%M:%S')) > '00:00:00'])