Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas to_datetime AttributeError: 'tuple' object has no attribute 'lower'

Tags:

python

pandas

I have a csv consisting of 6 columns, the first has a particular date formatting, so I need to transform it into US format YYYY-mm-dd

after reading the contents of the CSV file I proceed with the modification of the 'date' column but I keep getting the following error

CSV

Gmt time,Open,High,Low,Close,Volume
01.01.2021 00:00:00.000,1.22174,1.22174,1.22174,1.22174,0
01.01.2021 01:00:00.000,1.22174,1.22174,1.22174,1.22174,0
01.01.2021 02:00:00.000,1.22174,1.22174,1.22174,1.22174,0
01.01.2021 03:00:00.000,1.22174,1.22174,1.22174,1.22174,0
01.01.2021 04:00:00.000,1.22174,1.22174,1.22174,1.22174,0

PYTHON

import pandas as pd

df = pd.read_csv('history/EURUSDhours.csv')
df.columns = [['date','open','high','low','close','volume']]
df.date= pd.to_datetime((df.date),format='%d.%m.%Y %H:%M:%S.%f')
print(df.head)

DTYPES

date       object
open      float64
high      float64
low       float64
close     float64
volume      int64
dtype: object

ERROR

Traceback (most recent call last):
  File "eurusd.py", line 5, in <module>
    df.date= pd.to_datetime((df.date),format='%d.%m.%Y %H:%M:%S.%f')
  File "\venv\lib\site-packages\pandas\core\tools\datetimes.py", line 808, in to_datetime
    result = _assemble_from_unit_mappings(arg, errors, tz)
  File "\venv\lib\site-packages\pandas\core\tools\datetimes.py", line 899, in _assemble_from_unit_mappings
    unit = {k: f(k) for k in arg.keys()}
  File "\venv\lib\site-packages\pandas\core\tools\datetimes.py", line 899, in <dictcomp>
    unit = {k: f(k) for k in arg.keys()}
  File "\venv\lib\site-packages\pandas\core\tools\datetimes.py", line 894, in f
    if value.lower() in _unit_map:
AttributeError: 'tuple' object has no attribute 'lower'
like image 727
SartuX Avatar asked Apr 29 '26 10:04

SartuX


1 Answers

Issue was passing list of list of elements to df.columns.

import pandas as pd

df = pd.read_csv('history/EURUSDhours.csv')
df.columns = ['date','open','high','low','close','volume']
df.date= pd.to_datetime(df.date,format='%d.%m.%Y %H:%M:%S.%f')
df

Output

    date    open    high    low close   volume
0   2021-01-01 00:00:00 1.22174 1.22174 1.22174 1.22174 0
1   2021-01-01 01:00:00 1.22174 1.22174 1.22174 1.22174 0
2   2021-01-01 02:00:00 1.22174 1.22174 1.22174 1.22174 0
3   2021-01-01 03:00:00 1.22174 1.22174 1.22174 1.22174 0
4   2021-01-01 04:00:00 1.22174 1.22174 1.22174 1.22174 0
like image 69
Utsav Avatar answered May 01 '26 22:05

Utsav