Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove leap year day from pandas dataframe

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.

like image 387
user308827 Avatar asked Jan 23 '16 17:01

user308827


3 Answers

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.

like image 52
Fabio Lamanna Avatar answered Nov 11 '22 17:11

Fabio Lamanna


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)

like image 21
jezrael Avatar answered Nov 11 '22 18:11

jezrael


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.

like image 7
Markus Weninger Avatar answered Nov 11 '22 17:11

Markus Weninger