AI4Finance-Foundation / FinRL

FinRL: Financial Reinforcement Learning. 🔥
https://ai4finance.org
MIT License
9.48k stars 2.31k forks source link

Problem in Stock_NeurlPS2018_3_Backtest Mean Variance Optimization - Panda 2.0.0 issue #984

Open joshwigginton opened 1 year ago

joshwigginton commented 1 year ago

Going through part 3 of the Tutorials, when I am creating the Mean Variance Optimization, switched to Pandas 1.5.3 and no issue, just reporting possible incompatibility w/ Pandas 2.0.0

Python 3.9.16 Pandas 2.0.0

I am getting the following errors from this cell:

StockData = process_df_for_mvo(train) TradeData = process_df_for_mvo(trade)

TradeData.to_numpy()

KeyError: "None of [Index(['2009-01-02', '2009-01-02', '2009-01-02', '2009-01-02', '2009-01-02',\n '2009-01-02', '2009-01-02', '2009-01-02', '2009-01-02', '2009-01-02',\n '2009-01-02', '2009-01-02', '2009-01-02', '2009-01-02', '2009-01-02',\n '2009-01-02', '2009-01-02', '2009-01-02', '2009-01-02', '2009-01-02',\n '2009-01-02', '2009-01-02', '2009-01-02', '2009-01-02', '2009-01-02',\n '2009-01-02', '2009-01-02', '2009-01-02', '2009-01-02'],\n dtype='object')] are in the [index]

Traceback: KeyError Traceback (most recent call last) Cell In[21], line 1 ----> 1 StockData = process_df_for_mvo(train) 2 TradeData = process_df_for_mvo(trade) 4 TradeData.to_numpy()

Cell In[19], line 16, in process_df_for_mvo(df) 14 n = n.iloc[i stock_dimension:(i+1) stock_dimension, :] 15 date = n['date'][i*stock_dimension] ---> 16 mvo.loc[date] = n['close'].tolist() 18 return mvo

File /home/user/anaconda3/envs/FinRL/lib/python3.9/site-packages/pandas/core/indexing.py:845, in _LocationIndexer.setitem(self, key, value) 843 else: 844 key = com.apply_if_callable(key, self.obj) --> 845 indexer = self._get_setitem_indexer(key) 846 self._has_valid_setitem_indexer(key) 848 iloc = self if self.name == "iloc" else self.obj.iloc

File /home/user/anaconda3/envs/FinRL/lib/python3.9/site-packages/pandas/core/indexing.py:716, in _LocationIndexer._get_setitem_indexer(self, key) 712 if isinstance(key, range): 713 # GH#45479 test_loc_setitem_range_key 714 key = list(key) --> 716 return self._convert_to_indexer(key, axis=0) ... -> 5935 raise KeyError(f"None of [{key}] are in the [{axis_name}]") 5937 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique()) 5938 raise KeyError(f"{not_found} not in index")

funnymind commented 1 year ago

This is becouse of sort_values works incorect(my thoughts), it ignores "ignore_index" parameter, temporary solution could be to add: df = df.reset_index(drop = True) line, after df = df.sort_values in process_df_for_mvo method.