Skip to content
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

Mixed object array with astype to Extension Array throws TypeError #34460

Open
WillAyd opened this issue May 29, 2020 · 1 comment
Open

Mixed object array with astype to Extension Array throws TypeError #34460

WillAyd opened this issue May 29, 2020 · 1 comment
Labels
Astype Bug Dtype Conversions Unexpected or buggy dtype conversions NA - MaskedArrays Related to pd.NA and nullable extension arrays

Comments

@WillAyd
Copy link
Member

WillAyd commented May 29, 2020

This works

>>> pd.Series([1, "1"]).astype(int)
0    1
1    1
dtype: int64

This doesn't

>>> pd.Series([1, "1"]).astype("Int64")
Traceback (most recent call last):
  File "/Users/willayd/clones/pandas/pandas/core/arrays/integer.py", line 165, in safe_cast
    return values.astype(dtype, casting="safe", copy=copy)
TypeError: Cannot cast array from dtype('O') to dtype('int64') according to the rule 'safe'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/willayd/clones/pandas/pandas/core/generic.py", line 5635, in astype
    new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors,)
  File "/Users/willayd/clones/pandas/pandas/core/internals/managers.py", line 573, in astype
    return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
  File "/Users/willayd/clones/pandas/pandas/core/internals/managers.py", line 402, in apply
    applied = getattr(b, f)(**kwargs)
  File "/Users/willayd/clones/pandas/pandas/core/internals/blocks.py", line 581, in astype
    values = astype_nansafe(vals1d, dtype, copy=True)
  File "/Users/willayd/clones/pandas/pandas/core/dtypes/cast.py", line 912, in astype_nansafe
    return dtype.construct_array_type()._from_sequence(arr, dtype=dtype, copy=copy)
  File "/Users/willayd/clones/pandas/pandas/core/arrays/integer.py", line 362, in _from_sequence
    return integer_array(scalars, dtype=dtype, copy=copy)
  File "/Users/willayd/clones/pandas/pandas/core/arrays/integer.py", line 153, in integer_array
    values, mask = coerce_to_array(values, dtype=dtype, copy=copy)
  File "/Users/willayd/clones/pandas/pandas/core/arrays/integer.py", line 270, in coerce_to_array
    values = safe_cast(values, dtype, copy=False)
  File "/Users/willayd/clones/pandas/pandas/core/arrays/integer.py", line 172, in safe_cast
    raise TypeError(
TypeError: cannot safely cast non-equivalent object to int64
@WillAyd WillAyd added Bug Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays. labels May 29, 2020
@liebergk
Copy link

liebergk commented Jun 2, 2020

Hi there, I'm new here looking for a first issue and wondering if this might be a good one.

Is the desired behavior that the second example converting explicitly to Int64 should succeed like the first one does?

Curiously (to me, a python non-expert) the following with lowercase "int64" does work in the console:

pd.Series([1, "1"]).astype("int64")

@jbrockmendel jbrockmendel added NA - MaskedArrays Related to pd.NA and nullable extension arrays and removed ExtensionArray Extending pandas with custom dtypes or arrays. labels Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Astype Bug Dtype Conversions Unexpected or buggy dtype conversions NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

No branches or pull requests

3 participants