yoshiserry
yoshiserry

Reputation: 21355

Simplest way to find the difference between two dates in pandas

I'm trying to find the difference between two dates in a multi index data frame that is the result of a pivot table operation.

The data frame contains three columns. The first is a measurement the second is the end date and the third is the start date.

I've been able to successfully add a third multi index column to the data frame but only to make the result of reach cell zero

Pt["min"]["start_date"] = 0 but when I try to subtract the two dates I get a string error and appending .Dt.Days to the end of each column results in an error as well.

What is the simplest way to find the difference in days between two dates in a multi index pandas data frame?

Upvotes: 2

Views: 3308

Answers (1)

jezrael
jezrael

Reputation: 862761

You can select Multiindex in columns by tuples and subtract columns:

print (df)
     a                        
  meas         end       start
0    7  2015-04-05  2015-04-01
1    8  2015-04-07  2015-04-02
2    9  2015-04-14  2015-04-04

#if dtypes not datetime
df[('a','end')] = pd.to_datetime(df[('a','end')])
df[('a','start')] = pd.to_datetime(df[('a','start')])

df[('a','diff')] = df[('a','end')] - df[('a','start')]

print (df)
     a                              
  meas        end      start    diff
0    7 2015-04-05 2015-04-01  4 days
1    8 2015-04-07 2015-04-02  5 days
2    9 2015-04-14 2015-04-04 10 days

If need output in days:

df[('a','diff')] = (df[('a','end')] - df[('a','start')]).dt.days
print (df)
     a                           
  meas        end      start diff
0    7 2015-04-05 2015-04-01    4
1    8 2015-04-07 2015-04-02    5
2    9 2015-04-14 2015-04-04   10

Upvotes: 2

Related Questions