gpt4 book ai didi

python - 属性错误 : 'tuple' object has no attribute 'lower'

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:36 24 4
gpt4 key购买 nike

我想指定日期的格式,因为它是欧洲格式(否则在我将其作为索引列后日期将不按顺序排列)。我完全按照教程进行了如下操作: enter image description here

但是在我执行之后

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'

为什么我得到了错误,而我遵循的错误却没有?我准确地复制了代码。它出什么问题了?谢谢。

最佳答案

列名中有双括号。

另外,为什么不让 pandas 为您工作呢?例子,

编辑:因为您不想考虑 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)

编辑 2:该行的解释[x[:-9] for x in df['date'].squeeze().tolist()]

df['date'].squeeze() -> squeeze dataframe column in a series

df['date'].squeeze().tolist() -> turn in into a list

[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

从您的子集数据中,这就是我得到的。 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

关于python - 属性错误 : 'tuple' object has no attribute 'lower' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47698720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com