【问题标题】:How to convert UTC datetime to local datetime (Australia/Melbourne) in Python如何在 Python 中将 UTC 日期时间转换为本地日期时间(澳大利亚/墨尔本)
【发布时间】:2021-12-23 15:30:01
【问题描述】:

我有一个带有 UTC 格式“日期”列的数据框。

Date
2021-10-14T06:57:00.000+0000
2021-09-05T08:30:00.000+0000
2021-10-20T04:34:00.000+0000
2021-10-19T21:49:00.000+0000
2021-09-30T20:53:00.000+0000

试过了,没用;

df['Date'] = df['Date'].substr(replace(to_iso8601(from_iso8601_timestamp(Date) AT TIME ZONE 'Australia/Melbourne'), 'T', ' '), 1, 16) Date_local

我无法将 UTC 时间转换为当地时区(澳大利亚/墨尔本)。

任何帮助将不胜感激。

【问题讨论】:

标签: python dataframe datetime timezone python-datetime


【解决方案1】:

使用熊猫功能; pd.to_datetime,然后是tz_convert

# input strings to datetime data type:
df['Date'] = pd.to_datetime(df['Date'])

# UTC is already set (aware datetime); just convert:
df['Date'] = df['Date'].dt.tz_convert('Australia/Melbourne')

df['Date']
Out[2]: 
0   2021-10-14 17:57:00+11:00
1   2021-09-05 18:30:00+10:00
2   2021-10-20 15:34:00+11:00
3   2021-10-20 08:49:00+11:00
4   2021-10-01 06:53:00+10:00
Name: Date, dtype: datetime64[ns, Australia/Melbourne]

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 2020-07-08
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2013-02-14
    相关资源
    最近更新 更多