如何在 Pandas DataFrame 中设置值的时区? [英] How to set time zone of values in a Pandas DataFrame?
问题描述
我想设置 Pandas DataFrame 中列值的时区.我正在使用 pandas.read_csv() 读取 DataFrame.
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().
推荐答案
您可以直接从 read_csv
通过手动设置 date_parser
函数,例如:
You can read dates as UTC directly from read_csv
by setting the date_parser
function manually, for example:
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)
.
如果您正在创建时间序列,则可以使用 date_range
的 tz
参数:
If you are creating a timeseries, you can use the tz
argument of 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
设置时区的方法:
If your DataFrame/Series is already index by a timeseries, you can use the tz_localize
method to set a timezone:
df.tz_localize('UTC')
或者如果它已经有时区,请使用 tz_convert
:
or if it already has a timezone, use tz_convert
:
df.tz_convert('UTC')
这篇关于如何在 Pandas DataFrame 中设置值的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!