Skip to content
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

KeyError: 0 when running example #6

Closed
ElLorans opened this issue Dec 2, 2020 · 2 comments · Fixed by #14
Closed

KeyError: 0 when running example #6

ElLorans opened this issue Dec 2, 2020 · 2 comments · Fixed by #14

Comments

@ElLorans
Copy link

ElLorans commented Dec 2, 2020

data = pd.Series([1, 8, 9, 10, 9])
grubbs.test(data, alpha=0.05)

Gives

KeyError                                  Traceback (most recent call last)
<ipython-input-5-1838b39dbcff> in <module>
      1 data = pd.Series([1, 8, 9, 10, 9])
----> 2 grubbs.test(data, alpha=0.05)

~\miniconda3\lib\site-packages\outliers\smirnov_grubbs.py in test(data, alpha)
    241 
    242 def test(data, alpha=DEFAULT_ALPHA):
--> 243     return two_sided_test(data, alpha)

~\miniconda3\lib\site-packages\outliers\smirnov_grubbs.py in two_sided_test(data, alpha)
    205 
    206 def two_sided_test(data, alpha=DEFAULT_ALPHA):
--> 207     return _two_sided_test(data, alpha, OutputType.DATA)
    208 
    209 

~\miniconda3\lib\site-packages\outliers\smirnov_grubbs.py in _two_sided_test(data, alpha, output_type)
    193 
    194 def _two_sided_test(data, alpha, output_type):
--> 195     return _test(TwoSidedGrubbsTest, data, alpha, output_type)
    196 
    197 

~\miniconda3\lib\site-packages\outliers\smirnov_grubbs.py in _test(test_class, data, alpha, output_type)
    189 
    190 def _test(test_class, data, alpha, output_type):
--> 191     return test_class(data).run(alpha, output_type=output_type)
    192 
    193 

~\miniconda3\lib\site-packages\outliers\smirnov_grubbs.py in run(self, alpha, output_type)
    120 
    121         while True:
--> 122             outlier_index = self._test_once(data, alpha)
    123             if outlier_index is None:
    124                 break

~\miniconda3\lib\site-packages\outliers\smirnov_grubbs.py in _test_once(self, data, alpha)
    101         :return: the index of the outlier if one if found; None otherwise
    102         """
--> 103         target_index, value = self._target(data)
    104 
    105         g = value / data.std()

~\miniconda3\lib\site-packages\outliers\smirnov_grubbs.py in _target(self, data)
    152         relative_values = abs(data - data.mean())
    153         index = relative_values.argmax()
--> 154         value = relative_values[index]
    155         return index, value
    156 

~\miniconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    869         key = com.apply_if_callable(key, self)
    870         try:
--> 871             result = self.index.get_value(self, key)
    872 
    873             if not is_scalar(result):

~\miniconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   4402         k = self._convert_scalar_indexer(k, kind="getitem")
   4403         try:
-> 4404             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4405         except KeyError as e1:
   4406             if len(self) > 0 and (self.holds_integer() or self.is_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: 0

@gitPrinz
Copy link

gitPrinz commented Feb 4, 2021

I switched the first two numbers, so it doesn't fail:

data = pd.Series([8, 1, 9, 10, 9])

But named rows

data = pd.Series({'a': 8, 'b': 1, 'c': 9, 'd': 10, 'e': 9})

give

Traceback (most recent call last):
  File "xxx\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-37-4ecfcc731723>", line 1, in <module>
    grubbs.test(data, alpha=0.05)
  File "xxx\lib\site-packages\outliers\smirnov_grubbs.py", line 243, in test
    return two_sided_test(data, alpha)
  File "xxx\lib\site-packages\outliers\smirnov_grubbs.py", line 207, in two_sided_test
    return _two_sided_test(data, alpha, OutputType.DATA)
  File "xxx\lib\site-packages\outliers\smirnov_grubbs.py", line 195, in _two_sided_test
    return _test(TwoSidedGrubbsTest, data, alpha, output_type)
  File "xxx\lib\site-packages\outliers\smirnov_grubbs.py", line 191, in _test
    return test_class(data).run(alpha, output_type=output_type)
  File "xxx\lib\site-packages\outliers\smirnov_grubbs.py", line 127, in run
    data = self._delete_item(data, outlier_index)
  File "xxx\lib\site-packages\outliers\smirnov_grubbs.py", line 59, in _delete_item
    return data.drop(index)
  File "xxx\lib\site-packages\pandas\core\series.py", line 4139, in drop
    errors=errors,
  File "xxx\lib\site-packages\pandas\core\generic.py", line 3936, in drop
    obj = obj._drop_axis(labels, axis, level=level, errors=errors)
  File "xxx\lib\site-packages\pandas\core\generic.py", line 3970, in _drop_axis
    new_axis = axis.drop(labels, errors=errors)
  File "xxx\lib\site-packages\pandas\core\indexes\base.py", line 5017, in drop
    raise KeyError(f"{labels[mask]} not found in axis")
KeyError: '[1] not found in axis'

Maybe this is related?

@magiccpp
Copy link

Fixed, please check this pull request.
#11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants