Calendar feature extraction
Turn parsed timestamps into month, year, or quarter columns for reporting.
Pandas · Pipe
Series.dt is pandas’ dtype-specific accessor for datetime, timedelta, and period values. It provides vectorized date-related properties and methods such as extracting month, year, or weekday information.
Use the Series dt accessor after converting values to a pandas datetime-like dtype. It exposes calendar properties such as month and year, plus methods for date-related operations; each expression operates element by element on the Series.
Series.dt.<property_or_method>A datetimelike Series property or operation result; the exact result depends on the selected dt member.
Series.dt to reach datetimelike values and their supported properties.Series.dt.<property> form.import pandas as pd
s = pd.Series(pd.to_datetime(["2024-01-15", "2024-02-20"]))
print(s.dt.month.tolist())
print(s.dt.day_name().tolist())The accessor extracts calendar properties and provides date-related methods from a datetime-like Series.
Turn parsed timestamps into month, year, or quarter columns for reporting.
Derive weekday labels or numbers before grouping records.
Apply datetime-like formatting and transformations to a Series.
dt accessor?Series.dt is a dtype-specific namespace for datetimelike Series. It exposes datetime, timedelta, and period properties and operations such as month extraction and formatting.
Follow-up: What should be done when the source values are strings? Convert valid strings with pd.to_datetime before using dt.
dt?A datetime-like Series can use expressions such as s.dt.month to obtain a component and s.dt.day_name() to obtain a derived label. These operations are vectorized over the Series.
Follow-up: How would you group records by weekday? Use a weekday-related dt property, then group or aggregate using the resulting Series.
dt cover?The accessor supports multiple datetimelike families: datetime, timedelta, and period values. The available namespace depends on the Series dtype.
Follow-up: Why can the same dt expression fail on another Series? Its dtype may not belong to a supported datetimelike family.
pandas.Series.dt.monthpandas.Series.dt.yearpandas.Series.dt.quarterpandas.Series.dt.day_namepandas.to_datetime