C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Pandas Time OffsetThe 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:class pandas.tseries.offsets.DateOffset(n=1, normalize=False, **kwds) 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:
The parameters used for replacing the offset value are as follows:
Example:import pandas as pd # Create the Timestamp p = pd.Timestamp('2018-12-12 06:25:18') # Create the DateOffset do = pd.tseries.offsets.DateOffset(n = 2) # Print the Timestamp print(p) # Print the DateOffset print(do) Output: 2018-12-12 06:25:18 <2 * DateOffsets> Example2:import pandas as pd # Create the Timestamp p = pd.Timestamp('2018-12-12 06:25:18') # Create the DateOffset do = pd.tseries.offsets.DateOffset(n = 2) # Add the dateoffset to given timestamp new_timestamp = p + do # Print updated timestamp print(new_timestamp) Output: Timestamp('2018-12-14 06:25:18')
Next TopicPandas Time Periods
|