Python 从熊猫时间戳转换时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25653529/
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
Converting timezones from pandas Timestamps
提问by Amelio Vazquez-Reina
I have the following in a dataframe:
我在数据框中有以下内容:
> df['timestamps'].loc[0]
Timestamp('2014-09-02 20:24:00')
I know the timezone(I think it is GMT) it uses and would like to convert the entire column to EST. How can I do that in Pandas?
我知道它使用的时区(我认为是GMT)并希望将整个列转换为EST。我怎么能在 Pandas 中做到这一点?
For reference, I found these other threads:
作为参考,我发现了这些其他线程:
- Changing a unix timestamp to a different timezone
- Pandas: Read Timestamp from CSV in GMT then resample
but they work with datetimetimestamps. E.g.:
但它们使用datetime时间戳。例如:
> datetime.datetime.fromtimestamp(df['timestamps'].loc[0], tz=None)
returns:
TypeError Traceback (most recent call last)
----> 2 datetime.datetime.fromtimestamp(ts, tz=None)
TypeError: an integer is required (got type Timestamp)
采纳答案by rhlobo
Just use tz_convertmethod.
就用tz_convert方法吧。
Lets say you have a Timestamp object:
假设您有一个 Timestamp 对象:
stamp = Timestamp('1/1/2014 16:20', tz='America/Sao_Paulo')
new_stamp = stamp.tz_convert('US/Eastern')
If you are interested in converting date ranges:
如果您有兴趣转换日期范围:
range = date_range('1/1/2014', '1/1/2015', freq='S', tz='America/Sao_Paulo')
new_range = range.tz_convert('US/Eastern')
For large time series:
对于大型时间序列:
import numpy as np
ts = Series(np.random.randn(len(range)), range)
new_ts = ts.tz_convert('US/Eastern')
As stated in another answer, if your data does not have a timezone set, you'll need to tz_localizeit:
正如另一个答案中所述,如果您的数据没有设置时区,则需要tz_localize:
data.tz_localize('utc')
回答by Andy Hayden
datetime's fromtimestamp is actually from a POSIX timestamp i.e. ms from 1970-1-1 GMT
datetime 的 fromtimestamp 实际上来自 POSIX 时间戳,即 1970-1-1 GMT 的毫秒
In [11]: datetime.datetime.fromtimestamp?
Type: builtin_function_or_method
String form: <built-in method fromtimestamp of type object at 0x101d90500>
Docstring: timestamp[, tz] -> tz's local time from POSIX timestamp.
In [12]: datetime.datetime.fromtimestamp(0)
Out[12]: datetime.datetime(1969, 12, 31, 16, 0)
In [13]: datetime.datetime.fromtimestamp(1)
Out[13]: datetime.datetime(1969, 12, 31, 16, 0, 1)
I think maybe is an issue as I'm in PST timezone.
我认为可能是一个问题,因为我在 PST 时区。
This is different from pandas Timestamp (although under the hood that is ns from 1970-1-1).
这与 Pandas Timestamp 不同(尽管从 1970-1-1 开始是 ns)。
In [21]: pd.Timestamp(0)
Out[21]: Timestamp('1970-01-01 00:00:00')
To convert a Timestamp/datetime64 column use tz_convert (if the are tz naive, i.e. don't have a timezone yet, you'll need to tz_localize first):
要转换 Timestamp/datetime64 列,请使用 tz_convert (如果 tz 天真,即还没有时区,您需要先 tz_localize):
In [31]: pd.Timestamp(0).tz_localize('UTC')
Out[31]: Timestamp('1970-01-01 00:00:00+0000', tz='UTC')
In [32]: t = pd.Timestamp(0).tz_localize('UTC')
In [33]: t.tz_convert('US/Eastern')
Out[33]: Timestamp('1969-12-31 19:00:00-0500', tz='US/Eastern')

