Skip to content

BUG: select_dtypes does not work properly with numpy.ndarray #60092

Closed
@fjossandon

Description

@fjossandon

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
import numpy as np

table_df = pd.DataFrame({"id": [1, 2, 3], "dest_id": ["var-foo-1", "var-foo-2", "baz-taz-1"], "arr_col": [np.array([1, 2]), np.array([3, 4]), np.array([4, 5])]})

print(type(table_df["dest_id"][0]), type(table_df["arr_col"][0]))

print(table_df.select_dtypes(include=[np.ndarray]).columns)

Issue Description

I work with several dataframes, which occasionally have array columns. I was using select_dtypes to search for those columns containing Array type to manipulate them, but the function also returns the string columns, and my code crashes when it tries to apply the array function to the string column.

I was working with pandas 2.2.2 / numpy 1.26.2 when this happened, but I made a new environment and upgraded to the latest versions and it still happened.

Expected Behavior

This is the current output:

>>> print(type(table_df["dest_id"][0]), type(table_df["arr_col"][0]))
<class 'str'> <class 'numpy.ndarray'>
>>> 
>>> print(table_df.select_dtypes(include=[np.ndarray]).columns)
Index(['dest_id', 'arr_col'], dtype='object')

I would expect that only arr_col is returned with select_dtypes when using include=[np.ndarray].

Installed Versions

INSTALLED VERSIONS

commit : 68d9dca
python : 3.11.6
python-bits : 64
OS : Linux
OS-release : 6.5.0-44-generic
Version : #44-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 7 15:10:09 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 3.0.0.dev0+1580.g68d9dcab5b
numpy : 2.1.2
dateutil : 2.9.0.post0
pip : 23.2
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : 5.2.2
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pytz : 2024.1
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions