stefan-jansen / machine-learning-for-trading

Code for Machine Learning for Algorithmic Trading, 2nd edition.
https://ml4trading.io
13.3k stars 4.2k forks source link

Chapter 02 : 'UnImplemented' object has no attribute 'description' #322

Closed EarltShirt closed 1 month ago

EarltShirt commented 3 months ago

Describe the bug Everytime I'm trying to access store['P'] I get the same error :

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-48-7ec50b54eb29> in <module>
      6     # The append() method is deprecated on new versions of Pandas and no longer works. Use the _append() method.
      7     # The concat method cannot be used because it changes the format of Q, from "table" to "fixed"
----> 8     trades = store['P']._append(store['Q'].rename(columns={'cross_price': 'price'}), sort=False).merge(stocks)
      9     trades = pd.concat([store['P'], store['Q']]).merge(stocks)
     10 

/opt/miniconda3/envs/ml4t/lib/python3.8/site-packages/pandas/io/pytables.py in __getitem__(self, key)
    576 
    577     def __getitem__(self, key: str):
--> 578         return self.get(key)
    579 
    580     def __setitem__(self, key: str, value):

/opt/miniconda3/envs/ml4t/lib/python3.8/site-packages/pandas/io/pytables.py in get(self, key)
    768             if group is None:
    769                 raise KeyError(f"No object named {key} in the file")
--> 770             return self._read_group(group)
    771 
    772     def select(

/opt/miniconda3/envs/ml4t/lib/python3.8/site-packages/pandas/io/pytables.py in _read_group(self, group)
   1761     def _read_group(self, group: "Node"):
...
-> 3352         return self.table.description
   3353 
   3354     @property

AttributeError: 'UnImplemented' object has no attribute 'description'

To Reproduce I'm running the 'newer' versions with the Impesud Notes, for example this cell :

with pd.HDFStore(itch_store) as store:
    stocks = store['R'].loc[:, ['stock_locate', 'stock']]
    # trades = store['P'].append(store['Q'].rename(columns={'cross_price': 'price'}), sort=False).merge(stocks)
    # Impesud Note:
    # The append() method is deprecated on new versions of Pandas and no longer works. Use the _append() method.
    # The concat method cannot be used because it changes the format of Q, from "table" to "fixed"
    trades = store['P']._append(store['Q'].rename(columns={'cross_price': 'price'}), sort=False).merge(stocks)
    trades = pd.concat([store['P'], store['Q']]).merge(stocks)

trades['value'] = trades.shares.mul(trades.price)
trades['value_share'] = trades.value.div(trades.value.sum())

trade_summary = trades.groupby('stock').value_share.sum().sort_values(ascending=False)
trade_summary.iloc[:50].plot.bar(figsize=(14, 6), color='darkblue', title='Share of Traded Value')

plt.gca().yaxis.set_major_formatter(FuncFormatter(lambda y, _: '{:.0%}'.format(y)))
sns.despine()
plt.tight_layout()

Steps to reproduce the behavior: As seen in the picture, this behavior is systematic when executing some of the first cells of the second chapter of the repo. P is indeed a key to the data frame, but the type isn't recognized because it 'doesn't have a description' ...

Capture d’écran 2024-07-18 à 14 18 48

And I systematically get this error :

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Environment If you are not using the latest version of the Docker imag:

stefan-jansen commented 1 month ago

Please do not use _append() but rather follow the pandas documentation and use pd.concat instead.