Home » Pandas Time Offset

Pandas Time Offset

The time series tools are most useful for data science applications and deals with other packages used in Python. The time offset performs various operations on time, i.e., adding and subtracting.

The offset specifies a set of dates that conform to the DateOffset. We can create the DateOffsets to move the dates forward to valid dates.

If the date is not valid, we can use the rollback and rollforward methods for rolling the date to its nearest valid date before or after the date. The pseudo-code of time offsets are as follows:

Syntax:

def __add__(date):

date = rollback(date). It returns nothing if the date is valid + <n number of periods>.

date = rollforward(date)

When we create a date offset for a negative number of periods, the date will be rolling forward.

Parameters:

n: Refers to int, default value is 1.

It is the number of time periods that represents the offsets.

normalize: Refers to a boolean value, default value False.

**kwds

It is an optional parameter that adds or replaces the offset value.

The parameters used for adding to the offset are as follows:

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

The parameters used for replacing the offset value are as follows:

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

Example:

Output:

2018-12-12 06:25:18  <2 * DateOffsets>  

Example2:

Output:

Timestamp('2018-12-14 06:25:18')  

You may also like