简体   繁体   English

理解 to_datetime AttributeError: 'tuple' 对象没有属性 'lower'

[英]Understand to_datetime AttributeError: 'tuple' object has no attribute 'lower'

I have inherited some very messy legacy code that calls Google Analytics API and saves some metrics into a pandas dataframe.我继承了一些非常凌乱的遗留代码,这些代码调用 Google Analytics API 并将一些指标保存到 Pandas 数据帧中。

There is a date column which is a string and I want to convert to a date.有一个date列,它是一个字符串,我想转换为日期。 I would normally use pd.to_datetime, like this final['date'] = pd.to_datetime(final['date'], format='%Y%m%d', errors='coerce') but it gives me this error:我通常会使用 pd.to_datetime,就像这样final['date'] = pd.to_datetime(final['date'], format='%Y%m%d', errors='coerce')但它给了我这个错误:

AttributeError: 'tuple' object has no attribute 'lower'

This is description of the dataframe:这是数据框的描述:

print(final.info())

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 37047 entries, 0 to 37046
Data columns (total 5 columns):
(date,)               37047 non-null object
(landingPagePath,)    37047 non-null object
(sessions,)           37047 non-null object
(bounces,)            37047 non-null object
(market,)             37047 non-null object
dtypes: object(5)
memory usage: 1.4+ MB
None

I have found a similar SO question here and the solution works if I do在这里找到了一个类似的 SO 问题,如果我这样做,解决方案就有效

final['date'] = pd.to_datetime([x for x in final['date'].squeeze().tolist()], dayfirst=True) . final['date'] = pd.to_datetime([x for x in final['date'].squeeze().tolist()], dayfirst=True)

However I still do not understand what caused the issue in the first place.但是,我仍然不明白是什么导致了这个问题。 I guess that the column is somehow inconsistent, but I do not know how to identify which row is the culprit.我猜该列在某种程度上不一致,但我不知道如何确定哪一行是罪魁祸首。 What's the best way to find out?最好的方法是什么?

as in my very similar case, please check type(final['date']) .在我非常相似的情况下,请检查type(final['date']) pd.to_datetime() works mostly with pd.Series . pd.to_datetime()主要与pd.Series一起pd.Series

This could even explain why squeeze() works.这甚至可以解释为什么squeeze()起作用。 It converts DataFrame to Series .它将DataFrame转换为Series

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

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