减去时间值在大熊猫
问题描述:
次专栏中,我有次列像这样减去时间值在大熊猫
df = pd.DataFrame({'times':['10:59:20.1647', '11:05:46.2258', '11:10:59.4658']})
我的目标是减去所有这一切的时候了第一次。为了做到这一点,我转换列datetime.time
类型和减去第一个值,所有列:
pd.to_datetime(df['times']).dt.time - pd.to_datetime(df['times']).dt.time.iloc[0]
然而,这样做我得到一个错误:
TypeError: unsupported operand type(s) for -: 'datetime.time' and'datetime.time'
你可以建议一个聪明和优雅的方式,以实现我的目标?
答
使用timedeltas
:
a = pd.to_timedelta(df['times'])
b = a - a.iat[0]
print (b)
0 00:00:00
1 00:06:26.061100
2 00:11:39.301100
Name: times, dtype: timedelta64[ns]
而且如果需要的时候:
c = pd.to_datetime(b).dt.time
print (c)
0 00:00:00
1 00:06:26.061100
2 00:11:39.301100
Name: times, dtype: object
print (c.apply(type))
0 <class 'datetime.time'>
1 <class 'datetime.time'>
2 <class 'datetime.time'>
Name: times, dtype: object
与输出timedelta
另一种解决方案:
a = pd.to_datetime(df['times'])
b = a - a.iat[0]
print (b)
0 00:00:00
1 00:06:26.061100
2 00:11:39.301100
Name: times, dtype: timedelta64[ns]
最后一件事:可以说,我想绕一圈基于小数的数字我应该怎么做?在此先感谢:) –
那么'00:06:26.061100'的输出是什么? – jezrael
00:06:26.061100将是00:06:26/ 00:06:28.510000将是00:06:28 –