python pandas convert index to datetime

后端 未结 3 1593
感动是毒
感动是毒 2020-11-28 23:55

How do i convert a pandas index of strings to datetime format

my dataframe \'df\' is like this

                     value          
2015-09-25 00:46          


        
3条回答
  •  孤城傲影
    2020-11-29 00:37

    You could explicitly create a DatetimeIndex when initializing the dataframe. Assuming your data is in string format

    data = [
        ('2015-09-25 00:46', '71.925000'),
        ('2015-09-25 00:47', '71.625000'),
        ('2015-09-25 00:48', '71.333333'),
        ('2015-09-25 00:49', '64.571429'),
        ('2015-09-25 00:50', '72.285714'),
    ]
    
    index, values = zip(*data)
    
    frame = pd.DataFrame({
        'values': values
    }, index=pd.DatetimeIndex(index))
    
    print(frame.index.minute)
    

提交回复
热议问题