How to subset pandas time series by time of day

后端 未结 1 1647
余生分开走
余生分开走 2020-12-19 00:37

I am trying to subset a pandas time series that spans multiple days by time of day. E.g., I only want times between 12:00 and 13:00.

I know how to do this for a spec

相关标签:
1条回答
  • 2020-12-19 01:25

    Assuming the index is a valid pandas timestamp, the following will work:

    test.index.hour returns an array containing the hours for each row in your dataframe. Ex:

    df = pd.DataFrame(randn(100000,1),columns=['A'],index=pd.date_range('20130101',periods=100000,freq='T'))
    

    df.index.year returns array([2013, 2013, 2013, ..., 2013, 2013, 2013])

    To grab all rows where the time is between 12 and 1, use

    df.between_time('12:00','13:00')
    

    This will grab that timeframe over several days/years etc. If the index is not a valid timestamp, convert it to a valid timestamp using pd.to_datetime()

    0 讨论(0)
提交回复
热议问题