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
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
df["order_time"] = pd.to_datetime(df["order_time"], errors="coerce")
print(df.dtypes)errors="coerce"converts invalid timestamps toNaT(missing datetime).
Extract parts of datetime with .dt
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)
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
# 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(notNaN).
Visualize it
flowchart LR A["Date strings"] --> B["pd.to_datetime()"] B --> C["datetime64 column"] C --> D[".dt.year / .dt.month / .dt.day_name()"] C --> E["groupby(df.date.dt.date)"]
🧪 Try It Yourself
Exercise 1 – Parse Strings Into Datetime
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Extract the Weekday Name
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Handle Unparseable Dates Safely
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Check your understanding
What does pd.to_datetime(col, errors="coerce") do with an invalid timestamp?
NaT is the missing value for datetimes. Investigate those rows afterwards.
Show answer
A — Converts it to NaT — NaT is the missing value for datetimes. Investigate those rows afterwards.
Which expression gives the weekday name, such as Wednesday, from a datetime column?
The dt accessor exposes the parts of a datetime. dt.day gives the day of the month as a number.
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.
In the orders data, 2025-01-01 has orders of 250 and 180. What do the daily totals show for that date?
Grouping by the date sums 250 + 180 = 430 and counts 2 orders.
Show answer
A — total_amount 430 and 2 orders — Grouping by the date sums 250 + 180 = 430 and counts 2 orders.
What is a missing datetime value called?
Missing datetimes are stored as NaT, not NaN.
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.