5 Examples to Learn Date and Time Manipulation with Python Pandas | by Soner Yıldırım | Towards Data Science

We often deal with dates and times in data science. If you are working with time-series data, they are always a part of your work.
Pandas is highly efficient and practical with regard to manipulating dates and times. In this article, we will go over 5 examples for solving some of the common date and time manipulation operations.
Don’t forget to subscribe if you’d like to get an email whenever I publish a new article.
Let’s start by creating a sample DataFrame that contains dates.
import pandas as pddf = pd.DataFrame({    "Date": pd.date_range(start="2021-12-28", periods=5, freq="D"),    "Measurement": [1, 10, 25, 7, 12]})
The DateOffset function can be used for adding a specific length of duration to dates.
Let’s create a new column by adding 6 months to the existing date column.
df["Date2"] = df["Date"] + pd.DateOffset(months=6)
The following line of code adds 1 year to the existing date column.
df["Date2"] = df["Date"] + pd.DateOffset(years=1)
It is important to note that the data type of values in the date column is datetime. Thus, we can also add time intervals.
df["Date"] + pd.DateOffset(hours=2)# output0   2021-12-28 02:00:001   2021-12-29 02:00:002   2021-12-30 02:00:003   2021-12-31 02:00:004   2022-01-01 02:00:00Name: Date, dtype: datetime64[ns]
If you need to do subtraction, just change the “+” sign to “-” or use a negative value inside the function.
We can use the Timedelta function for adding dates and times as well. Its syntax is slightly different than the syntax of the DateOffset function.
df["Date"] + pd.Timedelta(3, unit="day")# output0   2021-12-311   2022-01-012   2022-01-023   2022-01-034   2022-01-04Name: Date, dtype: datetime64[ns]
The Timedelta function also accepts strings for specifying the duration to be added. The following line of code does the same operation as above.
df["Date"] + pd.Timedelta("3 days")
A datetime object contains several pieces of information such as year, month, day, week, hour, microsecond, and so on.
We sometimes need a particular piece of information. For instance, we can extract month from a date as below:
df["Date"].dt.month# output0    121    122    123    124     1Name: Date, dtype: int64
The year and day methods return the year and day part of a date, respectively.
In retail analytics, the day of the week is a significant piece of information for analysis and modeling. The dayofweek method can be used to get this information from a date.
df["Date"].dt.dayofweek# output0    11    22    33    44    5Name: Date, dtype: int64
All these methods are available under the dt accessor so make sure to write “dt” before the name of the method.
The isocalendar function returns a DataFrame with year, week, and day of week. Thus, it is a quick way of extracting multiple pieces of information in a single operation.
For instance, we can add the year, week, and day of week columns to our initial DataFrame as below:
df = pd.concat([df, df["Date"].dt.isocalendar()], axis=1)
The part that creates the additional columns is:
df["Date"].dt.isocalendar()
We use the concat function to combine these columns with the original DataFrame.
The difference between two dates or times can be of great importance in some tasks. For instance, we might need to calculate the time between consecutive measurements in a process.
The subtraction operation with two datetime objects gives us the difference in days.
df["Diff"] = df["Date2"] - df["Date"]
The data type of the diff column is timedelta so we can get the number of days using the days method.
df["Diff"].dt.days# output0    3651    3652    3653    3654    365Name: Diff, dtype: int64
We can also divide it by a timedelta object of 1 day to get the number of days.
df["Diff"] / pd.Timedelta(days=1)# output0    365.01    365.02    365.03    365.04    365.0Name: Diff, dtype: float64
If you want to convert the difference to months or years, use the timedelta of NumPy because Pandas cannot construct a Timedelta from months or years.
import numpy as npdf["Diff"] / np.timedelta64(1, 'M')# output0    11.9920331    11.9920332    11.9920333    11.9920334    11.992033Name: Diff, dtype: float64
We have learned Pandas functions and methods for solving some of the common tasks in date and time manipulation.
Don’t forget to subscribe if you’d like to get an email whenever I publish a new article.
You can become a Medium member to unlock full access to my writing, plus the rest of Medium. If you do so using the following link, I will receive a portion of your membership fee at no additional cost to you.
sonery.medium.com
Thank you for reading. Please let me know if you have any feedback.