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
Use df. dates1-df. dates2 to find the difference between the two dates and then convert the result in the form of months.
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.
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]
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With