Subset Pandas Dataframe Based On Annual Returning Period Covering Multiple Months
This question is similar to Selecting Pandas DataFrame records for many years based on month & day range, but both the question and answer doesn't seem to cover my case import
Solution 1:
I would create a column of (month, day)
tuples:
month_day = pd.concat([
df.index.to_series().dt.month,
df.index.to_series().dt.day
], axis=1).apply(tuple, axis=1)
You can then compare them directly:
df[(month_day >= (startMM, startdd)) & (month_day <= (endMM, enddd))]
Solution 2:
Alternative solution:
In [79]: x = df.assign(x=df.index.strftime('%m-%d')) \
.query("'02-15' <= x <= '10-03'").drop('x',1)
In [80]: x
Out[80]:
A
2010-02-15 -1.0046632010-02-160.6833522010-02-170.1585182010-02-18 -0.4474142010-02-190.078998
... ...
2012-09-221.3782532012-09-231.2158852012-09-240.2030962012-09-25 -1.6669742012-09-260.231987
[687 rows x 1 columns]
In [81]: x.index.month.unique()
Out[81]: Int64Index([2, 3, 4, 5, 6, 7, 8, 9, 10], dtype='int64')
Post a Comment for "Subset Pandas Dataframe Based On Annual Returning Period Covering Multiple Months"