我需要一个以秒为单位的日期时间列,到处( 包括文档)都说我应该使用 Series.dt.total_seconds()
但它找不到函数。我假设我有错误的版本,但我没有……
pip freeze | grep pandas
pandas==0.20.3
python --version
Python 3.5.3
这一切都在一个 virtualenv 中,它已经运行了很长时间,并且其他 Series.dt
功能正常工作。这是代码:
from pandas import Series
from datetime import datetime
s = Series([datetime.now() for _ in range(10)])
0 2017-08-25 15:55:25.079495
1 2017-08-25 15:55:25.079504
2 2017-08-25 15:55:25.079506
3 2017-08-25 15:55:25.079508
4 2017-08-25 15:55:25.079509
5 2017-08-25 15:55:25.079510
6 2017-08-25 15:55:25.079512
7 2017-08-25 15:55:25.079513
8 2017-08-25 15:55:25.079514
9 2017-08-25 15:55:25.079516
dtype: datetime64[ns]
s.dt
<pandas.core.indexes.accessors.DatetimeProperties object at 0x7f5a686507b8>
s.dt.minute
0 55
1 55
2 55
3 55
4 55
5 55
6 55
7 55
8 55
9 55
dtype: int64
s.dt.total_seconds()
AttributeError: 'DatetimeProperties' object has no attribute 'total_seconds'
我也在第二台机器上测试过这个并得到相同的结果。有什么想法我想念的吗?
原文由 JakeCowton 发布,翻译遵循 CC BY-SA 4.0 许可协议
total_seconds
是—的成员timedelta
不是datetime
因此错误
你可能想要
dt.second
这将返回不同于
total_seconds
的第二个组件所以你需要执行某种算术运算,比如删除一些东西来生成一系列时间增量,然后你可以做
dt.total_seconds
例子: