如何在 Python 中将 UTC 日期时间转换为本地日期时间 (Australia/Melbourne)

How to convert UTC datetime to local datetime (Australia/Melbourne) in Python

我有一个包含 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 时间转换为本地时区 (Australia/Melbourne)。

非常感谢任何帮助。

使用pandas功能; pd.to_datetime and then 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]