Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting Pandas DataFrame records for many years based on month & day range

I've got some daily data in a Pandas DataFrame and it has a nice index. Something like this:

import pandas as pd
import numpy as np

rng = pd.date_range('1/1/2010', periods=1000, freq='D')
ts = pd.DataFrame(randn(len(rng)), index=rng, columns=['vals'])
print ts.head()

                vals
2010-01-01  1.098302
2010-01-02 -1.384821
2010-01-03 -0.426329
2010-01-04 -0.587967
2010-01-05 -0.853374

I'd like to subset my DataFrame to only the records that fall between February 2 & March 3 for all years.

It seems there should be a very Pandas-esque way of doing this but I'm struggling to find it. Any help?

like image 590
JD Long Avatar asked Aug 31 '25 01:08

JD Long


1 Answers

I don't think there is a native way to do this (there is with between times).

But you can do it naively (this will be efficient, but is a pain to write!):

In [11]: ts[((ts.index.month == 2) & (2 <= ts.index.day)  # in Feb after the 2nd inclusive
              | (ts.index.month == 3) & (ts.index.day <= 3))]  # in March before the 3rd inclusive
Out[11]: 
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 94 entries, 2010-02-01 00:00:00 to 2012-03-03 00:00:00
Data columns (total 1 columns):
vals    94  non-null values
dtypes: float64(1)
like image 93
Andy Hayden Avatar answered Sep 02 '25 13:09

Andy Hayden