Implementation:Fastai Fastbook Add Datepart
| Knowledge Sources | |
|---|---|
| Domains | Feature Engineering, Tabular Data |
| Last Updated | 2026-02-09 17:00 GMT |
Overview
Concrete tool for temporal feature engineering provided by fastai. It decomposes a date column into 13 distinct temporal features suitable for tree-based and neural network models.
Description
add_datepart is a fastai utility function that takes a DataFrame and the name of a date column, parses the column into a pandas datetime type, and then creates 13 new columns representing different temporal dimensions of the date: Year, Month, Week, Day, Dayofweek, Dayofyear, Is_month_end, Is_month_start, Is_quarter_end, Is_quarter_start, Is_year_end, Is_year_start, and Elapsed (seconds since epoch). By default, the original date column is dropped after extraction. Each new column is prefixed with the original column name (e.g., saledate produces saleYear, saleMonth, etc.).
Usage
Use add_datepart immediately after loading a tabular dataset that contains date columns. Apply it to both training and test DataFrames to ensure consistent feature sets. This is typically the first feature engineering step before creating a TabularPandas object.
Code Reference
Source Location
- Repository: fastbook
- File: translations/cn/09_tabular.md (Lines 311-331)
- Library source: fastai.tabular.core
Signature
def add_datepart(df, field_name, prefix=None, drop=True, time=False):
"""
Extracts date parts from a date column and adds them as new columns.
Parameters
----------
df : pandas.DataFrame
The DataFrame containing the date column.
field_name : str
The name of the date column to decompose.
prefix : str or None
Prefix for new column names. Defaults to field_name if None.
drop : bool
Whether to drop the original date column. Default True.
time : bool
Whether to also extract time-of-day features (Hour, Minute, Second).
Default False.
"""
Import
from fastai.tabular.all import add_datepart
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| df | pandas.DataFrame | Yes | DataFrame containing the date column to decompose |
| field_name | str | Yes | Name of the column holding date values (e.g., 'saledate')
|
| prefix | str or None | No | Prefix for the generated columns. Defaults to field_name. |
| drop | bool | No | If True (default), removes the original date column after extraction. |
| time | bool | No | If True, also extracts Hour, Minute, Second features. Default False. |
Outputs
| Name | Type | Description |
|---|---|---|
| df (modified in-place) | pandas.DataFrame | The same DataFrame with 13 new columns added (and original date column optionally dropped). New columns: {prefix}Year, {prefix}Month, {prefix}Week, {prefix}Day, {prefix}Dayofweek, {prefix}Dayofyear, {prefix}Is_month_end, {prefix}Is_month_start, {prefix}Is_quarter_end, {prefix}Is_quarter_start, {prefix}Is_year_end, {prefix}Is_year_start, {prefix}Elapsed.
|
Usage Examples
Basic Usage
from fastai.tabular.all import add_datepart
import pandas as pd
# Load dataset
path = Path('bulldozers')
df = pd.read_csv(path/'TrainAndValid.csv', low_memory=False)
# Decompose the 'saledate' column into 13 temporal features
df = add_datepart(df, 'saledate')
# Verify the new columns
sale_cols = [o for o in df.columns if o.startswith('sale')]
print(' '.join(sale_cols))
# saleYear saleMonth saleWeek saleDay saleDayofweek saleDayofyear
# saleIs_month_end saleIs_month_start saleIs_quarter_end saleIs_quarter_start
# saleIs_year_end saleIs_year_start saleElapsed
# Apply the same transformation to the test set
df_test = pd.read_csv(path/'Test.csv', low_memory=False)
df_test = add_datepart(df_test, 'saledate')
With Time Features
# If your date column includes timestamps and you want time-of-day features
df = add_datepart(df, 'created_at', time=True)
# This adds additional columns: created_atHour, created_atMinute, created_atSecond