Find the Date for n.xx Days Ago
Introduction
To find the birthdate of an individual based on a decimal typed aged column in a pandas DataFrame, you can follow these steps:
Convert the decimal age to a timedelta object in days.
Subtract the timedelta object from the current date.
Convert the resulting date to the desired format (e.g. YYYY-MM-DD).
Here's an example code snippet to accomplish this:
Example Code
import pandas as pd
from datetime import datetime, timedelta
# Create a sample dataframe with decimal age column
df = pd.DataFrame({'Name': ['John', 'Jane', 'Bob'],
'Age': [22.5, 31.2, 19.8]})
# Convert decimal age to timedelta object in days
df['Age_Days'] = pd.to_timedelta(df['Age'], unit='D')
# Subtract timedelta from current date
df['Birthdate'] = datetime.now().date() - df['Age_Days']
# Convert date to desired format
df['Birthdate'] = df['Birthdate'].dt.strftime('%Y-%m-%d')
# Display resulting dataframe
print(df)
This code will output a dataframe with the birthdate calculated for each individual based on their decimal age.