extract hour from timestamp with python

前端 未结 2 1747
萌比男神i
萌比男神i 2021-01-12 01:38

I have a dataframe df_energy2

df_energy2.info()

RangeIndex: 29974 entries, 0 to 29973
Data columns (total 4 co         


        
2条回答
  •  逝去的感伤
    2021-01-12 01:58

    Given your data:

    df_energy2.head()
    
    TIMESTAMP P_ACT_KW PERIODE_TARIF P_SOUSCR
    2016-01-01 00:00:00 116 HC 250 
    2016-01-01 00:10:00 121 HC 250
    

    You have timestamp as the index. For extracting hours from timestamp where you have it as the index of the dataframe:

      hours = df_energy2.index.hour
    

    Edit: Yes, jezrael you're right. Putting what he has stated: pandas dataframe has a property for this i.e. dt :

    ..dt.hour
    

    Example in your context - the column with date is TIMESTAMP

    df.TIMESTAMP.dt.hour
    

    A similar question - Pandas, dataframe with a datetime64 column, querying by hour

提交回复
热议问题