在 Python Pandas DataFrame 中将 timedelta64[ns] 列转换为秒 [英] Convert timedelta64[ns] column to seconds in Python Pandas DataFrame
问题描述
Pandas DataFrame 列 duration
包含 timedelta64[ns]
,如图所示.如何将它们转换为秒?
A pandas DataFrame column duration
contains timedelta64[ns]
as shown. How can you convert them to seconds?
0 00:20:32
1 00:23:10
2 00:24:55
3 00:13:17
4 00:18:52
Name: duration, dtype: timedelta64[ns]
我尝试了以下
print df[:5]['duration'] / np.timedelta64(1, 's')
但得到了错误
Traceback (most recent call last):
File "test.py", line 16, in <module>
print df[0:5]['duration'] / np.timedelta64(1, 's')
File "C:Python27libsite-packagespandascoreseries.py", line 130, in wrapper
"addition and subtraction, but the operator [%s] was passed" % name)
TypeError: can only operate on a timedeltas for addition and subtraction, but the operator [__div__] was passed
也试过了
print df[:5]['duration'].astype('timedelta64[s]')
但收到错误
Traceback (most recent call last):
File "test.py", line 17, in <module>
print df[:5]['duration'].astype('timedelta64[s]')
File "C:Python27libsite-packagespandascoreseries.py", line 934, in astype
values = com._astype_nansafe(self.values, dtype)
File "C:Python27libsite-packagespandascorecommon.py", line 1653, in _astype_nansafe
raise TypeError("cannot astype a timedelta from [%s] to [%s]" % (arr.dtype,dtype))
TypeError: cannot astype a timedelta from [timedelta64[ns]] to [timedelta64[s]]
推荐答案
这在 Pandas 的当前版本(0.14 版)中正常工作:
This works properly in the current version of Pandas (version 0.14):
In [132]: df[:5]['duration'] / np.timedelta64(1, 's')
Out[132]:
0 1232
1 1390
2 1495
3 797
4 1132
Name: duration, dtype: float64
这是旧版本的 Pandas/NumPy 的解决方法:
Here is a workaround for older versions of Pandas/NumPy:
In [131]: df[:5]['duration'].values.view('<i8')/10**9
Out[131]: array([1232, 1390, 1495, 797, 1132], dtype=int64)
timedelta64 和 datetime64 数据在内部存储为 8 字节整数(dtype'
timedelta64 and datetime64 data are stored internally as 8-byte ints (dtype
'<i8'
). So the above views the timedelta64s as 8-byte ints and then does integer
division to convert nanoseconds to seconds.
请注意,您 需要 NumPy 1.7 或更高版本 才能使用 datetime64/timedelta64s.
Note that you need NumPy version 1.7 or newer to work with datetime64/timedelta64s.
这篇关于在 Python Pandas DataFrame 中将 timedelta64[ns] 列转换为秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!