Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从带有日期时间索引的数据框中获取选定列的下一个月的值_Python_Python 3.x_Pandas_Datetime - Fatal编程技术网

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

Python 如何从带有日期时间索引的数据框中获取选定列的下一个月的值,python,python-3.x,pandas,datetime,Python,Python 3.x,Pandas,Datetime,我有以下数据框(日期时间索引,所有工作日在美国日历中) 对于列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

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

对于列
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天):


谢谢,是否有一些方法可以精确获得一个月后日期的值,例如,对于索引
2018-01-02
新日期应该是
2018-02-02
,对于索引
2018-01-05
,新日期
2018-02-05
等,您可以在上述代码中删除
pd.offset.BDay()。
              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
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')
      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