liveBook · Manning

Pandas datetime accessor

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

A pandas accessor that exposes datetime properties and operations for datetime-like Series and Index objects.

The pandas datetime accessor turns a datetime-like Series into a collection of useful calendar and clock features. After parsing dates, use expressions such as `s.dt.month`, `s.dt.month_name`, `s.dt.hour`, `s.dt.day_of_week`, and `s.dt.is_leap_year` without creating separate columns first. section 10.0

Group taxi tips by day and hour

Parse the pickup column, then group directly on accessor results: `df.groupby(df['tpep_pickup_datetime'].dt.day_of_week)['tip_percentage'].mean().sort_values(ascending=False)`. To compare hours, replace `day_of_week` with `hour`; to compare months, use `month`. The weekday values use Monday as 0 and Sunday as 6. section 10.14

Use the accessor as part of a broader datetime workflow:

  • Parse CSV date columns with `pd.read_csv(..., parse_dates=[...])` before applying `.dt`. section 10.0
  • Extract fields such as month, month name, hour, weekday, and leap-year status. section 10.0
  • Use extracted fields as grouping keys, then combine them with aggregation and sorting. section 10.14
  • Set a datetime column as the index when partial-date selection or resampling is clearer than column-based queries. section 10.10
Figure 10.1 My schedule, illustrated with timestamps and timedeltas

A schedule illustration helps connect extracted datetime parts with timestamps and timedeltas in practical time-based data. section 10.0

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Demonstrates accessor attributes and grouped analyses, while deferring the complete attribute list to external documentation.
Examples
Uses timestamp-property listings, historical oil prices, and New York taxi-tip comparisons by weekday, hour, and month.
What it assumes
Assumes familiarity with pandas Series, DataFrames, grouping, aggregation, and datetime parsing.
What you can do after
Apply it to group and summarize records by datetime components without manually materializing feature columns.
How it connects
Connects datetime parsing, timestamps, time-series indexing, groupby, aggregation, and resampling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.