8000 Backport PR #54894 on branch 2.1.x (MAINT: Reflect changes from `numpy` namespace refactor Part 5) by meeseeksmachine · Pull Request #54905 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Backport PR #54894 on branch 2.1.x (MAINT: Reflect changes from numpy namespace refactor Part 5) #54905

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
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
6 changes: 4 additions & 2 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ 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):
elif isinstance(values, np.rec.recarray):
# GH 48526
result = _isna_recarray_dtype(values, inf_as_na=inf_as_na)
elif is_string_or_object_np_dtype(values.dtype):
Expand Down Expand Up @@ -332,7 +332,9 @@ def _has_record_inf_value(record_as_array: np.ndarray) -> np.bool_:
return np.any(is_inf_in_record)


def _isna_recarray_dtype(values: np.recarray, inf_as_na: bool) -> npt.NDArray[np.bool_]:
def _isna_recarray_dtype(
values: np.rec.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())
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2402,7 +2402,7 @@ def maybe_reorder(

def to_records(
self, index: bool = True, column_dtypes=None, index_dtypes=None
) -> np.recarray:
) -> np.rec.recarray:
"""
Convert DataFrame to a NumPy record array.

Expand All @@ -2427,15 +2427,15 @@ def to_records(

Returns
-------
numpy.recarray
numpy.rec.recarray
NumPy ndarray with the DataFrame labels as fields and each row
of the DataFrame as entries.

See Also
--------
DataFrame.from_records: Convert structured or record ndarray
to DataFrame.
numpy.recarray: An ndarray that allows field access using
numpy.rec.recarray: An ndarray that allows field access using
attributes, analogous to typed columns in a
spreadsheet.

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def arrays_to_mgr(


def rec_array_to_mgr(
data: np.recarray | np.ndarray,
data: np.rec.recarray | np.ndarray,
index,
columns,
dtype: DtypeObj | None,
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2961,7 +2961,7 @@ def _convert_strls(self, data: DataFrame) -> DataFrame:
"""No-op, future compatibility"""
return data

def _prepare_data(self) -> np.recarray:
def _prepare_data(self) -> np.rec.recarray:
data = self.data
typlist = self.typlist
convert_dates = self._convert_dates
Expand Down Expand Up @@ -2993,7 +2993,7 @@ def _prepare_data(self) -> np.recarray:

return data.to_records(index=False, column_dtypes=dtypes)

def _write_data(self, records: np.recarray) -> None:
def _write_data(self, records: np.rec.recarray) -> None:
self._write_bytes(records.tobytes())

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/constructors/test_from_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_from_records_sequencelike(self):
tup.extend(b.iloc[i].values)
tuples.append(tuple(tup))

recarray = np.array(tuples, dtype=dtypes).view(np.recarray)
recarray = np.array(tuples, dtype=dtypes).view(np.rec.recarray)
recarray2 = df.to_records()
lists = [list(x) for x in tuples]

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_to_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def test_to_records_dtype(self, kwargs, expected):
# see GH#18146
df = DataFrame({"A": [1, 2], "B": [0.2, 1.5], "C": ["a", "bc"]})

if not isinstance(expected, np.recarray):
if not isinstance(expected, np.rec.recarray):
with pytest.raises(expected[0], match=expected[1]):
df.to_records(**kwargs)
else:
Expand Down
0