10  Dates and Times in pandas

For these examples, we will use a time-series dataset of stock prices:

Code
from pandas import read_csv

request_url = "https://raw.githubusercontent.com/prof-rossetti/intro-to-python/main/data/daily_adjusted_nflx.csv"
df = read_csv(request_url)
df.drop(columns=["split_coefficient", "dividend_amount"], inplace=True)
df.head()
timestamp open high low close adjusted_close volume
0 2021-10-18 632.1000 638.4100 620.5901 637.97 637.97 4669071
1 2021-10-15 638.0000 639.4200 625.1600 628.29 628.29 4116874
2 2021-10-14 632.2300 636.8800 626.7900 633.80 633.80 2672535
3 2021-10-13 632.1791 632.1791 622.1000 629.76 629.76 2424638
4 2021-10-12 633.0200 637.6550 621.9900 624.94 624.94 3227349

10.1 Converting to Datetime

When we are working with a column of strings representing date and/or time values, it is helpful to use the to_datetime function to convert these string values into datetime-aware values that respect datetime operations.

Here we see the original values in the “timestamp” column are date strings:

type(df["timestamp"][0])
str

After converting this column of string values to datetime-aware values, by passing that column to the to_datetime function, we see the resulting values become datetime-aware Timestamp objects:

from pandas import to_datetime

df["timestamp"] = to_datetime(df["timestamp"])

type(df["timestamp"][0])
pandas._libs.tslibs.timestamps.Timestamp

10.2 Datetime Operations

When working with Timestamp objects, we can easily convert and transform them to various component date parts (such as just the year, just the month, etc.):

df["year"] = df["timestamp"].dt.year
df["month"] = df["timestamp"].dt.month
df["weekday"] = df["timestamp"].dt.weekday
df["day_name"] = df["timestamp"].dt.strftime("%A") # DAY NAME

df[["timestamp", "year", "month", "weekday", "day_name"]]
timestamp year month weekday day_name
0 2021-10-18 2021 10 0 Monday
1 2021-10-15 2021 10 4 Friday
2 2021-10-14 2021 10 3 Thursday
3 2021-10-13 2021 10 2 Wednesday
4 2021-10-12 2021 10 1 Tuesday
... ... ... ... ... ...
95 2021-06-03 2021 6 3 Thursday
96 2021-06-02 2021 6 2 Wednesday
97 2021-06-01 2021 6 1 Tuesday
98 2021-05-28 2021 5 4 Friday
99 2021-05-27 2021 5 3 Thursday

100 rows × 5 columns

For more information about date and time conversions, see the following documentation links: