1

I have time a series datasets. I can select data from march to may by this code:

df[(df.index.month >=3) & (df.index.month<=5)]

But the problem is how to select the data from march-15 to may-15? Any help will be highly appreciated.

and my dataframe looks like:

2000-02-25   0.01
2000-02-26   0.03
2000-02-27   1.0
2000-02-28   1.52
2000-02-29   0.23
2000-03-01   0.45
2000-03-05   2.15
2000-03-06   1.75
.................
.................
bikuser
  • 2,013
  • 4
  • 33
  • 57

1 Answers1

2

You can use helper Series s where all years are replaced to same - e.g. 2000:

print (df)
               A
2001-02-25  0.01
2002-02-26  0.03
2003-02-27  1.00
2004-02-28  1.52
2005-03-29  0.23
2006-03-01  0.45
2007-03-05  2.15
2008-03-06  1.75

s = pd.Series(df.index.map(lambda x: pd.datetime(2000, x.month, x.day)))

mask = (s.dt.date > pd.datetime(2000,3,15).date()) & 
       (s.dt.date < pd.datetime(2000,5,15).date())
mask.index = df.index
print (mask)
2001-02-25    False
2002-02-26    False
2003-02-27    False
2004-02-28    False
2005-03-29     True
2006-03-01    False
2007-03-05    False
2008-03-06    False
dtype: bool

df = df[mask]
print (df)
               A
2005-03-29  0.23
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252