BBampaa

Lessons › Python

Python lesson

Dates

Each date is now a real date, and .dt.month gives 1, 2 or 3 for January, February and March.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

pd.to_datetime turns the column into real dates. Then .dt gives you any part of each date:

df["Date"] = pd.to_datetime(df["Date"])
df["Date"].dt.month

Output:

0 1
1 1
2 1
3 2
4 2
5 2
6 3
7 3
8 3

Key points

pd.to_datetime(df["Date"])Reads text like "2025-03-03" as real dates.
df["Date"] = …Save it back into the column, or it stays text.
.dt.monthThe month of each date, 1 to 12. Also .dt.year and .dt.day.
.dt.day_name()The weekday: Monday, Tuesday and so on.

Dates first: df["Date"] = pd.to_datetime(df["Date"]). Then .dt gives the month, year or weekday.

Practice

Try each one in your head first, then open the answer.

1. Build a line that turns the Date column into real dates.
df["Date"] = pd.to_datetime(df["Date"])
2. Build a line that shows the weekday of each sale.
df["Date"].dt.day_name()
3. Turn the Date column into real dates, then show the month of each sale.
df["Date"] = pd.to_datetime(df["Date"])
df["Date"].dt.month
4. Show the weekday name of each sale.
df["Date"] = pd.to_datetime(df["Date"])
df["Date"].dt.day_name()
5. Your manager wants only March's sales. Show every sale from March (month 3).
df["Date"] = pd.to_datetime(df["Date"])
df[df["Date"].dt.month == 3]

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed