简体   繁体   English

pandas dataframe 中 datetime.index 列中两个连续日期之间的天数差异

[英]Days Difference between two consecutive Dates in datetime.index column in pandas dataframe

Below is my Dataframe df which contains datetime index.下面是我的 Dataframe df ,其中包含日期时间索引。

              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 difference between two consecutive "Date" in datetime.index column.我想计算 datetime.index 列中两个连续“日期”之间的差异。

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 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请让我知道计算 pandas 中连续两行之间的天数差异的解决方案

If need count values per Currency use DataFrameGroupBy.diff :如果需要计算每个Currency的值,请使用DataFrameGroupBy.diff

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

If need without groups use Series.diff :如果需要没有组使用Series.diff

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

for the count values set by Currency you'll need to use groupby对于 Currency 设置的计数值,您需要使用 groupby

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dataframe:添加“就地”列,其中包含 datetime.index 中出现的日期的 cumcount() - Dataframe: Add 'inplace' a column with the cumcount() of the dates appeared in the datetime.index Pandas:选择两个日期之间的 DataFrame 行(日期时间索引) - Pandas: Selecting DataFrame rows between two dates (Datetime Index) 添加日期之间存在差异的列 pandas DataFrame - Add column with difference between dates pandas DataFrame 计算 Pandas DataFrame 的两个日期之间的差异 - Calculate difference between two dates for a Pandas DataFrame 如何使这个 function 在 python dataframe 和 datetime.index 中工作 - How to make this function work in a python dataframe with datetime.index Python-熊猫-Groupby-两个日期之间的值(非天)差 - Python - Pandas - Groupby - Value (not days) difference between two dates Pandas 数据帧中任意两连续行之间差异的平均值 - Pandas average of the difference between any two consecutive rows in dataframe 多索引熊猫数据框中两个不同日期的分组总和之间的差异 - Difference between grouped sums for two different dates in multi-index pandas dataframe 在DataFrame pandas中添加带有日期之间的天数的列 - Add column with number of days between dates in DataFrame pandas 一次计算 Pandas 数据框中每个日期列之间的天数差异 - Calculate days difference between every date column in Pandas dataframe at once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM