Skip to content

BUG: TypeError when printing dataframe with numpy.record #55011

Open
@rvernica

Description

@rvernica

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
ar = np.array([((255, 0),), ((255, 1),), ((255, 2),)], dtype=[('x', [('null', 'u1'), ('val', '<i8')])])
pd.DataFrame.from_records(ar)

Issue Description

Very similar to #48526

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.local/lib/python3.11/site-packages/pandas/core/frame.py", line 1093, in __repr__
    return self.to_string(**repr_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/core/frame.py", line 1270, in to_string
    return fmt.DataFrameRenderer(formatter).to_string(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/io/formats/format.py", line 1134, in to_string
    string = string_formatter.to_string()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/io/formats/string.py", line 30, in to_string
    text = self._get_string_representation()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/io/formats/string.py", line 45, in _get_string_representation
    strcols = self._get_strcols()
              ^^^^^^^^^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/io/formats/string.py", line 36, in _get_strcols
    strcols = self.fmt.get_strcols()
              ^^^^^^^^^^^^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/io/formats/format.py", line 615, in get_strcols
    strcols = self._get_strcols_without_index()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/io/formats/format.py", line 881, in _get_strcols_without_index
    fmt_values = self.format_col(i)
                 ^^^^^^^^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/io/formats/format.py", line 895, in format_col
    return format_array(
           ^^^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/io/formats/format.py", line 1330, in format_array
    return fmt_obj.get_result()
           ^^^^^^^^^^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/io/formats/format.py", line 1363, in get_result
    fmt_values = self._format_strings()
                 ^^^^^^^^^^^^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/io/formats/format.py", line 1421, in _format_strings
    & np.all(notna(vals), axis=tuple(range(1, len(vals.shape))))
             ^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/core/dtypes/missing.py", line 429, in notna
    res = isna(obj)
          ^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/core/dtypes/missing.py", line 183, in isna
    return _isna(obj)
           ^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/core/dtypes/missing.py", line 212, in _isna
    return _isna_array(obj, inf_as_na=inf_as_na)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.local/lib/python3.11/site-packages/pandas/core/dtypes/missing.py", line 302, in _isna_array
    result = np.isnan(values)
             ^^^^^^^^^^^^^^^^
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Expected Behavior

   x
0  (255, 0)
1  (255, 1)
2  (255, 2)

Installed Versions

/usr/lib/python3.11/site-packages/_distutils_hack/__init__.py:33: UserWarning: Setuptools is replacing distutils.
  warnings.warn("Setuptools is replacing distutils.")

INSTALLED VERSIONS
------------------
commit              : 4b456e23278b2e92b13e5c2bd2a5e621a8057bd1
python              : 3.11.4.final.0
python-bits         : 64
OS                  : Linux
OS-release          : 6.4.12-200.fc38.x86_64
Version             : #1 SMP PREEMPT_DYNAMIC Wed Aug 23 17:46:49 UTC 2023
machine             : x86_64
processor           : 
byteorder           : little
LC_ALL              : en_US.UTF-8
LANG                : en_US.UTF-8
LOCALE              : en_US.UTF-8

pandas              : 2.2.0dev0+171.g4b456e2327
numpy               : 1.25.2
pytz                : 2023.3.post1
dateutil            : 2.8.2
setuptools          : 65.5.1
pip                 : 23.2.1
Cython              : None
pytest              : 7.4.1
hypothesis          : None
sphinx              : None
blosc               : None
feather             : None
xlsxwriter          : None
lxml.etree          : 4.9.2
html5lib            : None
pymysql             : None
psycopg2            : None
jinja2              : 3.1.2
IPython             : 8.15.0
pandas_datareader   : None
bs4                 : 4.12.2
bottleneck          : None
dataframe-api-compat: None
fastparquet         : None
fsspec              : None
gcsfs               : None
matplotlib          : None
numba               : None
numexpr             : None
odfpy               : None
openpyxl            : None
pandas_gbq          : None
pyarrow             : 11.0.0
pyreadstat          : None
pyxlsb              : None
s3fs                : None
scipy               : None
sqlalchemy          : None
tables              : None
tabulate            : None
xarray              : None
xlrd                : None
zstandard           : None
tzdata              : 2023.3
qtpy                : None
pyqt5               : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugError ReportingIncorrect or improved errors from pandas

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions