I have the foll. dataframe:
datetime
2012-01-01 125.5010
2012-01-02 125.5010
2012-01-03 125.5010
2012-02-04 125.5010
2012-02-05 125.5010
2012-02-29 125.5010
2012-02-28 125.5010
2016-01-07 125.5010
2016-01-08 125.5010
2016-02-29 81.6237
I would like to drop all rows corresponding to Feb 29th, resulting in foll. data frame:
datetime
2012-01-01 125.5010
2012-01-02 125.5010
2012-01-03 125.5010
2012-02-04 125.5010
2012-02-05 125.5010
2012-02-28 125.5010
2016-01-07 125.5010
2016-01-08 125.5010
Right now, I am just doing it manually:
df.drop(df.index[['2012-02-29']])
. How can I make it so that it works for all years, without haveing to manually specify row index.
If your dataframe has already the datetime
column as index you can:
df = df[~((df.index.month == 2) & (df.index.day == 29))]
this should remove the rows containing the day February 29th for all years.
You can mask it and remove boolean indexing
:
df = df[(df.index.month != 2) | (df.index.day != 29)]
Solution with function:
def is_leap_and_29Feb(s):
return (s.index.month != 2) | (s.index.day != 29)
mask = is_leap_and_29Feb(df)
print mask
#[False False False False False True False False False True]
print df.loc[~mask]
# datetime
#2012-01-01 125.501
#2012-01-02 125.501
#2012-01-03 125.501
#2012-02-04 125.501
#2012-02-05 125.501
#2012-02-28 125.501
#2016-01-07 125.501
#2016-01-08 125.501
Or:
(s.index.month != 2) | (s.index.day != 29)
You can see the date as string
and see if it ends with 02-29
:
df = df[~df.index.str.endswith('02-29')]
Using this method, you can use any string-comparism method like contains
, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With