前提・実現したいこと
機械学習を用いて、価格変動を予測するにあたって、「次の日の価格」を目的変数に設定したいです。
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-49-b09e6a1aff7f> in <module> 1 data_next = [] 2 for i in idx: ----> 3 next_value = dataset["Close"][i+1] 4 data_next.append(next_value) 5 dataset["next"] = data_next ~/anaconda3/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key) 765 key = com._apply_if_callable(key, self) 766 try: --> 767 result = self.index.get_value(self, key) 768 769 if not is_scalar(result): ~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key) 3116 try: 3117 return self._engine.get_value(s, k, -> 3118 tz=getattr(series.dtype, 'tz', None)) 3119 except KeyError as e1: 3120 if len(self) > 0 and self.inferred_type in ['integer', 'boolean']: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item() KeyError: 7
該当のソースコード
Python
1data_next = [] 2for i in idx: 3 next_value = dataset["Close"][i+1] 4 data_next.append(next_value) 5dataset["next"] = data_next
元データ
|Date|Open|High|Low|Close|Adj Close|Volume|3SMA|5SMA|7SMA|
|:--|:--:|--:|
|1980-01-04|6560.160156|6560.160156|6560.160156|6560.160156|6560.160156|0|NaN|NaN|NaN|
|1980-01-07|6552.649902|6552.649902|6552.649902|6552.649902|6552.649902|0|NaN|NaN|NaN|
|1980-01-08|6563.279785|6563.279785|6563.279785|6563.279785|6563.279785|0|6558.696614|NaN|NaN|
|1980-01-09|6591.830078|6591.830078|6591.830078|6591.830078|6591.830078|0|6569.253255| NaN|NaN|
|1980-01-10|6594.009766|6594.009766|6594.009766|6594.009766|6594.009766|0.0|6583.039876 |6572.385937|NaN|
|1980-01-11|6615.509766|6615.509766|6615.509766|6615.509766|6615.509766 |0.0|6600.449870|6583.455859|NaN|
|1980-01-14|6659.549805|6659.549805|6659.549805|6659.549805|6659.549805|0.0|6623.023112|6604.835840|6590.998465|
|1980-01-16|6653.259766|6653.259766|6653.259766|6653.259766|6653.259766|0.0|6642.773112|6622.831836|6604.298410|
|1980-01-17|6656.299805|6656.299805|6656.299805|6656.299805|6656.299805|0.0|6656.369792|6635.725782|6619.105539|
|1980-01-18|6649.299805|6649.299805|6649.299805|6649.299805|6649.299805|0.0|6652.953125|6646.783789|6631.394113|
|1980-01-21|6666.270020|666.270020|6666.270020|6666.270020|6666.270020|0.0|6657.289877|6656.935840 |6642.028390|
|1980-01-22|6684.589844|6684.589844|6684.589844|6684.589844|6684.589844|0.0|6666.719890|6661.943848|6654.968402|
|1980-01-23|6694.189941|6694.189941|6694.189941|6694.189941|6694.189941|0.0|6681.683268|6670.129883|6666.208427|
|1980-01-24|6746.930176|6746.930176|6746.930176|6746.930176|6746.930176|0.0|6708.569987|6688.255957|6678.691337|
|1980-01-25|6767.709961|6767.709961|6767.709961|6767.709961|6767.709961|0.0|6736.276693|6711.937988|6695.041365|
|1980-01-28|6763.609863|6763.609863|6763.609863|6763.609863|6763.609863|0.0|6759.416667|6731.405957|6710.371373|
|1980-01-29|6770.830078|6770.830078|6770.830078|6770.830078|6770.830078|0.0|6767.383301|6748.654004|6727.732840|
|1980-01-30|6776.600098|6776.600098|6776.600098|6776.600098|6776.600098|0.0|6770.346680|6765.136035|6743.494280|
|1980-01-31|6768.160156|6768.160156|6768.160156|6768.160156|6768.160156|0.0|6771.863444|6769.382031|6755.432896|
|1980-02-01|6777.640137|6777.640137|6777.640137|6777.640137|6777.640137|0.0|6774.133464|6771.368066|6767.354353|
例えば、このデータのCloseの2行目を新たな列の1行目として設定したいという趣旨の質問です。
コードの中で、i+1 を i に変更したらエラーが起きなかったので、最後の行の次の行のデータがないため、エラーになったのだと思います。
目的を達成するための手段を教えていただけますと幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/12 08:12