Skip to content

MultiIndex.isin should handle input dims better #20252

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

Open
TomAugspurger opened this issue Mar 10, 2018 · 0 comments
Open

MultiIndex.isin should handle input dims better #20252

TomAugspurger opened this issue Mar 10, 2018 · 0 comments
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas isin isin method MultiIndex

Comments

@TomAugspurger
Copy link
Contributor

This should either be all False, or throw with a better error message about "did you forget level"?

In [54]: midx = pd.MultiIndex.from_arrays([[1, 2, 3], ['a', 'b', 'c']], names =  ('number', 'letter'))

In [55]: midx.isin([1, 2])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-55-c96f0bd8505a> in <module>()
----> 1 midx.isin([1, 2])

~/sandbox/pandas-ip/pandas/pandas/core/indexes/multi.py in isin(self, values, level)
   2902         if level is None:
   2903             values = MultiIndex.from_tuples(values,
-> 2904                                             names=self.names).values
   2905             return algos.isin(self.values, values)
   2906         else:

~/sandbox/pandas-ip/pandas/pandas/core/indexes/multi.py in from_tuples(cls, tuples, sortorder, names)
   1324             arrays = list(lib.tuples_to_object_array(tuples).T)
   1325         elif isinstance(tuples, list):
-> 1326             arrays = list(lib.to_object_array_tuples(tuples).T)
   1327         else:
   1328             arrays = lzip(*tuples)

~/sandbox/pandas-ip/pandas/pandas/_libs/src/inference.pyx in pandas._libs.lib.to_object_array_tuples()
   1557     k = 0
   1558     for i from 0 <= i < n:
-> 1559         tmp = len(rows[i])
   1560         if tmp > k:
   1561             k = tmp

TypeError: object of type 'int' has no len()

In [56]: midx = pd.MultiIndex.from_arrays([[1, 2, 3], ['a', 'b', 'c']], names =  ('number', 'letter'))

In [57]: midx.isin([1, 2])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-57-c96f0bd8505a> in <module>()
----> 1 midx.isin([1, 2])

~/sandbox/pandas-ip/pandas/pandas/core/indexes/multi.py in isin(self, values, level)
   2902         if level is None:
   2903             values = MultiIndex.from_tuples(values,
-> 2904                                             names=self.names).values
   2905             return algos.isin(self.values, values)
   2906         else:

~/sandbox/pandas-ip/pandas/pandas/core/indexes/multi.py in from_tuples(cls, tuples, sortorder, names)
   1324             arrays = list(lib.tuples_to_object_array(tuples).T)
   1325         elif isinstance(tuples, list):
-> 1326             arrays = list(lib.to_object_array_tuples(tuples).T)
   1327         else:
   1328             arrays = lzip(*tuples)

~/sandbox/pandas-ip/pandas/pandas/_libs/src/inference.pyx in pandas._libs.lib.to_object_array_tuples()
   1557     k = 0
   1558     for i from 0 <= i < n:
-> 1559         tmp = len(rows[i])
   1560         if tmp > k:
   1561             k = tmp

TypeError: object of type 'int' has no len()
In [58]: midx.isin([(1, 'a')])
Out[58]: array([ True, False, False], dtype=bool)

works fine.

@jbrockmendel jbrockmendel added the isin isin method label Oct 30, 2020
@mroeschke mroeschke added Enhancement Error Reporting Incorrect or improved errors from pandas labels Jun 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas isin isin method MultiIndex
Projects
None yet
Development

No branches or pull requests

3 participants