Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas difference in index with date values

Tags:

python

pandas

If I have a DataFrame like this

Date           value
04 May 2015     1
06 May 2015     1
07 May 2015     1
11 May 2015     1
11 May 2015     1

How do I get the diff of the Date Index? i.e. the third col below:

Date           value   Diff
04 May 2015     1      NA
06 May 2015     1       2
07 May 2015     1       1
11 May 2015     1       4
11 May 2015     1       0
like image 813
dram Avatar asked Jul 21 '15 23:07

dram


People also ask

How can I find the difference between two dates in pandas?

Use df. dates1-df. dates2 to find the difference between the two dates and then convert the result in the form of months.

What is a date time index pandas?

class pandas. DatetimeIndex [source] Immutable ndarray of datetime64 data, represented internally as int64, and which can be boxed to Timestamp objects that are subclasses of datetime and carry metadata such as frequency information.

How do you filter data between two dates in Python?

There are two possible solutions: Use a boolean mask, then use df. loc[mask] Set the date column as a DatetimeIndex, then use df[start_date : end_date]


1 Answers

you can use pandas.Series.diff

>>> df['Diff'] = df.index.to_series().diff()

            value     Diff
Date                    
2015-05-04      1      NaT
2015-05-06      1   2 days
2015-05-07      1   1 days
2015-05-11      1   4 days
2015-05-11      1   0 days

elegant way to convert to float is

df['Diff'] = df.index.to_series().diff().dt.days
>>df
            value  Diff
Date                   
2015-05-04      1   NaN
2015-05-06      1   2.0
2015-05-07      1   1.0
2015-05-11      1   4.0
2015-05-11      1   0.0

more faster way is to typecast to days

df.index.to_series().diff().astype('timedelta64[D]')

to convert to Integer (pandas verson >= 0.24)

df.index.to_series().diff().astype('timedelta64[D]').astype('Int64') 
>>df
            value  Diff
Date                   
2015-05-04      1   NaN
2015-05-06      1     2
2015-05-07      1     1
2015-05-11      1     4
2015-05-11      1     0

Note : Int64 is Pandas Nullable Integer Data Type (not int64)

like image 106
Shijith Avatar answered Sep 20 '22 00:09

Shijith