所以我已将熊猫升级到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')
答案 0 :(得分: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