未找到 Pandas Series.dt.total_seconds()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45884288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas Series.dt.total_seconds() not found
提问by RockJake28
I need a datetime column in seconds, everywhere (including the docs) is saying that I should use Series.dt.total_seconds()
but it can't find the function. I'm assuming I have the wrong version of something but I don't...
我需要一个以秒为单位的日期时间列,到处(包括文档)都在说我应该使用Series.dt.total_seconds()
但它找不到该功能。我假设我有一些错误的版本,但我不......
pip freeze | grep pandas
pandas==0.20.3
python --version
Python 3.5.3
This is all within a virtualenv that has worked without fault for a long time, and the other Series.dt
functions work. Here's the code:
这一切都在一个长期无故障运行的 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'
I've also tested this on a second machine and get the same result. Any ideas what I'm missing?
我也在第二台机器上对此进行了测试并得到了相同的结果。任何想法我错过了什么?
回答by EdChum
total_seconds
is a member of timedelta
not datetime
total_seconds
是timedelta
不是的成员datetime
Hence the error
因此错误
You maybe be wanting dt.second
你可能想要 dt.second
This returns the second component which is different to total_seconds
这将返回与 total_seconds
So you need to perform some kind of arithmetic operation such as deleting something against this in order to generate a series of timedeltas, then you can do dt.total_seconds
因此,您需要执行某种算术运算,例如针对此删除某些内容以生成一系列 timedeltas,然后您可以执行 dt.total_seconds
Example:
例子:
In[278]:
s = s - pd.datetime.now()
s
Out[278]:
0 -1 days +23:59:46.389639
1 -1 days +23:59:46.389639
2 -1 days +23:59:46.389639
3 -1 days +23:59:46.389639
4 -1 days +23:59:46.389639
5 -1 days +23:59:46.389639
6 -1 days +23:59:46.389639
7 -1 days +23:59:46.389639
8 -1 days +23:59:46.389639
9 -1 days +23:59:46.389639
dtype: timedelta64[ns]
In[279]:
s.dt.total_seconds()
Out[279]:
0 -13.610361
1 -13.610361
2 -13.610361
3 -13.610361
4 -13.610361
5 -13.610361
6 -13.610361
7 -13.610361
8 -13.610361
9 -13.610361
dtype: float64
回答by mortysporty
Alternatively, if you really want to have seconds (since 1970 epoch), you can try this
或者,如果你真的想要秒(自 1970 纪元以来),你可以试试这个
import pandas as pd
from datetime import datetime
import time
df = pd.DataFrame({'datetime' : [datetime(2012, 11, 19, 12, 40, 10),
datetime(2012, 11, 19, 12, 35, 10),
datetime(2012, 11, 19, 12, 30, 10),
datetime(2012, 11, 19, 12, 25, 10)
]})
df['seconds'] = [time.mktime(t.timetuple()) for t in df.datetime]
df['back_to_date_time'] = [datetime.utcfromtimestamp(t) for t in df.seconds]
>>>>df
Out[2]:
datetime seconds back_to_date_time
0 2012-11-19 12:40:10 1.353325e+09 2012-11-19 11:40:10
1 2012-11-19 12:35:10 1.353325e+09 2012-11-19 11:35:10
2 2012-11-19 12:30:10 1.353325e+09 2012-11-19 11:30:10
3 2012-11-19 12:25:10 1.353324e+09 2012-11-19 11:25:10
or you can look here How can I convert a datetime object to milliseconds since epoch (unix time) in Python?
或者您可以在这里查看 如何在 Python 中将日期时间对象转换为自纪元(unix 时间)以来的毫秒数?