For these examples, we will use a time-series dataset of stock prices:
Code
from pandas import read_csvrequest_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_datetimedf["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.):