Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DateOffset, step back one day

Tags:

date

pandas

I try to understand why

print(pd.Timestamp("2015-01-01") - pd.DateOffset(day=1))

does not result in

pd.Timestamp("2014-12-31")

I am using Pandas 0.18. I run within the CET timezone.

like image 561
tschm Avatar asked Apr 21 '16 08:04

tschm


3 Answers

pd.DateOffset(day=1) works (ie no error is raised) because "day" is a valid parameter, as is "days".

Look at the below one: "day" resets the actual day, "days" adds to the original day.

pd.Timestamp("2019-12-25") + pd.DateOffset(day=1)

Timestamp('2019-12-01 00:00:00')

pd.Timestamp("2019-12-25") + pd.DateOffset(days=1)

Timestamp('2019-12-26 00:00:00')

like image 155
user881111 Avatar answered Nov 20 '22 01:11

user881111


You can check pandas.tseries.offsets.DateOffset:

*kwds Temporal parameter that add to or replace the offset value.
Parameters that add to the offset (like Timedelta):

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds
  • microseconds
  • nanoseconds

Parameters that replace the offset value:

  • year
  • month
  • day
  • weekday
  • hour
  • minute
  • second
  • microsecond
  • nanosecond

print(pd.Timestamp("2015-01-01") - pd.DateOffset(days=1))
2014-12-31 00:00:00

Another solution:

print(pd.Timestamp("2015-01-01") - pd.offsets.Day(1))
2014-12-31 00:00:00

Also it is possible to subtract Timedelta:

print(pd.Timestamp("2015-01-01") - pd.Timedelta(1, unit='d'))
like image 44
jezrael Avatar answered Nov 20 '22 02:11

jezrael


Day(d) and DateOffset(days=d) do not behave exactly the same when used on timestamps with timezone information (at least on pandas 0.18.0). It looks like DateOffset add 1 day while keeping the hour information while Day adds just 24 hours of elapsed time.

>>> # 30/10/2016 02:00+02:00 is the hour before the DST change
>>> print(pd.Timestamp("2016-10-30 02:00+02:00", tz="Europe/Brussels") + pd.offsets.Day(1))
2016-10-31 01:00:00+01:00
>>> print(pd.Timestamp("2016-10-30 02:00+02:00", tz="Europe/Brussels") + pd.DateOffset(days=1))
2016-10-31 02:00:00+01:00
like image 2
sdementen Avatar answered Nov 20 '22 02:11

sdementen