Arithmetic Operations with Timedelta and DateOffset
Learn how to use the Timedelta and DateOffset functions to perform arithmetic operations.
Introduction
Part of working with time series data is applying math operations to it. Therefore, it’s important for us to understand the pandas
capabilities that allow for precise and flexible date-time arithmetics. In particular, we’ll recap the Timedelta
class before diving into DateOffset
objects.
Basic operations with Timedelta
As a recap, the Timedelta
class in pandas
represents a difference between two dates or times, expressed in standard date time units (e.g., days, hours, and minutes), and can be positive or negative. The Timedelta
is a powerful tool for working with time series data, because it allows us to perform arithmetic operations with time-based data. These arithmetic operations include adding or subtracting time intervals and calculating differences between dates.
Suppose we’re working with a DatetimeIndex
with seconds precision, as illustrated below:
Get hands-on with 1200+ tech skills courses.