Run pandas Online

Work with Dates in pandas

Turn text into real dates, then get the weekday, filter by month or count days between dates.

  • Intermediate
  • 2 min read
  • 1 runnable example
What you'll learn
  • pd.to_datetime
  • The .dt accessor: day_name, month, year
  • Date arithmetic with Timedelta
"2026-08-10"
  1. 1pd.to_datetime(...)A real date
  2. 2.dt.day_name()Monday
  3. 3.dt.month8

Text → datetime → useful parts

The problem

Dates usually arrive as text, such as "2026-08-10". Text cannot tell you the weekday, sort by month or measure the days between two events. Converting to real dates unlocks all of that.

Think of it like this

Converting text to dates is like turning handwritten notes into calendar entries. Once an entry is on the calendar, the calendar knows its weekday, its month and how far it is from any other entry.

From handwritten notes to a calendar
"2026-08-10"📝 A date written as text
pd.to_datetime(...)📅 Put it on the calendar
.dt.day_name()🗓️ Ask the calendar for the weekday
Monday✅ The answer for every row at once
Real dates know their weekday, month and distance to other dates.
In one sentence

pd.to_datetime converts text or numbers into datetime values, and the .dt accessor gives date parts such as the weekday, month or year for a whole column.

Key points

  1. pd.to_datetime(events["date"]) turns the text dates into datetime values.
  2. Assigning the result back to events["date"] replaces the text column.
  3. .dt.day_name() returns the weekday for every row: Monday and Wednesday.
  4. The new day column is added to the same table.

When should you use it?

  • Reports by day, week or month.
  • Calculating durations and deadlines.
  • Filtering a time range, such as the last 30 days.

When not to use it

  • Dates in an unusual or ambiguous format, such as 03/04/2026: pass format= or dayfirst=True.
  • A single date in plain Python code: the datetime module is enough.

Example code

Run code →
main.py
import pandas as pd

events = pd.DataFrame({"date": ["2026-08-10", "2026-08-12"]})
events["date"] = pd.to_datetime(events["date"])
events["day"] = events["date"].dt.day_name()

print(events)
Get a Run button for this example

Result of running the code

date        day
0 2026-08-10     Monday
1 2026-08-12  Wednesday

Code explanation

To_datetime converts text to datetime values. The dt accessor exposes vectorized date components such as day names.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Add rows, columns, and missing values.
  2. Combine filtering, sorting, and groupby into a small report.
  3. Load a real file, inspect dtypes, and handle invalid data.
Run in the Python compiler →

Common mistakes

Using .dt on text

The .dt accessor only works on datetime columns.

Problem
events["date"].dt.day_name()  # AttributeError on text
Fix
pd.to_datetime(events["date"]).dt.day_name()

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges