Home > Net >  Days Difference between two consecutive Dates in datetime.index column in pandas dataframe
Days Difference between two consecutive Dates in datetime.index column in pandas dataframe

Time:04-25

Below is my Dataframe df which contains datetime index.

              Open        High        Low        Close         Volume   Currency
Date                        
2021-04-20  14526.70    14526.95    14207.30    14296.40    456704720896    INR
2021-04-22  14219.15    14424.75    14151.40    14406.15    516985257984    INR
2021-04-23  14326.35    14461.15    14273.30    14341.35    476613607424    INR
2021-04-26  14449.45    14557.50    14421.30    14485.00    448533331968    INR
2021-04-27  14493.80    14667.55    14484.85    14653.05    442211696640    INR
... ... ... ... ... ... ...
2022-04-19  17258.95    17275.65    16824.70    16958.65    401400000   INR
2022-04-20  17045.25    17186.90    16978.95    17136.55    286070016   INR
2022-04-21  17234.60    17414.70    17215.50    17392.60    285200000   INR
2022-04-22  17242.75    17315.30    17149.20    17171.95    262740000   INR
2022-04-25  17006.10    17052.10    16889.75    16953.95    275571  INR

I want to Calculate the differece between two consecutive "Date" in datetime.index coloumn.

I tried by using below code, but it's not working if difference between two dates is big. Code-

df['date'] = df.index.day  #Extract day from datetime.index
df['difference'] = df['date'].diff()
df

Output

             Open       High        Low          Close     Volume   Currency    date    difference
Date                                
2021-04-20  14526.70    14526.95    14207.30    14296.40    456704720896    INR 20  NaN
2021-04-22  14219.15    14424.75    14151.40    14406.15    516985257984    INR 22  2.0
2021-04-23  14326.35    14461.15    14273.30    14341.35    476613607424    INR 23  1.0
2021-04-26  14449.45    14557.50    14421.30    14485.00    448533331968    INR 26  3.0
2021-04-27  14493.80    14667.55    14484.85    14653.05    442211696640    INR 27  1.0
... ... ... ... ... ... ... ... ...
2022-04-19  17258.95    17275.65    16824.70    16958.65    401400000   INR 19  1.0
2022-04-20  17045.25    17186.90    16978.95    17136.55    286070016   INR 20  1.0
2022-04-21  17234.60    17414.70    17215.50    17392.60    285200000   INR 21  1.0
2022-04-22  17242.75    17315.30    17149.20    17171.95    262740000   INR 22  1.0
2022-04-25  17006.10    17052.10    16889.75    16953.95    275571  INR 25  3.0
252 rows × 8 columns

Pls let me know solution to calculate the Date difference in Days between two consecutive rows in pandas

CodePudding user response:

If need count values per Currency use DataFrameGroupBy.diff:

df['difference'] = df.index.to_series().groupby(df['Currency']).diff().dt.days

If need without groups use Series.diff:

df['difference'] = df.index.to_series().diff().dt.days

CodePudding user response:

for the count values set by Currency you'll need to use groupby

df['difference'] = df.index.to_series().groupby(df['Currency']).diff().dt.days

  • Related