如何从 timedelta 中删除微秒
How to remove microseconds from timedelta
我想从 pandas 列中截断几微秒。我尝试了类似 analyze_me['how_long_it_took_to_order'] = analyze_me['how_long_it_took_to_order'].apply(lambda x: x.replace(microsecond=0)
的操作,但出现了此错误 replace() takes no keyword arguments
.
例如:我想让00:19:58.582052变成00:19:58或者00:19:58.58
您的 how_long_it_took_to_order
列似乎是字符串 (object
) dtype。
所以试试这个:
analyze_me['how_long_it_took_to_order'] = \
analyze_me['how_long_it_took_to_order'].str.split('.').str[0]
或:
analyze_me['how_long_it_took_to_order'] = \
analyze_me['how_long_it_took_to_order'].str.replace('(\.\d{2})\d+', r'')
对于 "centiseconds",例如:00:19:58.58
我认为您需要使用 pd.to_timedelta
将字符串转换为 timedelta,然后利用基于字符串截断的 floor 方法的优秀 dt 访问器。这是您数据的前两行。
df['how_long_it_took_to_order'] = pd.to_timedelta(df['how_long_it_took_to_order'])
df['how_long_it_took_to_order'].dt.floor('s')
0 00:19:58
1 00:25:09
可以四舍五入到百分之一秒。
df['how_long_it_took_to_order'].dt.floor('10ms')
0 00:19:58.580000
1 00:25:09.100000
我在这里创建了一些时间增量系列,然后使用 dt
访问器和 floor
方法来截断到最接近的微秒。
d = pd.timedelta_range(0, periods=6, freq='644257us')
s = pd.Series(d)
s
0 00:00:00
1 00:00:00.644257
2 00:00:01.288514
3 00:00:01.932771
4 00:00:02.577028
5 00:00:03.221285
dtype: timedelta64[ns]
现在截断
s.dt.floor('s')
0 00:00:00
1 00:00:00
2 00:00:01
3 00:00:01
4 00:00:02
5 00:00:03
dtype: timedelta64[ns]
如果您想截断到最接近的百分之一秒,请执行以下操作:
s.dt.floor('10ms')
0 00:00:00
1 00:00:00.640000
2 00:00:01.280000
3 00:00:01.930000
4 00:00:02.570000
5 00:00:03.220000
dtype: timedelta64[ns]
我需要一个我没有使用的简单脚本 Pandas,并想出了一个应该可以在任何地方使用的简单 hack。
age = age - timedelta(microseconds=age.microseconds)
其中 age
是我的 timedelta
对象。
你不能直接修改timedelta
对象的microseconds
成员,因为它是不可变的,但当然,你可以用另一个不可变对象替换它。
我想从 pandas 列中截断几微秒。我尝试了类似 analyze_me['how_long_it_took_to_order'] = analyze_me['how_long_it_took_to_order'].apply(lambda x: x.replace(microsecond=0)
的操作,但出现了此错误 replace() takes no keyword arguments
.
例如:我想让00:19:58.582052变成00:19:58或者00:19:58.58
您的 how_long_it_took_to_order
列似乎是字符串 (object
) dtype。
所以试试这个:
analyze_me['how_long_it_took_to_order'] = \
analyze_me['how_long_it_took_to_order'].str.split('.').str[0]
或:
analyze_me['how_long_it_took_to_order'] = \
analyze_me['how_long_it_took_to_order'].str.replace('(\.\d{2})\d+', r'')
对于 "centiseconds",例如:00:19:58.58
我认为您需要使用 pd.to_timedelta
将字符串转换为 timedelta,然后利用基于字符串截断的 floor 方法的优秀 dt 访问器。这是您数据的前两行。
df['how_long_it_took_to_order'] = pd.to_timedelta(df['how_long_it_took_to_order'])
df['how_long_it_took_to_order'].dt.floor('s')
0 00:19:58
1 00:25:09
可以四舍五入到百分之一秒。
df['how_long_it_took_to_order'].dt.floor('10ms')
0 00:19:58.580000
1 00:25:09.100000
我在这里创建了一些时间增量系列,然后使用 dt
访问器和 floor
方法来截断到最接近的微秒。
d = pd.timedelta_range(0, periods=6, freq='644257us')
s = pd.Series(d)
s
0 00:00:00
1 00:00:00.644257
2 00:00:01.288514
3 00:00:01.932771
4 00:00:02.577028
5 00:00:03.221285
dtype: timedelta64[ns]
现在截断
s.dt.floor('s')
0 00:00:00
1 00:00:00
2 00:00:01
3 00:00:01
4 00:00:02
5 00:00:03
dtype: timedelta64[ns]
如果您想截断到最接近的百分之一秒,请执行以下操作:
s.dt.floor('10ms')
0 00:00:00
1 00:00:00.640000
2 00:00:01.280000
3 00:00:01.930000
4 00:00:02.570000
5 00:00:03.220000
dtype: timedelta64[ns]
我需要一个我没有使用的简单脚本 Pandas,并想出了一个应该可以在任何地方使用的简单 hack。
age = age - timedelta(microseconds=age.microseconds)
其中 age
是我的 timedelta
对象。
你不能直接修改timedelta
对象的microseconds
成员,因为它是不可变的,但当然,你可以用另一个不可变对象替换它。