Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Previous month datetime pandas

I have a datetime instance declared as follows:

dtDate = datetime.datetime(2016,1,1,0,0)

How do I get the previous month and previous year from dtDate?

e.g. something like:

dtDate.minusOneMonth()
# to return datetime.datetime(2015,12,1,0,0)
like image 878
JohnAndrews Avatar asked Mar 29 '17 10:03

JohnAndrews


1 Answers

You can use:

dtDate = datetime.datetime(2016,1,1,0,0)

print (dtDate - pd.DateOffset(months=1))
2015-12-01 00:00:00

print (dtDate - pd.DateOffset(years=1))
2015-01-01 00:00:00

Add s is important, because if use year only:

print (dtDate - pd.DateOffset(year=1))
0001-01-01 00:00:00 
like image 155
jezrael Avatar answered Oct 02 '22 22:10

jezrael