pandas 计算 DateTimeIndex 的时差
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36583859/
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
compute time difference of DateTimeIndex
提问by Mark Bakker
I want to compute the time difference between times in a DateTimeIndex
我想计算 DateTimeIndex 中时间之间的时差
import pandas as pd
p = pd.DatetimeIndex(['1985-11-14', '1985-11-28', '1985-12-14', '1985-12-28'], dtype='datetime64[ns]')
I can compute the time difference of two times:
我可以计算出两次的时差:
p[1] - p[0]
gives
给
Timedelta('14 days 00:00:00')
But p[1:] - p[:-1] doesn't work and gives
但是 p[1:] - p[:-1] 不起作用并给出
DatetimeIndex(['1985-12-28'], dtype='datetime64[ns]', freq=None)
and a future warning:
以及未来的警告:
FutureWarning: using '-' to provide set differences with datetimelike Indexes is deprecated, use .difference()
Any thought on how how I can (easily) compute the time difference between values in a DateTimeIndex? And why does it work for 1 value, but not for the entire DateTimeIndex?
关于如何(轻松)计算 DateTimeIndex 中值之间的时间差的任何想法?为什么它适用于 1 个值,而不适用于整个 DateTimeIndex?
回答by EdChum
Convert the DatetimeIndex
to a Series
using to_series()
and then call diff
to calculate inter-row differences:
将 转换DatetimeIndex
为Series
usingto_series()
然后调用diff
以计算行间差异:
In [5]:
p.to_series().diff()
Out[5]:
1985-11-14 NaT
1985-11-28 14 days
1985-12-14 16 days
1985-12-28 14 days
dtype: timedelta64[ns]
As to why it failed, the -
operator here is attempting to perform a set difference or intersection of your different index ranges, you're trying to subtract the values from one range with another which diff
does.
至于它失败的原因,-
这里的操作员试图执行不同索引范围的集合差异或交集,您试图将一个范围中的值与另一个范围相减diff
。
when you did p[1]
- p[0]
the -
is performing a scalar subtraction but when you do this on an index it thinks that you're perform a set operation
当你做p[1]
-p[0]
将-
被进行标量减法但是当你做这个指数它认为你是执行一组操作
回答by johnchase
The -
operator is working, it's just not doing what you expect. In the second situation it is acting to give the difference of the two datetime indices, that is the value that is in p[1:]
but not in p[:-1]
该-
运营商工作,它只是没有做你的期望。在第二种情况下,它的作用是给出两个日期时间索引的差异,即在p[1:]
但不在的值p[:-1]
There may be a better solution, but it would work to perform the operation element wise:
可能有更好的解决方案,但明智地执行操作元素会起作用:
[e - k for e,k in zip(p[1:], p[:-1])]
回答by Alexander
I used None
to fill the first difference value, but I'm sure you can figure out how you would like to deal with that case.
我曾经None
填充第一个差值,但我相信您可以弄清楚您希望如何处理这种情况。
>>> [None] + [p[n] - p[n-1] for n in range(1, len(p))]
[None,
Timedelta('14 days 00:00:00'),
Timedelta('16 days 00:00:00'),
Timedelta('14 days 00:00:00')]
BTW, to just get the day difference:
顺便说一句,为了获得日差:
[None] + [(p[n] - p[n-1]).days for n in range(1, len(p))]
[None, 14, 16, 14]