Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a date from pandas date_range

So I have a pandas date_range like so

dates = pd.date_range(start='2005-1-1', end='2014-12-31', freq='D')

I want to remove all the extra days resulting from leap years.

I do a for loop

for each in index:
    if each.month==2 and each.day==29:
        print(each) # I actually want to delete this item from dates

But my problem is that I don't know how to delete the item. The regular python list methods and functions doesn't work. I've looked everywhere on SO. I've looked at the documentation for pandas.date_range but found nothing

Any help will be appreciated.

like image 371
chidimo Avatar asked Dec 24 '22 17:12

chidimo


1 Answers

You probably want to use drop to remove the rows.

import pandas as pd
dates = pd.date_range(start='2005-1-1', end='2014-12-31', freq='D')

leap = []
for each in dates:
    if each.month==2 and each.day ==29:
        leap.append(each)

dates = dates.drop(leap)
like image 82
nitin Avatar answered Dec 26 '22 05:12

nitin