delete leap days in pandas
Use dt
accessor:
df = df[~((df.Date.dt.month == 2) & (df.Date.dt.day == 29))]
Add dt
accessor because working with Series
, not with DatetimeIndex
:
df = df[~((df.Date.dt.month == 2) & (df.Date.dt.day == 29))]
Or invert condition with chaining |
for bitwise OR
and !=
for not equal:
df = df[(df.Date.dt.month != 2) | (df.Date.dt.day != 29)]
Or use strftime
for convert to MM-DD
format:
df = df[df.Date.dt.strftime('%m-%m') != '02-29']
Another way you can try below in incase your Date
column is not proper datetime
rather a str.
df[~df.Date.str.endswith('02-29')]
OR , if it's in datetime
format even you can try converting to str
.
df[~df.Date.astype(str).str.endswith('02-29')]
OR, Even use contains:
df[~df.Date.str.contains('02-29')]