3
votes

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: enter image description here

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.

3
why not use the parameter for european dates? pd.to_datetime(df['date'], dayfirst=True) - SamuelNLP
After I specify the date as index column. The order of the dates are all messed up. - saga
can you post a print of what you mean by messed up? - SamuelNLP
DatetimeIndex(['2015-01-01 12:00:00', '2015-01-01 13:00:00', '2015-01-01 14:00:00', '2015-01-01 15:00:00', '2015-01-01 16:00:00', '2015-01-01 17:00:00', '2015-01-01 18:00:00', '2015-01-31 19:00:00', '2015-01-31 20:00:00', '2015-01-31 21:00:00', ... '2017-01-12 11:00:00'] As you see, the date jumps from 2015-01-01 18:00 to 2015-01-31 19:00. But there is 2015-01-01 19:00 in the dataset in European format - saga
see my updated answer. - SamuelNLP

3 Answers

3
votes

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

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()]

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

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
0
votes

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

0
votes

Just do this:

df = pd.read_csv('GBPUSD.csv', index_col=0, parse_dates=True)