相关疑难解决方法(0)

在Python Pandas DataFrame中将timedelta64 [ns]列转换为秒

pandas DataFrame列duration包含timedelta64[ns]如图所示.你如何将它们转换为秒?

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]
Run Code Online (Sandbox Code Playgroud)

我尝试了以下内容

print df[:5]['duration'] / np.timedelta64(1, 's')
Run Code Online (Sandbox Code Playgroud)

但得到了错误

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    print df[0:5]['duration'] / np.timedelta64(1, 's')
  File "C:\Python27\lib\site-packages\pandas\core\series.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
Run Code Online (Sandbox Code Playgroud)

也试过了

print df[:5]['duration'].astype('timedelta64[s]') …
Run Code Online (Sandbox Code Playgroud)

python numpy python-2.7 pandas

37
推荐指数
4
解决办法
4万
查看次数

标签 统计

numpy ×1

pandas ×1

python ×1

python-2.7 ×1