Skip to content

Commit ec08728

Browse files
committed
More fixes found by typing checks (or working around them)
1 parent 404827b commit ec08728

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

pandas/core/arrays/categorical.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,10 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
581581
elif len(self.codes) == 0 or len(self.categories) == 0:
582582
# For NumPy 1.x compatibility we cannot use copy=None. And
583583
# `copy=False` has the meaning of `copy=None` here:
584-
asarray_func = np.array if copy else np.asarray
585-
result = asarray_func(
586-
self,
587-
dtype=dtype,
588-
)
584+
if not copy:
585+
result = np.asarray(self, dtype=dtype)
586+
else:
587+
result = np.array(self, dtype=dtype)
589588

590589
else:
591590
# GH8628 (PERF): astype category codes instead of astyping array
@@ -1693,16 +1692,15 @@ def __array__(
16931692
"Unable to avoid copy while creating an array as requested."
16941693
)
16951694

1696-
# TODO: using asarray_func because NumPy 1.x doesn't support copy=None
1697-
asarray_func = np.asarray if copy is None else np.array
1698-
16991695
ret = take_nd(self.categories._values, self._codes)
1700-
if dtype and np.dtype(dtype) != self.categories.dtype:
1701-
return asarray_func(ret, dtype)
17021696
# When we're a Categorical[ExtensionArray], like Interval,
17031697
# we need to ensure __array__ gets all the way to an
17041698
# ndarray.
1705-
return asarray_func(ret)
1699+
1700+
if copy is None:
1701+
# Branch required since copy=None is not defined on 1.x
1702+
return np.asarray(ret, dtype=dtype)
1703+
return np.array(ret, dtype=dtype)
17061704

17071705
def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
17081706
# for binary ops, use our custom dunder methods

pandas/core/indexes/multi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ def __array__(self, dtype=None, copy=None) -> np.ndarray:
13931393
"""the array interface, return my values"""
13941394
if copy is True:
13951395
# Note: branch avoids `copy=None` for NumPy 1.x support
1396-
return np.asarray(self.values, dtype=dtype, copy=copy)
1396+
return np.array(self.values, dtype=dtype, copy=copy)
13971397
return self.values
13981398

13991399
def view(self, cls=None) -> Self:

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ def __array__(
883883
# Note: branch avoids `copy=None` for NumPy 1.x support
884884
arr = np.asarray(values, dtype=dtype)
885885
else:
886-
arr = np.asarray(values, dtype=dtype, copy=copy)
886+
arr = np.array(values, dtype=dtype, copy=copy)
887887

888888
if copy is True:
889889
return arr

0 commit comments

Comments
 (0)