Skip to content

BUG: Fixed DataFrame.__repr__ Type Error when column dtype is np.record (GH 48526) #48637

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

Merged
merged 17 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ Conversion
^^^^^^^^^^
- Bug in :func:`DataFrame.style.to_latex` and :func:`DataFrame.style.to_html` if the DataFrame contains integers with more digits than can be represented by floating point double precision (:issue:`52272`)
- Bug in :meth:`ArrowDtype.numpy_dtype` returning nanosecond units for non-nanosecond ``pyarrow.timestamp`` and ``pyarrow.duration`` types (:issue:`51800`)
- Bug in :meth:`DataFrame.__repr__` incorrectly raising a ``TypeError`` when the dtype of a column is ``np.record`` (:issue:`48526`)
- Bug in :meth:`DataFrame.info` raising ``ValueError`` when ``use_numba`` is set (:issue:`51922`)
-

Expand Down
31 changes: 31 additions & 0 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ def _isna_array(values: ArrayLike, inf_as_na: bool = False):
# "Union[ndarray[Any, Any], ExtensionArraySupportsAnyAll]", variable has
# type "ndarray[Any, dtype[bool_]]")
result = values.isna() # type: ignore[assignment]
elif isinstance(values, np.recarray):
# GH 48526
result = _isna_recarray_dtype(values, inf_as_na=inf_as_na)
elif is_string_or_object_np_dtype(values.dtype):
result = _isna_string_dtype(values, inf_as_na=inf_as_na)
elif dtype.kind in "mM":
Expand Down Expand Up @@ -314,6 +317,34 @@ def _isna_string_dtype(values: np.ndarray, inf_as_na: bool) -> npt.NDArray[np.bo
return result


def _has_record_inf_value(record_as_array: np.ndarray) -> np.bool_:
is_inf_in_record = np.zeros(len(record_as_array), dtype=bool)
for i, value in enumerate(record_as_array):
is_element_inf = False
try:
is_element_inf = np.isinf(value)
except TypeError:
is_element_inf = False
is_inf_in_record[i] = is_element_inf

return np.any(is_inf_in_record)


def _isna_recarray_dtype(values: np.recarray, inf_as_na: bool) -> npt.NDArray[np.bool_]:
result = np.zeros(values.shape, dtype=bool)
for i, record in enumerate(values):
record_as_array = np.array(record.tolist())
does_record_contain_nan = isna_all(record_as_array)
does_record_contain_inf = False
if inf_as_na:
does_record_contain_inf = bool(_has_record_inf_value(record_as_array))
result[i] = np.any(
np.logical_or(does_record_contain_nan, does_record_contain_inf)
)

return result


@overload
def notna(obj: Scalar) -> bool:
...
Expand Down
65 changes: 65 additions & 0 deletions pandas/tests/frame/test_repr_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,71 @@ def test_datetime64tz_slice_non_truncate(self):
result = repr(df)
assert result == expected

def test_to_records_no_typeerror_in_repr(self):
# GH 48526
df = DataFrame([["a", "b"], ["c", "d"], ["e", "f"]], columns=["left", "right"])
df["record"] = df[["left", "right"]].to_records()
expected = """ left right record
0 a b [0, a, b]
1 c d [1, c, d]
2 e f [2, e, f]"""
result = repr(df)
assert result == expected

def test_to_records_with_na_record_value(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have tests for inf as well here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mroeschke I added 2 tests for inf, is the expected result what you would also expect?

# GH 48526
df = DataFrame(
[["a", np.nan], ["c", "d"], ["e", "f"]], columns=["left", "right"]
)
df["record"] = df[["left", "right"]].to_records()
expected = """ left right record
0 a NaN [0, a, nan]
1 c d [1, c, d]
2 e f [2, e, f]"""
result = repr(df)
assert result == expected

def test_to_records_with_na_record(self):
# GH 48526
df = DataFrame(
[["a", "b"], [np.nan, np.nan], ["e", "f"]], columns=[np.nan, "right"]
)
df["record"] = df[[np.nan, "right"]].to_records()
expected = """ NaN right record
0 a b [0, a, b]
1 NaN NaN [1, nan, nan]
2 e f [2, e, f]"""
result = repr(df)
assert result == expected

def test_to_records_with_inf_as_na_record(self):
# GH 48526
with option_context("use_inf_as_na", True):
df = DataFrame(
[[np.inf, "b"], [np.nan, np.nan], ["e", "f"]], columns=[np.nan, np.inf]
)
df["record"] = df[[np.nan, np.inf]].to_records()
expected = """ NaN inf record
0 NaN b [0, inf, b]
1 NaN NaN [1, nan, nan]
2 e f [2, e, f]"""
result = repr(df)
assert result == expected

def test_to_records_with_inf_record(self):
# GH 48526
with option_context("use_inf_as_na", False):
df = DataFrame(
[[np.inf, "b"], [np.nan, np.nan], ["e", "f"]], columns=[np.nan, np.inf]
)
df["record"] = df[[np.nan, np.inf]].to_records()
expected = """ NaN inf record
0 inf b [0, inf, b]
1 NaN NaN [1, nan, nan]
2 e f [2, e, f]"""
result = repr(df)
assert result == expected

def test_masked_ea_with_formatter(self):
# GH#39336
df = DataFrame(
Expand Down