Quantipy / quantipy

Python for people data
MIT License
66 stars 14 forks source link

DataSet.first_responses fails if no n-first mention is found in data #1323

Open AndersFreund opened 4 years ago

AndersFreund commented 4 years ago

If you use DataSet.first_responses with it default setting n=3 on a variable only having max 2 answers then DataSet.first_responses will fail doing the third mention.

 self.qds.first_responses(variable, n=n, others='others')
File "<decorator-gen-355>", line 2, in first_responses
File "/usr/local/lib/python2.7/site-packages/quantipy/core/tools/qp_decorators.py", line 143, in _deco
return func(*args, **kwargs)
File "<decorator-gen-853>", line 2, in first_responses
File "/usr/local/lib/python2.7/site-packages/quantipy/core/tools/qp_decorators.py", line 63, in _var_in_ds
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/quantipy/core/dataset.py", line 4109, in first_responses
o_string = self[name].str.split(';', n=n, expand=True)[n]
File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 2059, in __getitem__
return self._getitem_column(key)
File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 2066, in _getitem_column
return self._get_item_cache(key)
File "/usr/local/lib/python2.7/site-packages/pandas/core/generic.py", line 1386, in _get_item_cache
values = self._data.get(item)
File "/usr/local/lib/python2.7/site-packages/pandas/core/internals.py", line 3543, in get
loc = self.items.get_loc(item)
File "/usr/local/lib/python2.7/site-packages/pandas/indexes/base.py", line 2136, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas/index.c:4145)
File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:4009)
File "pandas/src/hashtable_class_helper.pxi", line 404, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8146)
File "pandas/src/hashtable_class_helper.pxi", line 410, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8090)
KeyError: 3