calculate datetime-difference in years, months, etc. in a new pandas dataframe column

后端 未结 7 2268
长发绾君心
长发绾君心 2021-02-07 05:34

I have a pandas dataframe looking like this:

Name    start        end
A       2000-01-10   1970-04-29

I want to add a new column providing the

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 06:02

    Pretty much straightforward with relativedelta:

    from dateutil import relativedelta
    
    >>          end      start
    >> 0 1970-04-29 2000-01-10
    
    for i in df.index:
        df.at[i, 'diff'] = relativedelta.relativedelta(df.ix[i, 'start'], df.ix[i, 'end'])
    
    >>          end      start                                           diff
    >> 0 1970-04-29 2000-01-10  relativedelta(years=+29, months=+8, days=+12)
    

提交回复
热议问题