Skip to content

KeyError: "Column 'name' not in data" #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nerdyckc opened this issue Feb 19, 2020 · 2 comments
Closed

KeyError: "Column 'name' not in data" #44

nerdyckc opened this issue Feb 19, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@nerdyckc
Copy link

nerdyckc commented Feb 19, 2020

any idea why i keep getting this error?

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-54-9b8afa777b14> in <module>
     20               cash=10000, commission=.002)
     21 
---> 22 bt.run()
     23 bt.plot()

~/miniconda3/lib/python3.7/site-packages/backtesting/backtesting.py in run(self, **kwargs)
    681         strategy._set_params(**kwargs)
    682 
--> 683         strategy.init()
    684         indicator_attrs = {attr: indicator
    685                            for attr, indicator in strategy.__dict__.items()

<ipython-input-54-9b8afa777b14> in init(self)
      7     def init(self):
      8         data_df = self.data
----> 9         self.supertrend = self.I(ST_numpy_wrapper, data_df, ATR_factor, period)
     10 
     11     def next(self):

~/miniconda3/lib/python3.7/site-packages/backtesting/backtesting.py in I(self, func, name, plot, overlay, color, scatter, *args, **kwargs)
    119         """
    120         if name is None:
--> 121             params = ','.join(filter(None, map(_as_str, chain(args, kwargs.values()))))
    122             func_name = func.__name__.replace('<lambda>', 'λ')
    123             name = ('{}({})' if params else '{}').format(func_name, params)

~/miniconda3/lib/python3.7/site-packages/backtesting/_util.py in _as_str(value)
      9     if isinstance(value, (Number, str)):
     10         return str(value)
---> 11     name = str(getattr(value, 'name', '') or '')
     12     if callable(value):
     13         name = value.__name__.replace('<lambda>', '')

~/miniconda3/lib/python3.7/site-packages/backtesting/_util.py in __getattr__(self, item)
     96             return self.__get_array(item)
     97         except KeyError:
---> 98             raise KeyError("Column '{}' not in data".format(item)) from None
     99 
    100     def _set_length(self, i):

KeyError: "Column 'name' not in data"
@kernc
Copy link
Owner

kernc commented Feb 19, 2020

You see the error because you pass data_df to your function:

self.supertrend = self.I(ST_numpy_wrapper, data_df, ATR_factor, period)

and data_df does not have either a column named "name" nor a .name attribute which only series have (note, data_df is not really a df, it just acts like one somewhat).

I'll look into supporting that case.

For the time being, the workaround is to pass individual series:

self.supertrend = self.I(ST_numpy_wrapper, data_df.High, data_df.Low, data_df.Close, ATR_factor, period)

@nerdyckc
Copy link
Author

Thanks a lot for your swift reply. I'll tweak my code accordingly 🙏

@kernc kernc added the bug Something isn't working label Feb 19, 2020
@kernc kernc closed this as completed in 469a930 Mar 23, 2020
Goblincomet pushed a commit to Goblincomet/forex-trading-backtest that referenced this issue Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants