Skip to content

Series[ExtensionArray].astype(str) failing with "has no attribute ravel" #20578

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

astype(float) and astype(object) are working on an example Series[DecimalArray], but astype(str) is failing (while on the array itself, it works).

This is because we check for string types, and in that case use self.values (which is still an extension array) and not self.get_values(), and afterwards we call .ravel() on the result:

if issubclass(dtype.type,
(compat.text_type, compat.string_types)):
# use native type formatting for datetime/tz/timedelta
if self.is_datelike:
values = self.to_native_types()
# astype formatting
else:
values = self.values
else:
values = self.get_values(dtype=dtype)
# _astype_nansafe works fine with 1-d only
values = astype_nansafe(values.ravel(), dtype, copy=True)

Example:

In [40]: from pandas.tests.extension.decimal.array import DecimalArray, make_data

In [41]: dec_arr = DecimalArray(make_data()[:3])

In [42]: s = pd.Series(dec_arr)

In [43]: s
Out[43]: 
0    0.84287460911336775648550201367470435798168182...
1    0.41192659105851348044780024792999029159545898...
2    0.46823308426791498959573800675570964813232421875
dtype: decimal

In [44]: s.astype(float)
Out[44]: 
0    0.842875
1    0.411927
2    0.468233
dtype: float64

In [45]: s.astype(object)
Out[45]: 
0    0.84287460911336775648550201367470435798168182...
1    0.41192659105851348044780024792999029159545898...
2    0.46823308426791498959573800675570964813232421875
dtype: object

In [47]: s.astype(str)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-47-b17e25b1f3f4> in <module>()
----> 1 s.astype(str)

/home/joris/scipy/pandas/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    175                 else:
    176                     kwargs[new_arg_name] = new_arg_value
--> 177             return func(*args, **kwargs)
    178         return wrapper
    179     return _deprecate_kwarg

/home/joris/scipy/pandas/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
   5001             # else, only a single dtype is given
   5002             new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,
-> 5003                                          **kwargs)
   5004             return self._constructor(new_data).__finalize__(self)
   5005 

/home/joris/scipy/pandas/pandas/core/internals.py in astype(self, dtype, **kwargs)
   3670 
   3671     def astype(self, dtype, **kwargs):
-> 3672         return self.apply('astype', dtype=dtype, **kwargs)
   3673 
   3674     def convert(self, **kwargs):

/home/joris/scipy/pandas/pandas/core/internals.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
   3537 
   3538             kwargs['mgr'] = self
-> 3539             applied = getattr(b, f)(**kwargs)
   3540             result_blocks = _extend_blocks(applied, result_blocks)
   3541 

/home/joris/scipy/pandas/pandas/core/internals.py in astype(self, dtype, copy, errors, values, **kwargs)
    573     def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
    574         return self._astype(dtype, copy=copy, errors=errors, values=values,
--> 575                             **kwargs)
    576 
    577     def _astype(self, dtype, copy=False, errors='raise', values=None,

/home/joris/scipy/pandas/pandas/core/internals.py in _astype(self, dtype, copy, errors, values, klass, mgr, **kwargs)
    662 
    663                 # _astype_nansafe works fine with 1-d only
--> 664                 values = astype_nansafe(values.ravel(), dtype, copy=True)
    665                 values = values.reshape(self.shape)
    666 

AttributeError: 'DecimalArray' object has no attribute 'ravel'

In [49]: dec_arr.astype(str)
Out[49]: 
array(['0.84287460911336775648550201367470435798168182373046875',
       '0.411926591058513480447800247929990291595458984375',
       '0.46823308426791498959573800675570964813232421875'],
      dtype='<U55')

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExtensionArrayExtending pandas with custom dtypes or arrays.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions