Pandas升级并且已经识别tz,使用tz_convert进行转换

问题描述 投票:0回答:1

所以我把熊猫升级到了0.24.2,我有一个:

 Already tz-aware, use tz_convert to convert.

对于这一行:

df["Created"] = df['Created'] = df['Created'].dt.tz_localize('Europe/London', nonexistent='shift_forward').dt.tz_convert('Europe/Paris')

但我需要转换它,因为如果不是我有一个不存在的时间错误。

所以我看到这个消息:

"TypeError: Already tz-aware, use tz_convert to convert." when I update pandas

这是我的代码:

data = result["Document"]
df = pd.DataFrame(data)

df["Created"] = pd.to_datetime(df["Created"])

df["Created"] = df['Created'] = df['Created'].dt.tz_localize('Europe/London', nonexistent='shift_forward').dt.tz_convert('Europe/Paris')
python pandas
1个回答
0
投票
df["Created"] = df['Created'] = df['Created']...

可能不正确,但不太可能是错误的根本原因。

Already tz-aware, use tz_convert to convert.错误无法再现如下。可能由于缺少适当的输入数据。

下面的输入数据以Datetime开头为天真(没有tz),希望调用tz_localize来解释错误。

import pandas as pd
from pandas.compat import StringIO

print(pd.__version__)

csvdata = StringIO("""Created,open,high,low,close,volume
2019-04-02 05:59:00,381.00,381.00,379.70,379.70,0
2019-04-02 05:58:00,380.90,380.90,380.85,380.85,5040
,380.85,380.95,380.65,380.95,9615
2019-04-02 05:56:00,380.60,381.20,380.60,381.00,13041
2019-04-02 05:55:00,379.80,380.60,379.80,380.60,19586""")

#df = pd.read_csv(csvdata, sep=",", index_col="Created", parse_dates=True, infer_datetime_format=True)
df = pd.read_csv(csvdata, sep=",")

df["Created"] = pd.to_datetime(df["Created"])
print(df)

df['Created'] = df['Created'].dt.tz_localize('Europe/London', nonexistent='shift_forward').dt.tz_convert('Europe/Paris')
print(df)

结果是

0.24.2
              Created    open    high     low   close  volume
0 2019-04-02 05:59:00  381.00  381.00  379.70  379.70       0
1 2019-04-02 05:58:00  380.90  380.90  380.85  380.85    5040
2                 NaT  380.85  380.95  380.65  380.95    9615
3 2019-04-02 05:56:00  380.60  381.20  380.60  381.00   13041
4 2019-04-02 05:55:00  379.80  380.60  379.80  380.60   19586
                    Created    open    high     low   close  volume
0 2019-04-02 06:59:00+02:00  381.00  381.00  379.70  379.70       0
1 2019-04-02 06:58:00+02:00  380.90  380.90  380.85  380.85    5040
2                       NaT  380.85  380.95  380.65  380.95    9615
3 2019-04-02 06:56:00+02:00  380.60  381.20  380.60  381.00   13041
4 2019-04-02 06:55:00+02:00  379.80  380.60  379.80  380.60   19586


推荐问答