我如何获得 pandas 的年龄和日期

How do I get an age in years and date on pandas

这是我的数据

Customer_id    Date-of-birth
1              1992-07-02
2              1991-07-03

这是我的代码

import datetime as dt
df['now'] = dt.datetime.now()
df['age'] = df['now'].dt.date - df['Date-of-birth']

这是结果

Customer_id    Date-of-birth     age
1              1992-07-02        xxxx days
2              1991-07-03        xxxx days

我想要的结果是

Customer_id    Date-of-birth     age
1              1992-07-02        26 years 22 days
2              1991-07-03        27 years 21 days

现在让你,df.dtypesDate-of-birth 是一个对象,因为它基于客户在下拉列表中的输入

我怎样才能做到这一点?我希望问题足够清楚

使用astype('<m8[Y]')

例如:

df['age'] = (pd.to_datetime('now') - df['Date-of-birth']).astype('<m8[Y]')

演示:

import pandas as pd

df = pd.DataFrame({"Date-of-birth": pd.to_datetime(['1992-07-24', '1991-07-24'])})
df["age"] = (pd.to_datetime('now') - df["Date-of-birth"]).astype('<m8[Y]')
print(df)

输出:

  Date-of-birth   age
0    1992-07-24  25.0
1    1991-07-24  27.0

也许你可以使用类似下面的东西。请注意,它依赖于平均一年有 365.25 天的事实,因此有时可能会休息一天。

import datetime as dt

def year_days_diff(x):
    diff = (dt.datetime.now() - x).days
    return str(int(diff / 365.25)) + ' years ' + str(int(diff / 365.25 % 1 * 365.25)) + ' days'

示例:

birth_date = dt.datetime.now() - dt.timedelta(10000)
year_days_diff(birth_date)

输出:

'27 years 138 days'

这可以通过四舍五入得出您的年龄。

ref_date = dt.datetime.now()
df['age'] = df['Date-of-birth'].apply(lambda x: len(pd.date_range(start = x, end = ref_date, freq = 'Y'))) 

输入:

import pandas as pd
import datetime as dt

now = dt.datetime.now()
for i in range(0, len(df)):
    diff = now - dt.datetime.strptime(df['Date-of-Birth'][i], '%Y-%m-%d')
    years = diff.days // 365
    days = diff.days - (years*365)
    df['age'][i] = str(years) + ' years ' + str(days) + ' days'

print(df)

输出:

Customer_id     Date-of-Birth          age
    1            1992-07-04       26 years 25 days
    2            1991-07-04       27 years 26 days

使用自定义函数this solution,因为闰年​​所以计算起来不容易:

from dateutil.relativedelta import relativedelta

def f(end):
    r = relativedelta(pd.to_datetime('now'), end) 
    return '{} years {} days'.format(r.years, r.days)

df['age'] = df["Date-of-birth"].apply(f)
print (df)
   Customer_id Date-of-birth               age
0            1    1992-07-02  26 years 22 days
1            2    1991-07-03  27 years 21 days