Change dataframe column names from string format to datetime

前端 未结 2 1239
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 16:16

I have a dataframe where the names of the columns are dates (Year-month) in the form of strings. How can I convert these names in datetime format? I tried doing this:

         


        
2条回答
  •  渐次进展
    2021-02-06 16:59

    If select by loc columns values was not changed, so get KeyError.

    So you need assign output to columns:

    df.columns = pd.to_datetime(df.columns)
    

    Sample:

    cols = ['2000-01-01', '2000-02-01', '2000-03-01', '2000-04-01', '2000-05-01']
    vals = np.arange(5)
    df = pd.DataFrame(columns = cols, data=[vals])
    print (df)
       2000-01-01  2000-02-01  2000-03-01  2000-04-01  2000-05-01
    0           0           1           2           3           4
    
    print (df.columns)
    Index(['2000-01-01', '2000-02-01', '2000-03-01', '2000-04-01', '2000-05-01'], dtype='object')
    
    df.columns = pd.to_datetime(df.columns)
    
    print (df.columns)
    DatetimeIndex(['2000-01-01', '2000-02-01', '2000-03-01', '2000-04-01',
                   '2000-05-01'],
                  dtype='datetime64[ns]', freq=None)
    

    Also is possible convert to period:

    print (df.columns)
    Index(['2000-01-01', '2000-02-01', '2000-03-01', '2000-04-01', '2000-05-01'], dtype='object')
    
    df.columns = pd.to_datetime(df.columns).to_period('M')
    
    print (df.columns)
    PeriodIndex(['2000-01', '2000-02', '2000-03', '2000-04', '2000-05'],
                 dtype='period[M]', freq='M')
    

提交回复
热议问题