Skip to content

BUG: strings fails to convert to nullable Integer dtype in dataframe/series #58496

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
2 of 3 tasks
scotscotmcc opened this issue Apr 30, 2024 · 2 comments
Closed
2 of 3 tasks
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@scotscotmcc
Copy link

scotscotmcc commented Apr 30, 2024

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame({'a':[123,'123']})
df["a"].astype("Int64")

output:

Traceback (most recent call last):
  File "C:\Users\MCCULSXCQY\Scripts\Python\test_pd\.venv\Lib\site-packages\pandas\core\arrays\integer.py", line 53, in _safe_cast
    return values.astype(dtype, casting="safe", copy=copy)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Cannot cast array data 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 "c:\Users\MCCULSXCQY\Scripts\Python\test_pd\test.py", line 4, in <module>
    df["a"].astype("Int64")
  File "C:\Users\MCCULSXCQY\Scripts\Python\test_pd\.venv\Lib\site-packages\pandas\core\generic.py", line 6643, in astype
    new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MCCULSXCQY\Scripts\Python\test_pd\.venv\Lib\site-packages\pandas\core\internals\managers.py", line 430, in astype
    return self.apply(
           ^^^^^^^^^^^
  File "C:\Users\MCCULSXCQY\Scripts\Python\test_pd\.venv\Lib\site-packages\pandas\core\internals\managers.py", line 363, in apply
    applied = getattr(b, f)(**kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MCCULSXCQY\Scripts\Python\test_pd\.venv\Lib\site-packages\pandas\core\internals\blocks.py", line 758, in astype
    new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MCCULSXCQY\Scripts\Python\test_pd\.venv\Lib\site-packages\pandas\core\dtypes\astype.py", line 237, in astype_array_safe
    new_values = astype_array(values, dtype, copy=copy)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MCCULSXCQY\Scripts\Python\test_pd\.venv\Lib\site-packages\pandas\core\dtypes\astype.py", line 182, in astype_array
    values = _astype_nansafe(values, dtype, copy=copy)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MCCULSXCQY\Scripts\Python\test_pd\.venv\Lib\site-packages\pandas\core\dtypes\astype.py", line 80, in _astype_nansafe
    return dtype.construct_array_type()._from_sequence(arr, dtype=dtype, copy=copy)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MCCULSXCQY\Scripts\Python\test_pd\.venv\Lib\site-packages\pandas\core\arrays\masked.py", line 152, in _from_sequence
    values, mask = cls._coerce_to_array(scalars, dtype=dtype, copy=copy)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MCCULSXCQY\Scripts\Python\test_pd\.venv\Lib\site-packages\pandas\core\arrays\numeric.py", line 272, in _coerce_to_array
    values, mask, _, _ = _coerce_to_data_and_mask(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MCCULSXCQY\Scripts\Python\test_pd\.venv\Lib\site-packages\pandas\core\arrays\numeric.py", line 229, in _coerce_to_data_and_mask
    values = dtype_cls._safe_cast(values, dtype, copy=False)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MCCULSXCQY\Scripts\Python\test_pd\.venv\Lib\site-packages\pandas\core\arrays\integer.py", line 59, in _safe_cast
    raise TypeError(
TypeError: cannot safely cast non-equivalent object to int64

Issue Description

In the example, we get TypeError: cannot safely cast non-equivalent object to int64.

However, if in the example we do df["a"].astype("int64") (lowercase int), we get no error and things seem to work just fine

Expected Behavior

I expect converting to Int64 to work the same, as it relates to strings, as int64. That is, I expect the example to give:

>>> import pandas as pd
>>> df = pd.DataFrame({'a':[123,'123']})
>>> df["a"].astype("Int64")
0    123
1    123
Name: a, dtype: Int64

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.12.1.final.0
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 154 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_United States.1252

pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : None
pip : 24.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@scotscotmcc scotscotmcc added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 30, 2024
@Aloqeely
Copy link
Member

Thanks for the report! This is a duplicate of #34460, but feel free to submit a PR to fix this

@mroeschke
Copy link
Member

Closing as a duplicate of #34460

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

3 participants