Skip to main content

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


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.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

calculate_age-0.1.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file calculate_age-0.1.1.tar.gz.

File metadata

  • Download URL: calculate_age-0.1.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for calculate_age-0.1.1.tar.gz
Algorithm Hash digest
SHA256 14ff71269bb16cef2f94fcfe1d8f82b04ddca838a2fbe5479bf25e24d2658f4c
MD5 1eae8a4ca25ffbcd9586c310a75a9f28
BLAKE2b-256 75cee22d95e08d63dec9d653dfeeea2663e5675504ba370a3f2f4bcde4f80cd4

See more details on using hashes here.

File details

Details for the file calculate_age-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: calculate_age-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for calculate_age-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 30bd4af29d1c1e43657df1a2fcf8f825ebcebf94011fa875e902c4c9bbeee4a5
MD5 7e4884a86678d05a3479fa10378c615a
BLAKE2b-256 4b764127cb64e74a9a29649685b9dbeee9b6bbc971ed5679b2a3f17cd3195bc1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page