Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.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/swift/19.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_Pandas - Fatal编程技术网

在Python中将每日股票数据转换为每周股票数据

在Python中将每日股票数据转换为每周股票数据,python,pandas,Python,Pandas,我有以下数据格式 Date Open High Low Close 2018-11-12 **10607.80** 10645.50 10464.05 10482.20 2018-11-13 10451.90 10596.25 10440.55 10582.50 2018-11-14 10634.90 10651.60 10532.70 10576.30 2018-11-15 10580.60 10646.50 10

我有以下数据格式

Date          Open      High        Low     Close
2018-11-12  **10607.80**  10645.50  10464.05  10482.20
2018-11-13  10451.90  10596.25  10440.55  10582.50
2018-11-14  10634.90  10651.60  10532.70  10576.30
2018-11-15  10580.60  10646.50  10557.50  10616.70
2018-11-16  10644.00  10695.15  10631.15  **10682.20**
2018-11-19  **10731.25**  10774.70  10688.80  10763.40
2018-11-20  10740.10  10740.85  10640.85  10656.20
2018-11-21  10670.95  10671.30  10562.35  10600.05
2018-11-22  10612.65  10646.25  10512.00  **10526.75**
2018-11-26  **10568.30**  10637.80  10489.75  10628.60
2018-11-27  10621.45  10695.15  10596.35  10685.60
2018-11-28  10708.75  10757.80  10699.85  10728.85
2018-11-29  10808.70  10883.05  10782.35  10858.70
2018-11-30  10892.10  10922.45  10835.10  **10876.75**
我想知道周一的开盘价和下周五的收盘价

这是我的代码

open = df.Open.resample('W-MON').last()
print open.tail(5)

close = df.Close.resample('W-FRI').last().resample('W-MON').first()
print close.tail(5)

weekly_data = pd.concat([open, close], axis=1)

print weekly_data.tail(5)
它分别为打开和关闭提供正确的数据,但当我合并到每周的_数据时,它为关闭提供错误的输出。它显示了上周五的收盘价

如何解决此问题?

您可以在
-4
天内对这两个
日期时间索引进行对齐:

open = df.Open.resample('W-MON').last()
print (open.tail(5))
Date
2018-11-12    10607.80
2018-11-19    10731.25
2018-11-26    10568.30
2018-12-03    10892.10
Freq: W-MON, Name: Open, dtype: float64

close = df.Close.resample('W-FRI').last().shift(-4, freq='D')
print (close.tail(5))
Date
2018-11-12    10682.20
2018-11-19    10526.75
2018-11-26    10876.75
Freq: W-MON, Name: Close, dtype: float64

weekly_data = pd.concat([open, close], axis=1)
print (weekly_data)
                Open     Close
Date                          
2018-11-12  10607.80  10682.20
2018-11-19  10731.25  10526.75
2018-11-26  10568.30  10876.75
2018-12-03  10892.10       NaN

@杰斯雷尔,为什么只移动索引,你能添加细节吗pls@pyd-因为它移位
datetimeIndex
,所以它有一个参数
freq