专门有Timedelta
对象表示时间差。
print(pd.Timedelta('2 days 2 hours 15 minutes 30 seconds'))
'''
2 days 02:15:30
'''
print(pd.Timedelta(6, unit='h'))
'''
0 days 06:00:00
'''
print(pd.Timedelta(hours=2))
'''
0 days 02:00:00
'''
可以Series/DataFrame上直接操作,通过在datetime64[ns] Series对象或者Timestamp上减法操作来构造timedelta64[ns] Series对象。 以如下代码作为例子:
s = pd.Series(pd.date_range('2012-1-1', periods=3, freq='D'))
td = pd.Series([ pd.Timedelta(days=i) for i in range(3) ])
df = pd.DataFrame(dict(A = s, B = td))
print(df)
'''
A B
0 2012-01-01 0 days
1 2012-01-02 1 days
2 2012-01-03 2 days
'''
print(df['A'] + df['B'])
'''
0 2012-01-01
1 2012-01-03
2 2012-01-05
dtype: datetime64[ns]
'''
print(df['A'] - df['B'])
'''
0 2012-01-01
1 2012-01-01
2 2012-01-01
dtype: datetime64[ns]
'''
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有