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

后端 未结 7 2270
长发绾君心
长发绾君心 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:26

    I think this is the most 'pandas' way to do it, without using any for loops or defining external functions:

    >>> df = pd.DataFrame({'Name': ['A'], 'start': [datetime(2000, 1, 10)], 'end': [datetime(1970, 4, 29)]})
    >>> df['diff'] = map(lambda td: datetime(1, 1, 1) + td, list(df['start'] - df['end']))
    >>> df['diff'] = df['diff'].apply(lambda d: '{0}y{1}m'.format(d.year - 1, d.month - 1))
    >>> df
      Name        end      start   diff
    0    A 1970-04-29 2000-01-10  29y8m
    

    Had to use map instead of apply because of pandas' timedelda64, which doesn't allow a simple addition to a datetime object.

    0 讨论(0)
提交回复
热议问题