Python 转换时区熊猫数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22800079/
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 time zone pandas dataframe
提问by user1234440
I have data:
我有数据:
Symbol bid ask
Timestamp
2014-01-01 21:55:34.378000 EUR/USD 1.37622 1.37693
2014-01-01 21:55:40.410000 EUR/USD 1.37624 1.37698
2014-01-01 21:55:47.210000 EUR/USD 1.37619 1.37696
2014-01-01 21:55:57.963000 EUR/USD 1.37616 1.37696
2014-01-01 21:56:03.117000 EUR/USD 1.37616 1.37694
The timestamp is of GMT. Is there a way to convert that to Eastern?
时间戳为 GMT。有没有办法将其转换为东方?
Note when I do:
当我这样做时请注意:
data.index
I get output:
我得到输出:
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-01-01 21:55:34.378000, ..., 2014-01-01 21:56:03.117000]
Length: 5, Freq: None, Timezone: None
采纳答案by unutbu
Localize the index (using tz_localize
) to UTC (to make the Timestamps timezone-aware) and then convert to Eastern (using tz_convert
):
将索引(使用tz_localize
)本地化为 UTC(使时间戳时区感知),然后转换为东部(使用tz_convert
):
import pytz
eastern = pytz.timezone('US/Eastern')
df.index = index.tz_localize(pytz.utc).tz_convert(eastern)
For example:
例如:
import pandas as pd
import pytz
index = pd.date_range('20140101 21:55', freq='15S', periods=5)
df = pd.DataFrame(1, index=index, columns=['X'])
print(df)
# X
# 2014-01-01 21:55:00 1
# 2014-01-01 21:55:15 1
# 2014-01-01 21:55:30 1
# 2014-01-01 21:55:45 1
# 2014-01-01 21:56:00 1
# [5 rows x 1 columns]
print(df.index)
# <class 'pandas.tseries.index.DatetimeIndex'>
# [2014-01-01 21:55:00, ..., 2014-01-01 21:56:00]
# Length: 5, Freq: 15S, Timezone: None
eastern = pytz.timezone('US/Eastern')
df.index = index.tz_localize(pytz.utc).tz_convert(eastern)
print(df)
# X
# 2014-01-01 16:55:00-05:00 1
# 2014-01-01 16:55:15-05:00 1
# 2014-01-01 16:55:30-05:00 1
# 2014-01-01 16:55:45-05:00 1
# 2014-01-01 16:56:00-05:00 1
# [5 rows x 1 columns]
print(df.index)
# <class 'pandas.tseries.index.DatetimeIndex'>
# [2014-01-01 16:55:00-05:00, ..., 2014-01-01 16:56:00-05:00]
# Length: 5, Freq: 15S, Timezone: US/Eastern
回答by wordsforthewise
Recently (I think in 2017) some datetime functionality has been added to pandas, and this is built-in. You can convert timezones with tz_convert()
. If your data column/index is not timezone-aware, you will get a warning, and should first make the data timezone-aware with tz_localize
.
最近(我认为在 2017 年)一些日期时间功能已添加到 Pandas,这是内置的。您可以使用tz_convert()
. 如果您的数据列/索引不是时区感知的,您将收到警告,并且应首先使用tz_localize
.
df = pd.DataFrame({'Symbol': ['EUR/USD'] * 5,
'bid': [1.37622, 1.37624, 1.37619, 1.37616, 1.37616],
'ask': [1.37693, 1.37698, 1.37696, 1.37696, 1.37694]})
df.index = pd.to_datetime(['2014-01-01 21:55:34.378000',
'2014-01-01 21:55:40.410000',
'2014-01-01 21:55:47.210000',
'2014-01-01 21:55:57.963000',
'2014-01-01 21:56:03.117000'])
df.index = df.index.tz_localize('GMT')
df.index = df.index.tz_convert('America/New_York')
This also works similarly for datetime columns:
这也适用于日期时间列:
df['column'] = df['column'].dt.tz_convert('America/New_York')
回答by Shivendra Rajawat
To convert EST time into Asia tz
将 EST 时间转换为 Asia tz
df.index = data.index.tz_localize('EST')
df.index = data.index.tz_convert('Asia/Kolkata')
Pandas has now inbuilt tz conversion ability.
Pandas 现在内置了 tz 转换功能。