Dates and Times
to_datetime parses text into datetimes. The .dt accessor gives year, month, weekday and more.
Example
import pandas as pd
import numpy as np
d = pd.to_datetime(pd.Series(['2024-01-15', '2024-03-02', '2024-12-31']))
print(d.dt.year.tolist(), d.dt.month.tolist())
print(d.dt.day_name().tolist())
print((d.iloc[1] - d.iloc[0]).days)Result
[2024, 2024, 2024] [1, 3, 12] ['Monday', 'Saturday', 'Tuesday'] 47
Exercise
Parse dates.
pd.("2024-05-01")
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.