Reputation: 14256
I have a DataFrame indexed by date. I would like to be able to Null out all rows where the index is greater than some value (like today) but keep them in the DataFrame. What's the best way to do this? For instance this
10/20/16 15, 20
10/25/16 13, 12
10/30/16 16, 15
#--> 10/30/16 should go to NaN, NaN
Upvotes: 4
Views: 3308
Reputation: 862611
Solution with DataFrame.mask
, for mask
is necessary same index
as df
:
#convert index to datetime
df.index = pd.to_datetime(df.index)
mask = pd.Series(df.index > pd.datetime.today(), index=df.index)
print (mask)
Date
2016-10-20 False
2016-10-25 False
2016-10-30 True
dtype: bool
df = df.mask(mask)
print (df)
a b
Date
2016-10-20 15.0 20.0
2016-10-25 13.0 12.0
2016-10-30 NaN NaN
Upvotes: 6