Python Pandas: drop rows of a timeserie based on time range

后端 未结 4 1345
我在风中等你
我在风中等你 2021-02-08 20:56

I have the following timeserie:

start = pd.to_datetime(\'2016-1-1\')
end = pd.to_datetime(\'2016-1-15\')
rng = pd.date_range(start, end, freq=\'2h\')
df = pd.Dat         


        
4条回答
  •  灰色年华
    2021-02-08 21:25

    Another one to try. Exclude the dates in the date_range:

    Edit: Added frequency to date_range. This is now the same as original data.

    dropThis = pd.date_range(start_remove,end_remove,freq='2h')
    df[~df.index.isin(dropThis)]
    

    We can see the rows are now dropped.

    len(df)
    169
    
    len(df[~pd.to_datetime(df.index).isin(dropThis)])
    120
    

提交回复
热议问题