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

Viewed 1474

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'
2 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

Assign your column names with a 1-dimension array

df.columns = ['date', 'open', 'high', 'low', 'close', 'volume']

You can also let read_csv do all

  • header to not use file header
  • names to give the new col names
  • parse_dates to specify which col parse to date
  • infer_datetime_dates may fasten date parsing
df = pd.read_csv('file123.txt', header=0, 
                 names=['date', 'open', 'high', 'low', 'close', 'volume'],
                 parse_dates=['date'], infer_datetime_format=True)
Related