[英]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:我完全按照教程做的如下:
But after I execute但是在我执行之后
df.date=pd.to_datetime(df.date,format='%d.%m.%Y %H:%M:%S.%f')
I get this error我收到这个错误
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.谢谢。
You have double brackets in the columns name.列名称中有双括号。
Also why not let pandas work for you?还有为什么不让大熊猫为你工作? Example,例子,
EDIT: since you don't want the GMT part to be taken into account, I removed it with a list comprehension编辑:由于您不希望考虑 GMT 部分,因此我使用列表理解将其删除
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)
EDIT 2: explanation of the line [x[:-9] for x in df['date'].squeeze().tolist()]
编辑 2: [x[:-9] for x in df['date'].squeeze().tolist()]
行[x[:-9] for x in df['date'].squeeze().tolist()]
df['date'].squeeze()
-> squeeze dataframe column in a seriesdf['date'].squeeze()
-> 挤压系列中的数据框列
df['date'].squeeze().tolist()
-> turn in into a listdf['date'].squeeze().tolist()
-> 变成一个列表
[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[x[:-9] for x in df['date'].squeeze().tolist()]
-> 对于列表中的每个日期,只保留元素直到从末尾算起第 9 次,这意味着删除 GMT 部分
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. Pandas 足够聪明,可以理解 GMT-0500 并考虑到这一点来转换日期。
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
I am also replicating this algorithm and had the same error till I realized it was an issue with how I downloaded the data.我也在复制这个算法并且遇到了同样的错误,直到我意识到这是我下载数据的方式的问题。 Select GMT when downloading from Dukasopy instead of local then you can use his original code从 Dukasopy 下载时选择 GMT 而不是本地然后你可以使用他的原始代码
Just do this:只需这样做:
df = pd.read_csv('GBPUSD.csv', index_col=0, parse_dates=True) df = pd.read_csv('GBPUSD.csv', index_col=0, parse_dates=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.