python - 如何从带有日期时间索引的 pandas 数据框中获取选定列的下个月值

标签 python python-3.x pandas datetime

我有以下数据框(日期时间索引,所有工作日均采用美国日历)

import pandas as pd
from pandas.tseries.holiday import USFederalHolidayCalendar
from pandas.tseries.offsets import CustomBusinessDay

import random

us_bd = CustomBusinessDay(calendar=USFederalHolidayCalendar())
dt_rng = pd.date_range(start='1/1/2018', end='12/31/2018', freq=us_bd)

n1 = [round(random.uniform(20, 35),2) for _ in range(len(dt_rng))]
n2 = [random.randint(100, 200) for _ in range(len(dt_rng))]
df = pd.DataFrame(list(zip(n1,n2)), index=dt_rng, columns=['n1','n2'])

print(df)
               n1   n2
2018-01-02  24.78  197
2018-01-03  23.33  176
2018-01-04  33.19  128
2018-01-05  32.49  110
...           ...  ...
2018-12-26  31.34  173
2018-12-27  29.72  166
2018-12-28  31.07  104
2018-12-31  33.52  184

[251 rows x 2 columns]

对于n1列中的每一行,如何获取下个月同一天同一列的值? (如果该确切日期的值不可用(由于周末或节假日),则应在下一个可用日期获取该值。)。我尝试使用 df.n1.shift(21),但它不起作用,因为每个月的确切工作日不同。

预期输出如下

              n1   n2   next_mnth_val
2018-01-02  25.97  184  28.14
2018-01-03  24.94  133  27.65      # three values below are same, because on Feb 2018, the next working day after 2nd is 5th
2018-01-04  23.99  143  27.65
2018-01-05  24.69  182  27.65
2018-01-08  28.43  186  28.45
2018-01-09  31.47  104  23.14
...           ...  ...    ...
2018-12-26  29.06  194  20.45
2018-12-27  29.63  158  20.45
2018-12-28  30.60  148  20.45
2018-12-31  20.45  121  20.45

对于 12 月,下个月的值应该是数据框的最后一个值,即索引 2018-12-31 处的值 (20.45)。

请帮忙。

最佳答案

这是一个有趣的问题。我会将日期移动 1 个月,然后再次移动到下一个工作日:

df1 = df.copy().reset_index()
df1['new_date'] = df1['index'] + pd.DateOffset(months=1) + pd.offsets.BDay()
df.merge(df1, left_index=True, right_on='new_date')

输出(前 31 天):

      n1_x  n2_x      index   n1_y  n2_y   new_date
0    34.82   180 2018-01-02  29.83   129 2018-02-05
1    34.82   180 2018-01-03  24.28   166 2018-02-05
2    34.82   180 2018-01-04  27.88   110 2018-02-05
3    24.89   186 2018-01-05  25.34   111 2018-02-06
4    31.66   137 2018-01-08  26.28   138 2018-02-09
5    25.30   162 2018-01-09  32.71   139 2018-02-12
6    25.30   162 2018-01-10  34.39   159 2018-02-12
7    25.30   162 2018-01-11  20.89   132 2018-02-12
8    23.44   196 2018-01-12  29.27   167 2018-02-13
12   25.40   153 2018-01-19  28.52   185 2018-02-20
13   31.38   126 2018-01-22  23.49   141 2018-02-23
14   30.90   133 2018-01-23  25.56   145 2018-02-26
15   30.90   133 2018-01-24  23.06   155 2018-02-26
16   30.90   133 2018-01-25  24.95   174 2018-02-26
17   29.39   138 2018-01-26  21.28   157 2018-02-27
18   32.94   173 2018-01-29  20.26   189 2018-03-01
19   32.94   173 2018-01-30  22.41   196 2018-03-01
20   32.94   173 2018-01-31  27.32   149 2018-03-01
21   28.09   119 2018-02-01  31.39   192 2018-03-02
22   32.21   199 2018-02-02  28.22   151 2018-03-05
23   21.78   120 2018-02-05  34.82   180 2018-03-06
24   28.25   127 2018-02-06  24.89   186 2018-03-07
25   22.06   189 2018-02-07  32.85   125 2018-03-08
26   33.78   121 2018-02-08  30.12   102 2018-03-09
27   30.79   137 2018-02-09  31.66   137 2018-03-12
28   29.88   131 2018-02-12  25.30   162 2018-03-13
29   20.02   143 2018-02-13  23.44   196 2018-03-14
30   20.28   188 2018-02-14  20.04   102 2018-03-15

关于python - 如何从带有日期时间索引的 pandas 数据框中获取选定列的下个月值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58135853/

相关文章:

python - 这段代码中的运算符 == 和 * 是用来做什么的?

python - 类型错误 : cannot convert the series to <class 'float' >

python - 如果整行为空,如何删除 Pandas 数据框中的一行?

python - python/pandas read_csv 中的前导逗号

python - Pandas UnicodeEncodeError : 'charmap' codec can't encode character

python - 在验证集上调整模型的参数

python - 如何使用 SQLAlchemy 在插入时解析相关的外键?

python - 没有客户端/服务器的 key : value store in Python for possibly 100 GB of data,

python - 为什么只能从文本文件的开头到 `seek`?

python-3.x - mac 上的 Pycharm - 在 pycharm 的 ''(引号)之前禁用方形符号