Working with Dates & Times
A datetime in pandas is a special value that stores a precise moment in time, and pandas gives you tools to parse text into datetimes, extract their parts with the .dt accessor, filter by date ranges, and group rows into time buckets.
Learn Working with Dates & Times in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Learn to turn date strings into real dates, pull out the year or weekday, set a DatetimeIndex, resample into monthly totals, and do arithmetic with time spans.
Dates often arrive as plain text. pd.to_datetime() converts a string column into a true datetime64 Series that pandas understands. When reading a CSV you can parse in one step by passing parse_dates=["col"] to pd.read_csv .
Once a column is a datetime, the .dt accessor unlocks its parts: .dt.year , .dt.month , .dt.day , .dt.dayofweek (Monday is 0), and .dt.day_name() for the weekday name. It mirrors the .str accessor you used for text.
Because datetimes compare in time order, you can filter a range with normal comparisons. Setting the date column as the index creates a DatetimeIndex , which then lets you resample("M").sum() to roll the data up into monthly totals. Add or subtract spans of time with pd.Timedelta .
Replace the blank so you extract the year from each date. The expected output is the list [2021, 2022, 2023].
The .dt accessor only works after conversion. Call pd.to_datetime first.
Set the date column as the index before resampling.
Parse a date column, label each day, and total only the weekend sales.
Lesson complete — time is on your side!
You can parse strings into datetimes, extract any part with .dt, filter by date ranges, set a DatetimeIndex, resample into monthly totals, and add time spans with Timedelta. These skills unlock every time-series analysis.
🚀 Up next: GroupBy Split-Apply-Combine — summarise data by category.
Practice quiz
Which function converts a column of date strings into datetime64 values?
- pd.parse()
- pd.date()
- pd.to_datetime()
- pd.strptime()
Answer: pd.to_datetime(). pd.to_datetime() parses common date formats and returns a datetime64 Series.
How can you parse dates while reading a CSV in one step?
- col
Answer: col. parse_dates=['col'] in pd.read_csv converts those columns during loading.
What does the .dt accessor give you?
- String methods
- Category codes
- Plotting tools
- The parts of a datetime, like .dt.year and .dt.month
Answer: The parts of a datetime, like .dt.year and .dt.month. .dt exposes datetime parts; it is the datetime equivalent of the .str accessor.
In .dt.dayofweek, what integer represents Monday?
- 1
- 0
- 7
- 6
Answer: 0. dayofweek numbers weekdays 0-6 with Monday as 0 and Sunday as 6.
For 2024-01-05 (a Friday), what does .dt.dayofweek return?
- 4
- 0
- 5
- 1
Answer: 4. With Monday=0, Friday is 4.
What does .dt.day_name() return for 2024-12-25?
- 'December'
- '25'
- 'Wednesday'
- 'Christmas'
Answer: 'Wednesday'. 2024-12-25 falls on a Wednesday, so day_name() returns 'Wednesday'.
What does resample('M').sum() do on a DatetimeIndex?
- Sorts by date
- Rolls the data up into monthly totals
- Drops missing dates
- Converts to strings
Answer: Rolls the data up into monthly totals. resample groups time-series rows into calendar buckets; 'M' with sum gives monthly totals.
What must be true before you can call resample on a DataFrame?
- The values must be integers
- There must be no NaT
- The frame must be sorted
- The index must be a DatetimeIndex
Answer: The index must be a DatetimeIndex. resample needs a DatetimeIndex, so set the date column as the index first.
Why does df['date'].dt.month fail on a plain text column?
- month is not a real attribute
- The .dt accessor only works after pd.to_datetime conversion
- It needs parentheses
- Text columns have no months
Answer: The .dt accessor only works after pd.to_datetime conversion. You must convert to datetime first; .dt only works on datetime columns.
How do you add 30 days to every date in a datetime Series?
- series + 30
- series.add_days(30)
- series + pd.Timedelta(days=30)
- series.dt + 30
Answer: series + pd.Timedelta(days=30). Add a pd.Timedelta(days=30) to shift each datetime by that span.