Skip to content

Series with MultiIndex "at" function raises "TypeError" #26989

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
ndamclean opened this issue Jun 21, 2019 · 5 comments · Fixed by #32520
Closed

Series with MultiIndex "at" function raises "TypeError" #26989

ndamclean opened this issue Jun 21, 2019 · 5 comments · Fixed by #32520
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Milestone

Comments

@ndamclean
Copy link

Code Sample

# Your code here
import pandas as pd
s = pd.Series(index=pd.MultiIndex.from_tuples([('a', 0)]))
s.loc['a', 0]  # loc function works fine, no complaints here
s.at['a', 0]  # raises TypeError

Stack Trace:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/pandas/core/indexing.py", line 2270, in __getitem__
    return self.obj._get_value(*key, takeable=self._takeable)
TypeError: _get_value() got multiple values for argument 'takeable'

Problem description

I would expect the at function to either return the values given by the indexers or raise a KeyError. In the above example, the index value exists, so at should return the value from the series at that index (nan).

The at function works fine for series with normal indexes. There is nothing in the documentation indicating it should not work for multi-indexes.

Expected Output

nan
nan

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-50-generic
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.2
pytest: 3.4.2
pip: 19.1.1
setuptools: 41.0.1
Cython: 0.29.10
numpy: 1.16.4
scipy: 1.3.0
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2019.1
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@WillAyd
Copy link
Member

WillAyd commented Jun 21, 2019

Makes sense - if you'd like to take a look and submit a PR would certainly be welcome

@WillAyd WillAyd added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Jun 21, 2019
@WillAyd WillAyd added this to the Contributions Welcome milestone Jun 21, 2019
@Seemachopra22
Copy link

try this option:
s.loc['a'].at[0]

@ndamclean
Copy link
Author

@Seemachopra22 That seems like a reasonable workaround, but I still think this is a bug that should be fixed. I'll work on a PR if I have time.

@rhshadrach
Copy link
Member

I stumbled upon the same issue and did a little digging into the code. Please forgive any errors that are to follow - I am quite new to looking at the Pandas internals.

It seems like the issue is line 2087 of this block of code within _ScalarAccessIndexer (similar issues for the getter methods):

pandas/pandas/core/indexing.py

Lines 2085 to 2091 in 844dc4a

if not isinstance(key, tuple):
key = _tuplify(self.ndim, key)
if len(key) != self.ndim:
raise ValueError("Not enough indexers for scalar access (setting)!")
key = list(self._convert_key(key, is_setter=True))
key.append(value)
self.obj._set_value(*key, takeable=self._takeable)

When I run this on a series with a multi index, e.g.

s = pd.DataFrame([['x', 'y', 1], ['z', 'w', 2]], columns=['a', 'b', 'c']).set_index(['a', 'b']).c
s.at['x', 'y'] = 5

self.ndim is 1 whereas key is ('x', 'y'), which leads to raising the value error. What I can't tell is whether self.ndim here should be 2, or if perhaps this check is not correct.

@rhshadrach
Copy link
Member

If self.ndim is the same as pd.Series.ndim, then self.ndim must be 1 and I believe this check can be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants