You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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_castreturn 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 astypereturnself.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_nansafereturn 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_castraiseTypeError(
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:
>>>importpandasaspd>>>df=pd.DataFrame({'a':[123,'123']})
>>>df["a"].astype("Int64")
01231123Name: 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 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
output:
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")
(lowercaseint
), we get no error and things seem to work just fineExpected Behavior
I expect converting to
Int64
to work the same, as it relates to strings, asint64
. That is, I expect the example to give: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
The text was updated successfully, but these errors were encountered: