-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 14 commits
bd811e2
a454c0f
34109e3
8204a58
25a9ab1
2ecf9a3
dd837ce
076814f
3a15ab1
961ebf0
4fb9443
923d29d
fdcdb57
5c4c29f
8275116
396172c
145699f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
from functools import partial | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
overload, | ||
) | ||
|
||
|
@@ -284,6 +285,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": | ||
|
@@ -315,6 +319,35 @@ def _isna_string_dtype(values: np.ndarray, inf_as_na: bool) -> npt.NDArray[np.bo | |
return result | ||
|
||
|
||
def _check_record_value(element: Any, inf_as_na: bool) -> np.bool_: | ||
is_element_nan = False | ||
if element != element: | ||
is_element_nan = True | ||
|
||
is_element_inf = False | ||
if inf_as_na: | ||
try: | ||
if np.isinf(element): | ||
is_element_inf = True | ||
except TypeError: | ||
is_element_inf = False | ||
|
||
return np.any(np.logical_or(is_element_nan, is_element_inf)) | ||
|
||
|
||
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): | ||
does_record_contain_nan = np.zeros(len(record), dtype=bool) | ||
for j, element in enumerate(record): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mroeschke I used isnan_all for the record and I rewrote a little bit the isinf part but I cannot use |
||
does_record_contain_nan[j] = _check_record_value( | ||
element, inf_as_na=inf_as_na | ||
) | ||
result[i] = np.any(does_record_contain_nan) | ||
|
||
return result | ||
|
||
|
||
@overload | ||
def notna(obj: Scalar) -> bool: | ||
... | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
Timestamp, | ||
date_range, | ||
option_context, | ||
options, | ||
period_range, | ||
) | ||
import pandas._testing as tm | ||
|
@@ -361,6 +362,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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to have tests for inf as well here There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
options.mode.use_inf_as_na = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
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 | ||
options.mode.use_inf_as_na = False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
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( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this to 2.1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course, done