简体   繁体   English

将原始日期时间列转换为新时区 Pandas Dataframe

[英]Converting a naive datetime column to a new timezone Pandas Dataframe

I have the following dataframe, named 'ORDdataM', with a DateTimeIndex column 'date', and a price point column 'ORDprice'.我有以下 dataframe,名为“ORDdataM”,带有 DateTimeIndex 列“date”和价格点列“ORDprice”。 The date column has no timezone associated with it (and is naive) but is actually in 'Australia/ACT'.日期列没有与之关联的时区(并且很幼稚),但实际上位于“澳大利亚/ACT”中。 I want to convert it into 'America/New_York' time.我想将其转换为“美国/纽约”时间。

                    ORDprice
date    
2021-02-23 18:09:00 24.01
2021-02-23 18:14:00 23.91
2021-02-23 18:19:00 23.98
2021-02-23 18:24:00 24.00
2021-02-23 18:29:00 24.04
... ...
2021-02-25 23:44:00 23.92
2021-02-25 23:49:00 23.88
2021-02-25 23:54:00 23.92
2021-02-25 23:59:00 23.91
2021-02-26 00:09:00 23.82

The line below is one that I have played around with quite a bit, but I cannot figure out what is erroneous.下面的行是我玩过很多次的行,但我无法弄清楚什么是错误的。 The only error message is: KeyError: 'date'唯一的错误信息是: KeyError: 'date'

ORDdataM['date'] = ORDdataM['date'].dt.tz_localize('Australia/ACT').dt.tz_convert('America/New_York')

I have also tried我也试过

ORDdataM.date = ORDdataM.date.dt.tz_localize('Australia/ACT').dt.tz_convert('America/New_York')

What is the issue here?这里有什么问题?

Your date is index not a column, try:您的date是索引而不是列,请尝试:

df.index = df.index.tz_localize('Australia/ACT').tz_convert('America/New_York')

df
#                           ORDprice
#date                               
#2021-02-23 02:09:00-05:00     24.01
#2021-02-23 02:14:00-05:00     23.91
#2021-02-23 02:19:00-05:00     23.98
#2021-02-23 02:24:00-05:00     24.00
#2021-02-23 02:29:00-05:00     24.04
#2021-02-25 07:44:00-05:00     23.92
#2021-02-25 07:49:00-05:00     23.88
#2021-02-25 07:54:00-05:00     23.92
#2021-02-25 07:59:00-05:00     23.91
#2021-02-25 08:09:00-05:00     23.82

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

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