AttributeError: 'tuple' object has no attribute 'lower'

后端 未结 3 1545
一向
一向 2020-12-11 22:54

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 t

3条回答
  •  自闭症患者
    2020-12-11 23:34

    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
    

提交回复
热议问题