How do I properly set the Datetimeindex for a Pandas datetime object in a dataframe?

后端 未结 2 2018
醉话见心
醉话见心 2021-01-30 05:14

I have a pandas dataframe:

    lat         lng         alt days              date        time
0   40.003834   116.321462  211 39745.175405      2008-10-24  04:12         


        
相关标签:
2条回答
  • 2021-01-30 05:39

    You are not creating datetime index properly,

    format = '%Y-%m-%d %H:%M:%S'
    df['Datetime'] = pd.to_datetime(df['date'] + ' ' + df['time'], format=format)
    df = df.set_index(pd.DatetimeIndex(df['Datetime']))
    
    0 讨论(0)
  • 2021-01-30 05:49

    To simplify Kirubaharan's answer a bit:

    df['Datetime'] = pd.to_datetime(df['date'] + ' ' + df['time'])
    df = df.set_index('Datetime')
    

    And to get rid of unwanted columns (as OP did but did not specify per se in the question):

    df = df.drop(['date','time'], axis=1)
    
    0 讨论(0)
提交回复
热议问题