Pandas DateOffset, step back one day

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'))

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

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')

Tags:

Pandas

Date