python pandas dataframe slicing by date conditions

前端 未结 4 880
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 18:27

I am able to read and slice pandas dataframe using python datetime objects, however I am forced to use only existing dates in index. For example, this works:

4条回答
  •  没有蜡笔的小新
    2020-11-29 18:58

    You can use a simple mask to accomplish this:

    date_mask = (data.index > start) & (data.index < end)
    dates = data.index[date_mask]
    data.ix[dates]
    

    By the way, this works for hierarchical indexing as well. In that case data.index would be replaced with data.index.levels[0] or similar.

提交回复
热议问题