recode_generation

recode_generation(x, input='age', year=None, scheme=None)

Recode age or birth year into a generational cohort.

Maps either an age (converted to a birth year using a reference year) or a birth year directly onto a generational cohort such as Millennial or Gen Z. Text input like “34 years” is salvaged with ensure_numeric().

When input="age", the birth year is year - age, where year defaults to the current_year option or the system year, so set ezrsurvey_options(current_year=2026) to keep results stable across runs. When input="year", x is treated as the birth year directly. Birth years before the earliest cohort return missing. For simple age bands use recode_age().

Parameters

Name Type Description Default
x list or Series Ages or birth years (numbers or coercible text). required
input str What x represents: “age” (default) or “year” (birth year). 'age'
year int Reference year for the age-to-birth-year conversion. If None (default), uses the current_year option or the system year. None
scheme str or pandas.DataFrame Either a scheme name passed to generation_scheme() (default from the generation_scheme option) or your own data frame with from and label columns. None

Returns

Name Type Description
pandas.Series A categorical of cohort labels, categories from the oldest cohort to the youngest; values outside the scheme’s range are missing.

See Also

generation_scheme, recode_age, ezrsurvey_options

Examples

>>> recode_generation([1990, 2001, 1968], input="year")
>>> recode_generation([36, 25], input="age", year=2026)