Skip to content

Commit

Permalink
Backport PR #54894 on branch 2.1.x (MAINT: Reflect changes from `nump…
Browse files Browse the repository at this point in the history
…y` namespace refactor Part 5) (#54905)

Backport PR #54894: MAINT: Reflect changes from `numpy` namespace refactor Part 5

Co-authored-by: Mateusz Sokół <8431159+mtsokol@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and mtsokol committed Aug 31, 2023
1 parent c85679c commit c1353e9
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 10 deletions.
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 comments on commit c1353e9

Please sign in to comment.