Python difference in years between a datetime.now() and a Series filled up with dates?

喜欢而已 提交于 2019-12-22 10:43:57

问题


I would like to create a new column in my dataset, which is a difference in years between today and a another column already in the dataset, filled up with dates.

the code above:

df['diff_years'] = datetime.today() - df['some_date']
df['diff_years']

give me the following output (exemple):

1754 days 11:44:28.971615

and i have to get something like (meaning the output above in years):

4,8 
(or 5)

I appreciate any help!

PS.: i would like to avoid looping the series, path i believe would give me a desired solution, but due having a big series i would like to avoid this way.


回答1:


Before some days i was facing same issue in my project now i had tried with these ,

from dateutil.relativedelta import relativedelta
from datetime import date
now = date.today()
some_date = date(df['some_date'])

rdelta = relativedelta(now, some_date)
print('diff in years - ', rdelta.years)
print('remaining months - ', rdelta.months)
print('remaining days - ', rdelta.days)

It should print difference in years




回答2:


Here is one way:

import pandas as pd, numpy as np

df = pd.DataFrame({'date': ['2009-06-15 00:00:00']})

df['years'] = (pd.to_datetime('now') - pd.to_datetime(df['date'])) / np.timedelta64(1, 'Y')

#                   date     years
# 0  2009-06-15 00:00:00  8.713745


来源:https://stackoverflow.com/questions/49071561/python-difference-in-years-between-a-datetime-now-and-a-series-filled-up-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!