datetime to decimal hour and minutes in python3

后端 未结 1 1326
天涯浪人
天涯浪人 2021-01-18 19:37

I have a dataframe with meteorological data every 30 minutes. With my datetime index I need to create a column with timestamps

相关标签:
1条回答
  • 2021-01-18 19:49

    One way is to extract the hour and convert minutes to hours.

    There should be no need to convert to / from strings.

    import pandas as pd
    
    idx = pd.DatetimeIndex(['2016-01-01 00:30:00',
                            '2016-01-01 01:00:00',
                            '2016-01-01 01:30:00'],
                           dtype='datetime64[ns]', name='date_time', freq=None)
    
    idx.hour + idx.minute / 60
    
    # Float64Index([0.5, 1.0, 1.5], dtype='float64', name='date_time')
    
    0 讨论(0)
提交回复
热议问题