The date_field() function defines the constraints and behavior for a date column when generating synthetic data with generate_dataset(). You can control the date range with min_date= and max_date=, enforce uniqueness with unique=True, and introduce null values with nullable=True and null_probability=.
Dates are generated uniformly within the specified range. If no range is provided, the default range is 2000-01-01 to 2030-12-31. Both min_date= and max_date= accept either datetime.date objects or ISO 8601 date strings (e.g., "2024-06-15").
Parameters
min_date:str | date | None=None
Minimum date (inclusive). Can be an ISO format string (e.g., "2020-01-01") or a datetime.date object. Default is None (defaults to 2000-01-01).
max_date:str | date | None=None
Maximum date (inclusive). Can be an ISO format string (e.g., "2024-12-31") or a datetime.date object. Default is None (defaults to 2030-12-31).
nullable:bool=False
Whether the column can contain null values. Default is False.
null_probability:float=0.0
Probability of generating a null value for each row when nullable=True. Must be between 0.0 and 1.0. Default is 0.0.
unique:bool=False
Whether all values must be unique. Default is False. When True, the generator will retry until it produces n distinct dates. Ensure the date range is large enough to accommodate the requested number of unique dates.
generator:Callable[[], Any] | None=None
Custom callable that generates values. When provided, this overrides all other constraints. The callable should take no arguments and return a single datetime.date value.
Returns
DateField
A date field specification that can be passed to Schema().
Raises
:ValueError
If min_date is later than max_date, or if a date string cannot be parsed.
Examples
The min_date= and max_date= parameters accept datetime.date objects to define date ranges: