【发布时间】:2021-01-11 22:24:44
【问题描述】:
所以我最初的问题是 SettingwithCopyWarning,当我开始遇到这个关键错误时,我试图解决这个问题。原始(预期代码)是:
df['Dates'] = pd.to_datetime(df['Dates'])
这有效,但给了我在切片上设置副本的警告(确切的警告是:试图在数据帧的切片副本上设置值。尝试使用 .loc[row_indexer,col_indexer] = value而是...)
所以,为了修复它,我这样做了:
df['Date'] = pd.to_datetime(df.loc[df['Date'],])
还有这个:
pd.to_datetime(df.loc[df['Date'],])
这给了我这个新警告:
KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Index(['10/1/2020', '10/1/2020', '10/1/2020', '10/1/2020', '10/1/2020',\n...dtype='object')] are in the [index]"
我也尝试使用该列,但出现语法错误:
df['Date'] = pd.to_datetime(df.loc[,df['Date']])
SyntaxError: invalid syntax
我真的很难过,并试图避免我的代码完全破坏应该是简单地将日期列中的字符串值转换为日期时间格式的东西,以便我可以对其进行正确排序或过滤。任何帮助将不胜感激。
【问题讨论】:
标签: python pandas dataframe datetime