YouTip LogoYouTip

Pandas Dateoffset Object

The Pandas DateOffset object is a tool used for performing time offsets when working with date and time data. Here are some key features and usage examples of DateOffset: ### Basic Concepts The DateOffset object is similar to Timedelta, but it follows calendar-based date and time rules rather than performing straightforward arithmetic calculations. This means that DateOffset takes into account actual calendar daysβ€”for example, when dealing with dates spanning months or years, it correctly calculates the difference in days. In contrast, Timedelta simply adds the specified number of days without considering changes in months or years. ### Usage Methods DateOffset can be used to perform date offset operations through arithmetic operators (such as +) or the apply method. For example, you can use DateOffset to add months or hours like this: ## Example ts = pd.Timestamp('2017-01-01 09:10:11') ts + DateOffset(months=3) # Add three months ts + DateOffset(hours=2) # Add two hours If no parameters are specified, DateOffset() defaults to adding one calendar day: ts + DateOffset() # Default to adding one day ### Supported Parameters DateOffset supports various parameters that allow you to specify the time units to add or replace, such as years, months, weeks, days, hours, minutes, seconds, and more. These parameters can either add to the offset value (e.g., years, months, weeks, days, hours, minutes, seconds) or replace the offset value itself (e.g., year, month, day, weekday, hour, minute, second). ### Special Methods DateOffset also provides rollforward() and rollback() methods, which allow you to roll a date forward or backward to the nearest valid date. For example, if you use a business day offset (BDay), it will skip weekends and directly roll to the next business day. ## Example friday = pd.Timestamp('2022-01-05') two_business_days = 2 * pd.offsets.BDay() friday + two_business_days # Adding two business days skips weekends ### Frequency Strings DateOffset supports frequency strings or offset aliases, which can be passed as the freq parameter. These frequency strings represent DateOffset objects and their subclasses, defining the frequency of date-time indexes. DateOffset is a very useful tool in Pandas for handling time series data, making date and time offset operations more consistent with real-world calendar rules. * * * ## Common Functions ### **DateOffset Constructor** | **Class/Method** | **Description** | | --- | --- | | `pd.DateOffset(**kwargs)` | Creates a DateOffset object, supporting custom time offsets. | * * * ### **Common DateOffset Subclasses** | **Class** | **Description** | | --- | --- | | `pd.offsets.Day()` | Represents a daily offset. | | `pd.offsets.BDay()` | Represents a business day offset (excluding weekends). | | `pd.offsets.Hour()` | Represents an hourly offset. | | `pd.offsets.Minute()` | Represents a minute offset. | | `pd.offsets.Second()` | Represents a second offset. | | `pd.offsets.Milli()` | Represents a millisecond offset. | | `pd.offsets.Micro()` | Represents a microsecond offset. | | `pd.offsets.MonthEnd()` | Represents a monthly end offset. | | `pd.offsets.MonthBegin()` | Represents a monthly beginning offset. | | `pd.offsets.YearEnd()` | Represents a yearly end offset. | | `pd.offsets.YearBegin()` | Represents a yearly beginning offset. | | `pd.offsets.QuarterEnd()` | Represents a quarterly end offset. | | `pd.offsets.QuarterBegin()` | Represents a quarterly beginning offset. | | `pd.offsets.Week()` | Represents a weekly offset. | | `pd.offsets.WeekOfMonth()` | Represents the nth week of a month offset. | * * * ### **DateOffset Attributes** | **Attribute** | **Description** | | --- | --- | | `DateOffset.name` | Returns the name of the DateOffset. | | `DateOffset.n` | Returns or sets the number of offsets. | | `DateOffset.normalize` | Returns or sets whether the time is normalized to midnight. | * * * ### **DateOffset Methods** | **Method** | **Description** | | --- | --- | | `DateOffset.apply(other)` | Applies the offset to another datetime object. | | `DateOffset.rollforward(other)` | Rolls the date forward to the next offset. | | `DateOffset.rollback(other)` | Rolls the date backward to the previous offset. | | `DateOffset.is_anchored()` | Checks whether the offset is anchored (i.e., whether the frequency is fixed). | | `DateOffset.onOffset(date)` | Checks whether a date aligns with the offset. | * * * ### Example ## Example import pandas as pd from pandas.tseries.offsets import Day, BDay, MonthEnd # Create a DateOffset offset = Day(3) print(offset) # Output # Apply DateOffset date = pd.Timestamp('2023-01-01') new_date = date + offset print(new_date) # Output 2023-01-04 # Use common subclasses bday_offset = BDay(2) new_bdate = date + bday_offset print(new_bdate) # Output 2023-01-05 (skips weekends) # Monthly end offset month_end_offset = MonthEnd() new_month_end = date + month_end_offset print(new_month_end) # Output 2023-01-31 * * * ### **Detailed Parameter Explanation** #### **`pd.DateOffset()`** | **Parameter** | **Description** | | --- | --- | | `years` | Number of years offset. | | `months` | Number of months offset. | | `weeks` | Number of weeks offset. | | `days` | Number of days offset. | | `hours` | Number of hours offset. | | `minutes` | Number of minutes offset. | | `seconds` | Number of seconds offset. | | `milliseconds` | Number of milliseconds offset. | | `microseconds` | Number of microseconds offset. | | `nanoseconds` | Number of nanoseconds offset. | #### **`pd.offsets.BDay()`** | **Parameter** | **Description** | | --- | --- | | `n` | Number of offsets, default is 1. | | `normalize` | Whether to normalize the time to midnight, default is `False`. | #### **`pd.offsets.MonthEnd()`** | **Parameter** | **Description** | | --- | --- | | `n` | Number of offsets, default is 1. | | `normalize` | Whether to normalize the time to midnight, default is `False`. | * * * For more detailed information, refer to the (https://pandas.pydata.org/docs/reference/offset_frequency.html).
← Pytorch Torch Nn RefPandas Arrays Scalars Data Typ β†’