user137425
user137425

Reputation: 429

While loop doesn't stop when looping through dates

Why does this while loop never stop?

t = pd.to_datetime('2016.03.04')
T = pd.to_datetime('2019.09.04')
dates = T

while dates > t:

    dates = T- pd.DateOffset(years=1)
    print(dates)

Please help

Upvotes: 3

Views: 147

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210942

I guess you want to do something like this instead of looping:

dates =  pd.date_range('2016.03.04',periods=4,freq=pd.DateOffset(years=1))
print(dates)

Output:

DatetimeIndex(['2016-03-04', '2017-03-04', '2018-03-04', '2019-03-04'], dtype='datetime64[ns]', freq='<DateOffset: kwds={'years': 1}>')

Upvotes: 2

user2209008
user2209008

Reputation:

The problem is that you're not summing the offsets.

Change this line:

dates = T - pd.DateOffset(years=1)

to this:

dates -= pd.DateOffset(years=1)

Upvotes: 2

Related Questions