Pandas.DataFrame slicing with multiple date ranges

前端 未结 2 1145
难免孤独
难免孤独 2021-02-11 02:59

I have a datetime-indexed dataframe object with 100,000+ rows. I was wondering if there was a convenient way using pandas to get a subset of this dataframe that is within multip

2条回答
  •  日久生厌
    2021-02-11 03:36

    I feel the best option will be to use the direct checks rather than using loc function:

    df = df[((df.index >= '2016-6-27') & (df.index <= '2016-6-27 5:00')) 
        | ((df.index >= '2016-6-27 15:00') & (df.index < '2016-6-28'))]
    

    It works for me.

    Major issue with loc function with a slice is that the limits should be present in the actual values, if not this will result in KeyError.

提交回复
热议问题