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().
问问题
31618 次
3 回答
22
read_csv
您可以通过手动设置函数直接将日期读取为 UTC date_parser
,例如:
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)
.
如果您正在创建时间序列,则可以使用以下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
.
如果您的 DataFrame/Series 已经按时间序列索引,则可以使用该tz_localize
方法设置时区:
df.tz_localize('UTC')
或者如果它已经有一个时区,请使用tz_convert
:
df.tz_convert('UTC')
于 2012-12-22T10:50:53.967 回答
9
# 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)
给
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
于 2018-04-27T12:10:33.053 回答
0
df['date'] = df['date'].dt.tz_localize('UTC')
这似乎从我的“天真”时区开始工作。
于 2022-02-08T04:14:15.623 回答