0

I have a dataframe with two columns to be given in date, but the dtype of that is object.

DF :

   A                B
7/27/2002   5/29/2013
5/25/2004   4/21/2005
4/22/2008   4/28/2010
6/22/2007   7/30/2008
7/26/2008   6/21/2011
7/29/2008   6/20/2013
6/26/2000   7/23/2005
6/20/1991   7/27/2013
5/22/2005   4/27/2010

I want to subtract B from A to get the no of years and no of days in each of separate columns.

OUTPUT EXPECTED :

NO OF YEARS    NO OF DAYS
1                  320
2                  600
3                  900
pylearner
  • 1,358
  • 2
  • 10
  • 26

1 Answers1

2

I think need convert all values to datetimes, then subtract by sub and convert timedeltas to days, for years is used divide by constant 365.2425 with floor:

df = df.apply(pd.to_datetime)

df['days'] = df['B'].sub(df['A']).dt.days 
df['years'] = np.floor(df['days'] / 365.2425).astype(int)
print (df)
           A          B  days  years
0 2002-07-27 2013-05-29  3959     10
1 2004-05-25 2005-04-21   331      0
2 2008-04-22 2010-04-28   736      2
3 2007-06-22 2008-07-30   404      1
4 2008-07-26 2011-06-21  1060      2
5 2008-07-29 2013-06-20  1787      4
6 2000-06-26 2005-07-23  1853      5
7 1991-06-20 2013-07-27  8073     22
8 2005-05-22 2010-04-27  1801      4
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252