Skip to content

Should ExtensionArray.take accept scalar inputs? #22215

Closed
@TomAugspurger

Description

@TomAugspurger

ndarray.take accepts scalars, and returns a scalar. We should probably make that part of the interface, or document that we don't support it.

In [18]: np.array([1, 2]).take(0)
Out[18]: 1

Categorical currently returns an invalid categorical:

In [19]: res = pd.Categorical([0, 1]).take(0)

In [20]: type(res)
Out[20]: pandas.core.arrays.categorical.Categorical
In [21]: res
Out[21]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/Envs/pandas-dev/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~/Envs/pandas-dev/lib/python3.6/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    398                         if cls is not object \
    399                                 and callable(cls.__dict__.get('__repr__')):
--> 400                             return _repr_pprint(obj, self, cycle)
    401
    402             return _default_pprint(obj, self, cycle)

~/Envs/pandas-dev/lib/python3.6/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    693     """A pprint that just redirects to the normal repr function."""
    694     # Find newlines and replace them with p.break_()
--> 695     output = repr(obj)
    696     for idx,output_line in enumerate(output.splitlines()):
    697         if idx:

~/sandbox/pandas/pandas/core/base.py in __repr__(self)
     80         Yields Bytestring in Py2, Unicode String in py3.
     81         """
---> 82         return str(self)
     83
     84

~/sandbox/pandas/pandas/core/base.py in __str__(self)
     59
     60         if compat.PY3:
---> 61             return self.__unicode__()
     62         return self.__bytes__()
     63

~/sandbox/pandas/pandas/core/arrays/categorical.py in __unicode__(self)
   1942         """ Unicode representation. """
   1943         _maxlen = 10
-> 1944         if len(self._codes) > _maxlen:
   1945             result = self._tidy_repr(_maxlen)
   1946         elif len(self._codes) > 0:

TypeError: len() of unsized object
  • IntervalArray.take fails on take
  • SparseArray allows it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions