Skip to content Skip to sidebar Skip to footer

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, f

Solution 1:

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)

Solution 2:

To select all records of an annual returning period covering multiple months, do as follow:

rng = pd.date_range('2010-1-1', periods=1000, freq='D')
df = pd.DataFrame(np.random.randn(len(rng)), index=rng, columns=['A'])

startMM, startdd = (2,15) # Feb 15th 
endMM, enddd = (10,3) # Oct 3rd

month_day = pd.concat([
                df.index.to_series().dt.month, 
                df.index.to_series().dt.day
            ], axis=1).apply(tuple, axis=1)

df[(month_day >= (startMM, startdd)) & (month_day <= (endMM, enddd))]

as mentioned by @IanS in https://stackoverflow.com/a/45996897/2459096


Post a Comment for "Selecting Pandas DataFrame Records For Many Years Based On Month & Day Range"