python pandas dataframe slicing by date conditions

前端 未结 4 850
佛祖请我去吃肉
佛祖请我去吃肉 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 19:08

    Short answer: Sort your data (data.sort()) and then I think everything will work the way you are expecting.

    Yes, you can slice using datetimes not present in the DataFrame. For example:

    In [12]: df
    Out[12]: 
                       0
    2013-04-20  1.120024
    2013-04-21 -0.721101
    2013-04-22  0.379392
    2013-04-23  0.924535
    2013-04-24  0.531902
    2013-04-25 -0.957936
    
    In [13]: df['20130419':'20130422']
    Out[13]: 
                       0
    2013-04-20  1.120024
    2013-04-21 -0.721101
    2013-04-22  0.379392
    

    As you can see, you don't even have to build datetime objects; strings work.

    Because the datetimes in your index are not sequential, the behavior is weird. If we shuffle the index of my example here...

    In [17]: df
    Out[17]: 
                       0
    2013-04-22  1.120024
    2013-04-20 -0.721101
    2013-04-24  0.379392
    2013-04-23  0.924535
    2013-04-21  0.531902
    2013-04-25 -0.957936
    

    ...and take the same slice, we get a different result. It returns the first element inside the range and stops at the first element outside the range.

    In [18]: df['20130419':'20130422']
    Out[18]: 
                       0
    2013-04-22  1.120024
    2013-04-20 -0.721101
    2013-04-24  0.379392
    

    This is probably not useful behavior. If you want to select ranges of dates, would it make sense to sort it by date first?

    df.sort_index()
    

提交回复
热议问题