Python Central HubSign in

Data Analytics with Python / Phase 3: Data Manipulation with Pandas

Working with Dates and Times (to_datetime, dt accessor)

Parse, clean, and analyze date/time columns using to_datetime, dt accessor, and basic time series grouping.

Why datetime is a core analytics skill

Dates show up everywhere:

  • Orders, sessions, support tickets
  • Time-based KPIs (DAU/WAU/MAU)
  • Trend analysis and seasonality

In real datasets, datetime values often start as strings, so you must parse them correctly.

Example dataset

Python
import pandas as pd
 
df = pd.DataFrame({
    "order_id": [101, 102, 103, 104],
    "order_time": [
        "2025-01-01 10:15:00",
        "2025-01-01 10:45:00",
        "2025-01-02 09:10:00",
        "2025-01-03 18:30:00",
    ],
    "amount": [250, 180, 90, 300],
})
 
print(df.dtypes)

Parse strings into datetime: to_datetime

Python
df["order_time"] = pd.to_datetime(df["order_time"], errors="coerce")
print(df.dtypes)
  • errors="coerce" converts invalid timestamps to NaT (missing datetime).

Extract parts of datetime with .dt

Python
df["date"] = df["order_time"].dt.date
 
df["year"] = df["order_time"].dt.year
df["month"] = df["order_time"].dt.month
df["day"] = df["order_time"].dt.day
df["hour"] = df["order_time"].dt.hour
 
df["weekday"] = df["order_time"].dt.day_name()
 
print(df)

Group by date (daily totals)

Python
daily = (
    df.groupby(df["order_time"].dt.date)
    .agg(total_amount=("amount", "sum"), orders=("order_id", "count"))
)
 
print(daily)

Timezone quick note

If you work with timezones:

  • Store timestamps in UTC when possible
  • Convert for display/reporting
Python
# df["order_time"] = df["order_time"].dt.tz_localize("UTC")
# df["order_time_ist"] = df["order_time"].dt.tz_convert("Asia/Kolkata")

Common pitfalls

  • Don’t compare datetime strings if you can parse them.
  • Mixed formats can create parsing issues — use errors="coerce" and investigate invalid rows.
  • Missing datetimes are stored as NaT (not NaN).

Visualize it

diagram From string to datetime insight mermaid
Raw date strings are parsed once with to_datetime, then the dt accessor and groupby unlock time-based analysis.

🧪 Try It Yourself

Exercise 1 – Parse Strings Into Datetime

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Extract the Weekday Name

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Handle Unparseable Dates Safely

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Check your understanding

quizCheck yourself
  1. What does pd.to_datetime(col, errors="coerce") do with an invalid timestamp?

    Show answer

    A — Converts it to NaT — NaT is the missing value for datetimes. Investigate those rows afterwards.

  2. Which expression gives the weekday name, such as Wednesday, from a datetime column?

    Show answer

    A — df["order_time"].dt.day_name() — The dt accessor exposes the parts of a datetime. dt.day gives the day of the month as a number.

  3. In the orders data, 2025-01-01 has orders of 250 and 180. What do the daily totals show for that date?

    Show answer

    A — total_amount 430 and 2 orders — Grouping by the date sums 250 + 180 = 430 and counts 2 orders.

  4. What is a missing datetime value called?

    Show answer

    A — NaT — Missing datetimes are stored as NaT, not NaN.

Next

Once dates are parsed, you'll often need to build your own calendars from scratch — continue to Date Ranges, Frequencies and Shifting.

Was this page helpful?

Let us know how we did