BUG:to_numpy() returns numpy array with Object dtype, for multiple columns with the same dtype ("Float64" or "float64[pyarrow]"). #60038
Labels
Bug
ExtensionArray
Extending pandas with custom dtypes or arrays.
PDEP missing values
Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint
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
Issue Description
The array returned by the
to_numpy()
method has a dtype that is not consistent with the documentation, and in my interpretation is a bug.When using the
to_numpy()
method on a DataFrame with 2 or more columns with the same dtype the returned numpy array has dtypeObject
.This occurs when the columns are backed by
numpy_nullable
orpyarrow
dtype backends.For example, with 2 columns
['a', 'b']
with dtypefloat64[pyarrow]
:df[['a', 'b']].to_numpy()
- the dtype of thenumpy
array is of typeObject
df[['a']].to_numpy()
- the dtype of thenumpy
array is of typenp.float64
The documentation states:
The observed behaviour is in contradiction with this - we get the expected
numpy
dtype for a single column, but anObject
dtype when called with multiple columns (of the same dtype)Expected Behavior
When columns
'a'
and'b'
have the same dtype we would expect:And ideally when columns
'a'
and'b'
have the same dtype, of eitherFloat64
orfloat64[pyarrow]
Installed Versions
INSTALLED VERSIONS
commit : 0691c5c
python : 3.12.3
python-bits : 64
OS : Darwin
OS-release : 23.6.0
Version : Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:46 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6031
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_IE.UTF-8
LOCALE : en_IE.UTF-8
pandas : 2.2.3
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
pip : 24.2
Cython : 3.0.10
sphinx : 8.0.2
IPython : 8.26.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : 1.4.0
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.6.1
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.4
lxml.etree : None
matplotlib : 3.9.0
numba : 0.60.0
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 16.1.0
pyreadstat : None
pytest : 8.2.2
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : None
xarray : 2024.6.0
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: