Calculate ages from date columns in Pandas or Polars DataFrames
Project description
calculate-age
Calculate ages from date columns in Pandas/Polars DataFrames.
Install
pip install calculate-age
Usage
Accessor API (chainable)
import pandas as pd
import polars as pl
import calculate_age # registers accessors on import
#init a df
df = pd.DataFrame({
"name": ["Alice", "Bob"],
"birth_date": pd.to_datetime(["1990-01-01", "2000-06-15"])
})
# Pandas
df = df.calculate_age("birth_date")
# Adds column "age" (whole years vs today)
# name birth_date age
# 0 Alice 1990-01-01 36
# 1 Bob 2000-06-15 25
df = df.calculate_age_indays("birth_date")
# Adds column "age_in_days" (total days vs today)
# name birth_date age_in_days
# 0 Alice 1990-01-01 13223
# 1 Bob 2000-06-15 9374
df = df.calculate_age_exact("birth_date")
# Adds column "age_exact" (years to 2 decimal places vs today)
# name birth_date age_exact
# 0 Alice 1990-01-01 36.20
# 1 Bob 2000-06-15 25.75
# Polars
df_pl = df_pl.calculate_age("birth_date")
df_pl = df_pl.calculate_age_indays("birth_date")
df_pl = df_pl.calculate_age_exact("birth_date")
Function API
import datetime
from calculate_age import calculate_age, calculate_age_indays, calculate_age_exact
import pandas as pd
import polars as pl
# Pandas
df = pd.DataFrame({"birth_date": pd.to_datetime(["1990-01-01"])})
result = calculate_age(df, "birth_date") # vs today
result = calculate_age(df, "birth_date", datetipip install buildme.date(2026, 3, 15)) # vs a fixed date
result = calculate_age(df, "birth_date", "hire_date") # vs another column
result = calculate_age_indays(df, "birth_date") # age in total days
result = calculate_age_exact(df, "birth_date") # age in years (2 decimals)
# Polars
df_pl = pl.DataFrame({"birth_date": ["1990-01-01"]}).with_columns(
pl.col("birth_date").str.strptime(pl.Date, "%Y-%m-%d")
)
result = calculate_age(df_pl, "birth_date")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
calculate_age-0.1.2.tar.gz
(4.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file calculate_age-0.1.2.tar.gz.
File metadata
- Download URL: calculate_age-0.1.2.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9c5aa3c79ca756cb4554891b884c72bdbe6bdadccdcf730f10a3e64f2f38612
|
|
| MD5 |
8e4d2eb8bf2aac1f38ef82f00746c365
|
|
| BLAKE2b-256 |
193697cf7d6e0c40dbb27576d43e233405dbe7b638a647aaae9b644d81bebe07
|
File details
Details for the file calculate_age-0.1.2-py3-none-any.whl.
File metadata
- Download URL: calculate_age-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
449256c2a231943db1b1c89e05f13d852d4bbacf374bdd506413e72d188a745a
|
|
| MD5 |
87ee38e1b6538c7cdeb62c1887576964
|
|
| BLAKE2b-256 |
3436461fcb52be04cdac5068d9cd82fc41e34be9dd65d073dc9808eba75ccc2a
|