AttributeError:'tuple'对象没有属性'lower' [英] AttributeError: 'tuple' object has no attribute 'lower'
问题描述
我想指定日期的格式,因为它是欧洲格式的(否则,将日期设置为索引列后日期将不按顺序排列).我完全按照本教程进行了如下操作:
I wanted to specify the format of the date because it's in European format(Or else the dates will not be in order after I make it as index column). I did exactly from the tutorial as follow:
但是我执行
df.date=pd.to_datetime(df.date,format='%d.%m.%Y %H:%M:%S.%f')
我收到此错误
df = pd.read_csv("F:\Python\Jupyter notes\AUDCAD1h.csv")
df.columns = [['date', 'open','high','low','close','volume']]
df.head()
Out[66]:
date open high low close volume
0 01.01.2015 00:00:00.000 GMT-0500 0.94821 0.94821 0.94821 0.94821 0.0
1 01.01.2015 01:00:00.000 GMT-0500 0.94821 0.94821 0.94821 0.94821 0.0
2 01.01.2015 02:00:00.000 GMT-0500 0.94821 0.94821 0.94821 0.94821 0.0
3 01.01.2015 03:00:00.000 GMT-0500 0.94821 0.94821 0.94821 0.94821 0.0
4 01.01.2015 04:00:00.000 GMT-0500 0.94821 0.94821 0.94821 0.94821 0.0
df.Date=pd.to_datetime(df.date,format='%d.%m.%Y %H:%M:%S.%f')
Traceback (most recent call last):
File "<ipython-input-67-29b50fd32986>", line 1, in <module>
df.Date=pd.to_datetime(df.date,format='%d.%m.%Y %H:%M:%S.%f')
File "C:\Users\AM\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 376, in to_datetime
result = _assemble_from_unit_mappings(arg, errors=errors)
File "C:\Users\AM\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 446, in _assemble_from_unit_mappings
unit = {k: f(k) for k in arg.keys()}
File "C:\Users\AM\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 446, in <dictcomp>
unit = {k: f(k) for k in arg.keys()}
File "C:\Users\AM\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 441, in f
if value.lower() in _unit_map:
AttributeError: 'tuple' object has no attribute 'lower'
我怎么会得到错误,但我没有遵循该错误?我完全复制了代码.它出什么问题了?谢谢.
How come I got the error but the one I followed didn't? I copied the code exactly. What's wrong with it? Thanks.
推荐答案
列名中有两个中括号.
为什么不让熊猫为您服务?例如,
Also why not let pandas work for you? Example,
由于您不希望考虑GMT部分,因此我以列表理解的方式将其删除了
since you don't want the GMT part to be taken into account, I removed it with a list comprehension
import pandas as pd
df = pd.read_csv("date_t.csv")
print(df)
df.columns = ['date', 'open','high','low','close','volume']
df['date'] = pd.to_datetime([x[:-9] for x in df['date'].squeeze().tolist()], dayfirst=True)
df.set_index('date', inplace=True)
print(df)
该行的说明
[x[:-9] for x in df['date'].squeeze().tolist()]
EDIT 2: explanation of the line
[x[:-9] for x in df['date'].squeeze().tolist()]
df['date'].squeeze()
->压缩系列中的dataframe列
df['date'].squeeze()
-> squeeze dataframe column in a series
df['date'].squeeze().tolist()
->进入列表
[x[:-9] for x in df['date'].squeeze().tolist()]
->对于列表中的每个日期,仅保留元素直到从末尾开始第9个计数,这意味着删除了GMT部分
[x[:-9] for x in df['date'].squeeze().tolist()]
-> for each date in the list keep only the elements until the 9th counting from the end, meaning remove the GMT part
这是我从子集数据中得到的.熊猫足够聪明,可以理解GMT-0500并考虑到这一点来转换日期.
From your subset data, this is what I get. Pandas is smart enough to understand the GMT-0500 and convert the dates taking this into account.
1 2 3 4 5 6
0 01.01.2015 00:00:00.000 GMT-0500 0.94821 0.94821 0.94821 0.94821 0
1 01.01.2015 01:00:00.000 GMT-0500 0.94821 0.94821 0.94821 0.94821 0
2 01.01.2015 02:00:00.000 GMT-0500 0.94821 0.94821 0.94821 0.94821 0
3 01.01.2015 03:00:00.000 GMT-0500 0.94821 0.94821 0.94821 0.94821 0
4 01.01.2015 04:00:00.000 GMT-0500 0.94821 0.94821 0.94821 0.94821 0
open high low close volume
date
2015-01-01 00:00:00 0.94821 0.94821 0.94821 0.94821 0.0
2015-01-01 01:00:00 0.94821 0.94821 0.94821 0.94821 0.0
2015-01-01 02:00:00 0.94821 0.94821 0.94821 0.94821 0.0
2015-01-01 03:00:00 0.94821 0.94821 0.94821 0.94821 0.0
2015-01-01 04:00:00 0.94821 0.94821 0.94821 0.94821 0.0
这篇关于AttributeError:'tuple'对象没有属性'lower'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!