pandas 不同时区的时间数组的时间戳减法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28254408/
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
Timestamp subtraction of time arrays with different timezones
提问by Luis Miguel
I have the following code from somebody else that has a similar problem, but the solution proposed does not work on my DataFrame. The code subtracts a Pandas DataFrame index from a given date:
我从其他有类似问题的人那里得到了以下代码,但提出的解决方案不适用于我的 DataFrame。代码从给定日期中减去 Pandas DataFrame 索引:
my_date = pd.datetime.today()
MyDF['day_differential'] = (MyDF.index - my_date).days
Which is generating the following error in my DataFrame:
这在我的 DataFrame 中生成以下错误:
TypeError: Timestamp subtraction must have the same timezones or no timezones
How do I found out tz for both dates? How do I make them the same so I can calculate the number of days between them?
我如何找出两个日期的 tz ?我如何使它们相同,以便我可以计算它们之间的天数?
回答by EdChum
Here is an answer using J.F. Sebastian's comment thanks really to him, because your index has timezone information then the operations must also be timezone aware, in your case the timezone is utc so you need to generate a utc timestamp to perform the subtraction:
这是一个使用 JF Sebastian 评论的答案,非常感谢他,因为您的索引有时区信息,那么操作也必须知道时区,在您的情况下,时区是 utc,所以您需要生成一个 utc 时间戳来执行减法:
In [11]:
import pandas as pd
import numpy as np
import datetime as dt
my_date = pd.datetime.today()
MyDF = pd.DataFrame({'a':np.random.randn(5)})
MyDF.index = pd.date_range('1/1/2011', periods=5, freq='H', tz='utc')
MyDF['day_differential'] = MyDF.index.tz_convert(None) - dt.datetime.utcnow()
MyDF
Out[11]:
a day_differential
2011-01-01 00:00:00+00:00 1.399602 -1493 days +13:04:06.875715
2011-01-01 01:00:00+00:00 -1.962517 -1493 days +14:04:06.875715
2011-01-01 02:00:00+00:00 -1.574531 -1493 days +15:04:06.875715
2011-01-01 03:00:00+00:00 -0.224702 -1493 days +16:04:06.875715
2011-01-01 04:00:00+00:00 -0.800772 -1493 days +17:04:06.875715
You can find out if your index is timezone aware by ouputting the index:
您可以通过输出索引来确定您的索引是否可以识别时区:
In [12]:
MyDF.index
Out[12]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2011-01-01 00:00:00+00:00, ..., 2011-01-01 04:00:00+00:00]
Length: 5, Freq: H, Timezone: UTC
compare with a non timezone aware index:
与非时区感知索引进行比较:
In [14]:
MyDF.index
Out[14]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2011-01-01 00:00:00, ..., 2011-01-01 04:00:00]
Length: 5, Freq: H, Timezone: None

