Search code examples
pythonpython-3.xpandasdatetime-formatsubtraction

Count of Year in Python


How can I find count of year in python from certain date and a date since people opened an account (CRDACCT_DTE_OPEN)? The certain date (MIS_DATE) is 2021-03-01 with format= '%Y%m%d'.

The dataset given below:

import pandas as pd
df = pd.DataFrame(
     { "MIS_DATE": ["2018-03-02", "2020-03-26", "2019-08-17", "2019-08-17", "2019-08-19"],
       "CRDACCT_DTE_OPEN": ["2021-03-31", "2021-03-31", "2021-03-31", "2021-03-31",  "2021-03-31"]})

Format date:

df['CRDACCT_DTE_OPEN'] = pd.to_datetime(df['CRDACCT_DTE_OPEN'], format='%Y%m%d')
df['MIS_DATE'] = pd.to_datetime(df['MIS_DATE'], format='%d%m%Y')

I've tried to do this operation. Let's say MOB is subtract of MIS_DATE - CRDACCT_DTE_OPEN, but the result is not what I expected. I want the output in form of year format, for example if someone opened an account in 2018-03-31, hence the MOB is 3. Meaning that 3 years since that person open an account.

MOB = df['MIS_DATE'] - df['CRDACCT_DTE_OPEN']
MOB

Output:

1        370 days
2        592 days
3        592 days
4        590 days
 ...   

Name: MOB, Length: 5, dtype: timedelta64[ns]

Solution

  • You can use numpy's timedelta()

    import numpy
    (MOB/numpy.timedelta64(1, 'Y')).round()
    0    3.0
    1    1.0
    2    2.0
    3    2.0
    4    2.0
    dtype: float64