Remove leap year day from pandas dataframe

Question:

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.

Asked By: user308827

||

Answers:

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)

Answered By: jezrael

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.

Answered By: Fabio Lamanna

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.

Answered By: Markus Weninger
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.