Remove leap year from pandas dataframe

I have 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 delete all the lines corresponding to February 29, the result is 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 

Now I just do it manually:

df.drop(df.index[['2012-02-29']]) . How can I make it so that it works for many years without manually specifying the row index.

+6
source share
3 answers

IIUC you can mask it and delete it with loc :

 def is_leap_and_29Feb(s): return (s.index.year % 4 == 0) & ((s.index.year % 100 != 0) | (s.index.year % 400 == 0)) & (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 
+4
source

If your framework already has a datetime column as an index, you can:

 df = df[~((df.index.month == 2) & (df.index.day == 29))] 

this should remove lines containing the day of February 29th for all years.

+10
source

You can see the date as a 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 comparison method, for example, contains , etc.

+4
source

All Articles