如何在 Pandas DataFrame 中设置值的时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14002158/
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
How to set time zone of values in a Pandas DataFrame?
提问by Yariv
I'd like to set the time zone of the values of a column in a Pandas DataFrame. I am reading the DataFrame with pandas.read_csv().
我想设置 Pandas DataFrame 中列值的时区。我正在使用 pandas.read_csv() 读取 DataFrame。
回答by Andy Hayden
You can read dates as UTC directly from read_csvby setting the date_parserfunction manually, for example:
您可以read_csv通过date_parser手动设置函数来直接读取 UTC 日期,例如:
from dateutil.tz import tzutc
from dateutil.parser import parse
def date_utc(s):
return parse(s, tzinfos=tzutc)
df = read_csv('my.csv', parse_dates=[0], date_parser=date_utc)
.
.
If you are creating a timeseries, you can use the tzargument of date_range:
如果您要创建时间序列,则可以使用以下tz参数date_range:
dd = pd.date_range('2012-1-1 1:30', periods=3, freq='min', tz='UTC')
In [2]: dd
Out[2]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-01-01 01:30:00, ..., 2012-01-01 01:32:00]
Length: 3, Freq: T, Timezone: UTC
.
.
If your DataFrame/Series is already index by a timeseries, you can use the tz_localizemethod to set a timezone:
如果您的 DataFrame/Series 已经由时间序列索引,则可以使用该tz_localize方法设置时区:
df.tz_localize('UTC')
or if it already has a timezone, use tz_convert:
或者如果它已经有时区,请使用tz_convert:
df.tz_convert('UTC')
回答by Martin Thoma
# core modules
from datetime import timezone, datetime
# 3rd party modules
import pandas as pd
import pytz
# create a dummy dataframe
df = pd.DataFrame({'date': [datetime(2018, 12, 30, 20 + i, 56)
for i in range(2)]},)
print(df)
# Convert the time to a timezone-aware datetime object
df['date'] = df['date'].dt.tz_localize(timezone.utc)
print(df)
# Convert the time from to another timezone
# The point in time does not change, only the associated timezone
my_timezone = pytz.timezone('Europe/Berlin')
df['date'] = df['date'].dt.tz_convert(my_timezone)
print(df)
gives
给
date
0 2018-12-30 20:56:00
1 2018-12-30 21:56:00
date
0 2018-12-30 20:56:00+00:00
1 2018-12-30 21:56:00+00:00
date
0 2018-12-30 21:56:00+01:00
1 2018-12-30 22:56:00+01:00

