繁体   English   中英

将具有多个时区的 Pandas 列转换为单个时区

[英]Convert pandas column with multiple timezones to single timezone

问题

我在 Pandas DataFrame 中有一个列,其中包含带时区的时间戳。 本专栏中有两个不同的时区,我需要确保只有一个。 这是列末尾的输出:

260003    2019-05-21 12:00:00-06:00
260004    2019-05-21 12:15:00-06:00
Name: timestamp, Length: 260005, dtype: object

对于它的价值,时间戳在-06:00-07:00之间变化,并具有以下输出:

datetime.datetime(2007, 10, 1, 1, 0, tzinfo=tzoffset(None, -21600)) for -06:00 datetime.datetime(2007, 11, 17, 5, 15, tzinfo=tzoffset(None, -25200))-07:00

我做过什么

我一直在尝试使用 tz.localize 和 tz.convert,它们过去运行良好,但我认为数据只有一个时区。 例如,如果我这样做:

df['timestamp'].dt.tz_localize('MST', ambiguous='infer').dt.tz_convert('MST')

我得到:

ValueError: Array must be all same time zone

During handling of the above exception, another exception occurred:

ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True

有没有办法将这些转换为 MST? 或者任何时区,真的吗? 我想我可以按时区分解 DataFrame(不是 100% 确定如何,但我认为这是可能的)并对其进行处理,但我想我想看看是否有更智能的解决方案。 谢谢!

我试过:

df = pd.DataFrame({'timestamp':['2019-05-21 12:00:00-06:00',
                                '2019-05-21 12:15:00-07:00']})
df['timestamp'] = pd.to_datetime(df.timestamp)

df.timestamp.dt.tz_localize('MST')

工作正常并给出:

0   2019-05-21 18:00:00-07:00
1   2019-05-21 19:15:00-07:00
Name: timestamp, dtype: datetime64[ns, MST]

这不是你所期望的吗?


编辑:感谢@G.Anderson 的评论,我尝试了具有时区感知时间戳的不同数据:

df = pd.DataFrame({'timestamp':[pd.to_datetime('2019-05-21 12:00:00').tz_localize('MST'),
                         pd.to_datetime('2019-05-21 12:15:00').tz_localize('EST')]})

然后

df['timestamp'] = pd.to_datetime(df.timestamp)

确实给出了同样的错误。 然后我添加了utc=True

df.timestamp = pd.to_datetime(df.timestamp, utc=True)

# df.timestamp
# 0   2019-05-21 19:00:00+00:00
# 1   2019-05-21 17:15:00+00:00
# Name: timestamp, dtype: datetime64[ns, UTC]

df.timestamp.dt.tz_convert('MST')

工作正常并给出:

0   2019-05-21 12:00:00-07:00
1   2019-05-21 10:15:00-07:00
Name: timestamp, dtype: datetime64[ns, MST]
# input data
import pandas as pd
series = pd.Series(data=
    [pd.to_datetime('2019-01-01 00:00:00').tz_localize('MST'),
     pd.to_datetime('2019-01-01 01:10:00').tz_localize('UTC')])
print(series)

0    2019-01-01 00:00:00-07:00
1    2019-01-01 01:10:00+00:00
dtype: object

的确,

series.dt.tz_convert('MST')

给出"ValueError: Array must be all same time zone""ValueError: Tz-aware datetime.datetime cannot be convert to datetime64 until utc=True" 所以,看起来,你必须以非矢量化的方式来做:

new_series = pd.Series(index=series.index,
    data=[x.tz_convert('MST') for x in series])
print(new_series)

0   2019-01-01 00:00:00-07:00
1   2018-12-31 18:10:00-07:00
dtype: datetime64[ns, MST]

编辑:如果@QuangHoang 是正确的(即“选项自动将时间戳本地化为 utc” ,请仔细检查它!)关于pd.to_datetime(..., utc=True)的含义,那么以下解决方案也将起作用:

new_series = pd.to_datetime(series, utc=True).dt.tz_convert('MST')
print(new_series)

0   2019-01-01 00:00:00-07:00
1   2018-12-31 18:10:00-07:00
dtype: datetime64[ns, MST]

让我们有一个具有多个不同时区的系列a 我们期望a.tz_convert()a.tz_localize()工作,但他们没有。 解决方法是使用apply方法。 请参阅以下示例:

> a
0    2019-10-04 16:30:00+02:00
1    2019-10-07 16:00:00-04:00
2    2019-09-24 08:30:00-07:00
Name: localized, dtype: object

> a.iloc[0]
Timestamp('2019-10-04 16:30:00+0200', tz='Europe/Amsterdam')

> a.apply(lambda x: x.tz_convert('America/Los_Angeles'))
0   2019-10-04 07:30:00-07:00
1   2019-10-07 13:00:00-07:00
2   2019-09-24 08:30:00-07:00
Name: localized, dtype: datetime64[ns, America/Los_Angeles]

# Make it tz-naive, i.e. remove tz info, note you lose information here, you might want to store tz-info in another series before the conversion.
> a.apply(lambda x: x.tz_localize(None))
0   2019-10-04 16:30:00
1   2019-10-07 16:00:00
2   2019-09-24 08:30:00
Name: localized, dtype: datetime64[ns]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM