2

I got this code from youtube,

not sure, why the tutor(Sentdex) is not getting the same error as I am. I have a Test.csv file with dates as Index

Dates 'A Close' 'B Close' 'DLF Close' 'ICICI Close'
1 jan 18  555     111        122         400
2 jan 18  566     132        128         398

and so on .....

from collections import Counter
import numpy as np
import pandas as pd

hm_days = 7

def process_data(ticker):
    df = pd.read_csv('Test.csv', index_col=0)
    tickers = df.columns.values.tolist()
    df.fillna(0, inplace=True)
    for i in range(1, hm_days+1):
        df['{}_{}d'.format(ticker, i)] = (df[ticker].shift(-i)- 
                                      df[ticker])/df[ticker]
    df.fillna(0, inplace=True)

    return tickers, df

def buy_sell_hold(*args):
    cols = [c for c in args]
    req = 0.02
    for col in cols:
        if all(col) > req:
            return 1
        if all(col) < -req:
            return -1
    return 0

def extract_feature(ticker):
    tickers, df = process_data(ticker)
    df['{}_target'.format(ticker)] = list(map(buy_sell_hold, 
                                df[['{}_{}d'.format(ticker, i)
                                for i in range(1, hm_days + 1)]].values))
    vals = df['{}_target'.format(ticker)].values.tolist()
    str_vals = [str(i) for i in vals]
    print('Data spread:', Counter(str_vals))
    df.fillna(0, inplace=True)
    df = df.replace([np.inf, -np.inf], np.nan)
    df.dropna(inplace=True)
    df_vals = df[[ticker for ticker in tickers]].pct_change()
    df_vals = df_vals.replace([np.inf, -np.inf], 0)
    df_vals.fillna(0, inplace=True)
    x = df_vals.values
    y = df['{}_target'.format(ticker)].values

    return x, y, df

extract_feature('DLF Close')

This is the error I am getting:

Traceback (most recent call last):
  File "C:\Users\Sudipto\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3078, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'DLF Close_target'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Sudipto\Dropbox\Sentdex\PPF Backup\try.py", line 48, in <module>
    extract_feature('DLF Close')
  File "C:\Users\Sudipto\Dropbox\Sentdex\PPF Backup\try.py", line 33, in extract_feature
    vals = df['{}_target'.format(ticker)].values.tolist()
  File "C:\Users\Sudipto\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2688, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\Sudipto\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2695, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\Sudipto\Anaconda3\lib\site-packages\pandas\core\generic.py", line 2489, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\Sudipto\Anaconda3\lib\site-packages\pandas\core\internals.py", line 4115, in get
    loc = self.items.get_loc(item)
  File "C:\Users\Sudipto\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'DLF Close_target'

I gather the issue with the line:

vals = df['{}_target'.format(ticker)].values.tolist()

I checked the codes twice, thrice...and couldn't figure out what is wrong when I call for "DLF Close". Can anyone help me with this?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Sudipto Ghosh
  • 416
  • 2
  • 8
  • Use all() or any() – Lior Cohen Mar 22 '19 at 21:45
  • if col > req.all(): return 1 if col < -req.all(): AttributeError: 'float' object has no attribute 'all' if np.any(col > req): return 1 if np.any(col < -req): Error: During handling of the above exception, another exception occurred: – Sudipto Ghosh Mar 22 '19 at 21:53
  • Clearly `req` is a number, but what is `col` when it produces the error? If it is an array, then `col>req` will be a boolean array (print it if there's any doubt). Now what action do you want to take when you get `np.array([True, False, False, True,...])`? Only you can determine the correct action. – hpaulj Mar 22 '19 at 23:38

0 Answers0