pandas 如何查找两个日期之间的年差

h9a6wy2h  于 2023-03-16  发布在  其他
关注(0)|答案(4)|浏览(175)

在我的dataframe中有两列被转换成了datetime,我试着减去这两个数字,然后找出它们之间的年份差,这是我使用的代码:

from dateutil.relativedelta import relativedelta
difference_in_years = relativedelta(x['start'], x['end']).year

但是,我收到以下错误消息:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

问题是什么?

camsedfj

camsedfj1#

将属性.yearsapplyaxis=1一起用于按行处理:

df = pd.DataFrame({'start':['2015-10-02','2014-11-05'],
                   'end':['2018-01-02','2018-10-05']})

df['start'] = pd.to_datetime(df['start'])
df['end'] = pd.to_datetime(df['end'])

from dateutil.relativedelta import relativedelta

df['y'] = df.apply(lambda x: relativedelta(x['end'], x['start']).years, axis=1)

或者使用list comprehension
x一个一个一个一个x一个一个二个x
编辑:

df = pd.DataFrame({'start':['2015-10-02','2014-11-05'],
                   'end':['2018-01-02',np.nan]})

df['start'] = pd.to_datetime(df['start'])
df['end'] = pd.to_datetime(df['end'])

from dateutil.relativedelta import relativedelta

m = df[['start','end']].notnull().all(axis=1)
df.loc[m, 'y'] = df[m].apply(lambda x: relativedelta(x['end'], x['start']).years, axis=1)
print (df)
       start        end    y
0 2015-10-02 2018-01-02  2.0
1 2014-11-05        NaT  NaN
zzzyeukh

zzzyeukh2#

可以按年单位划分timedelta系列,如有必要,还可以四舍五入:

# data from jezrael

df['years'] = (df['end'] - df['start']) / np.timedelta64(1, 'Y')
df['years_floor'] = df['years'].round()

print(df)

       start        end     years  years_floor
0 2015-10-02 2018-01-02  2.253297          2.0
1 2014-11-05        NaT       NaN          NaN
jchrr9hc

jchrr9hc3#

你可以通过

(df['end'] - df['start'])/pd.Timedelta(1, 'Y')

并且如果需要的话对结果进行舍入。
在Pandasv0.23.4和以后你可以做

(df['end'] - df['start'])//pd.Timedelta(1, 'Y')

直接得到全年的差价。
更新:在panda v0.25和更高版本中,不支持pd.Timedelta(1, 'Y'),因为年不是一致的度量单位(有时是365d,有时是366d)。如果365d近似值可以接受,您可以改为使用pd.Timedelta(1, 'Y')

(df['end'] - df['start'])/pd.Timedelta(365, 'D')
yshpjwxd

yshpjwxd4#

检查此答案calculate the difference between two datetime.date() dates in years and months

from dateutil import relativedelta as rdelta
from datetime import date
d1 = date(2001,5,1)
d2 = date(2012,1,1)
rd = rdelta.relativedelta(d2,d1)
rd
relativedelta(years=+10, months=+8)

相关问题