marathonman4202
marathonman4202

Reputation: 37

pandas python select all data after day of year

I would like to select all data from a dataframe after a particular day of the year, every year but also within that year. For example, all the data after summer soltice, June 20 every year, through Dec. 31 every year. I thought that a mask would do it, so I tried.

import pandas as pd
import numpy.random as npr 
rng = pd.date_range('1/1/1990', periods=365*10, freq='D')
df = pd.DataFrame(npr.randn(len(rng)), index=rng)
df1 = df[(df.index.month>=6) & (df.index.day > 20)]
print df1

Of course, this selects the 21st through 30/31st of every month. I'd like to get all the data after June 20 (e.g., including July 1) through Dec. 31.

Upvotes: 1

Views: 864

Answers (1)

Def_Os
Def_Os

Reputation: 5467

You could use separate conditions for June and for everything after June, and combine them like this:

df2 = df[((df.index.month == 6) & (df.index.day > 20)) | (df.index.month > 6)]

Upvotes: 2

Related Questions