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
.................
.................
5
  • Does 15 represent the day or the year? Commented Oct 18, 2016 at 12:05
  • 15 is day not year Commented Oct 18, 2016 at 12:07
  • So you want the data from March 15th to May 15th for all years, not a single year? Commented Oct 18, 2016 at 12:08
  • exactly . I am looking for data from march 15 to may 15 for all years Commented Oct 18, 2016 at 12:09
  • 1
    refer stackoverflow.com/a/16176457/6633975 Commented Oct 18, 2016 at 12:24

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.