Skip to content

Commit 9a18980

Browse files
authored
BUG: incorrect is_array_like checks (#53057)
* BUG: incorrect is_array_like checks * mypy fixup
1 parent a20786a commit 9a18980

File tree

3 files changed

+22
-43
lines changed

3 files changed

+22
-43
lines changed

pandas/_typing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155

156156
RandomState = Union[
157157
int,
158-
ArrayLike,
158+
np.ndarray,
159159
np.random.Generator,
160160
np.random.BitGenerator,
161161
np.random.RandomState,

pandas/core/arrays/arrow/array.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,6 @@
1818
import numpy as np
1919

2020
from pandas._libs import lib
21-
from pandas._typing import (
22-
ArrayLike,
23-
AxisInt,
24-
Dtype,
25-
FillnaOptions,
26-
Iterator,
27-
NpDtype,
28-
PositionalIndexer,
29-
Scalar,
30-
Self,
31-
SortKind,
32-
TakeIndexer,
33-
TimeAmbiguous,
34-
TimeNonexistent,
35-
npt,
36-
)
3721
from pandas.compat import (
3822
pa_version_under7p0,
3923
pa_version_under8p0,
@@ -140,8 +124,22 @@ def floordiv_compat(
140124

141125
if TYPE_CHECKING:
142126
from pandas._typing import (
127+
ArrayLike,
128+
AxisInt,
129+
Dtype,
130+
FillnaOptions,
131+
Iterator,
132+
NpDtype,
143133
NumpySorter,
144134
NumpyValueArrayLike,
135+
PositionalIndexer,
136+
Scalar,
137+
Self,
138+
SortKind,
139+
TakeIndexer,
140+
TimeAmbiguous,
141+
TimeNonexistent,
142+
npt,
145143
)
146144

147145
from pandas import Series
@@ -805,8 +803,9 @@ def fillna(
805803
fallback_performancewarning()
806804
return super().fillna(value=value, method=method, limit=limit)
807805

808-
if is_array_like(value):
809-
value = cast(ArrayLike, value)
806+
if isinstance(value, (np.ndarray, ExtensionArray)):
807+
# Similar to check_value_size, but we do not mask here since we may
808+
# end up passing it to the super() method.
810809
if len(value) != len(self):
811810
raise ValueError(
812811
f"Length of 'value' does not match. Got ({len(value)}) "

pandas/core/common.py

+4-24
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@
3333

3434
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
3535
from pandas.core.dtypes.common import (
36-
is_array_like,
3736
is_bool_dtype,
3837
is_integer,
3938
)
40-
from pandas.core.dtypes.dtypes import ExtensionDtype
4139
from pandas.core.dtypes.generic import (
4240
ABCExtensionArray,
4341
ABCIndex,
@@ -120,9 +118,7 @@ def is_bool_indexer(key: Any) -> bool:
120118
check_array_indexer : Check that `key` is a valid array to index,
121119
and convert to an ndarray.
122120
"""
123-
if isinstance(key, (ABCSeries, np.ndarray, ABCIndex)) or (
124-
is_array_like(key) and isinstance(key.dtype, ExtensionDtype)
125-
):
121+
if isinstance(key, (ABCSeries, np.ndarray, ABCIndex, ABCExtensionArray)):
126122
if key.dtype == np.object_:
127123
key_array = np.asarray(key)
128124

@@ -420,7 +416,7 @@ def random_state(state: np.random.Generator) -> np.random.Generator:
420416

421417
@overload
422418
def random_state(
423-
state: int | ArrayLike | np.random.BitGenerator | np.random.RandomState | None,
419+
state: int | np.ndarray | np.random.BitGenerator | np.random.RandomState | None,
424420
) -> np.random.RandomState:
425421
...
426422

@@ -445,24 +441,8 @@ def random_state(state: RandomState | None = None):
445441
np.random.RandomState or np.random.Generator. If state is None, returns np.random
446442
447443
"""
448-
if (
449-
is_integer(state)
450-
or is_array_like(state)
451-
or isinstance(state, np.random.BitGenerator)
452-
):
453-
# error: Argument 1 to "RandomState" has incompatible type "Optional[Union[int,
454-
# Union[ExtensionArray, ndarray[Any, Any]], Generator, RandomState]]"; expected
455-
# "Union[None, Union[Union[_SupportsArray[dtype[Union[bool_, integer[Any]]]],
456-
# Sequence[_SupportsArray[dtype[Union[bool_, integer[Any]]]]],
457-
# Sequence[Sequence[_SupportsArray[dtype[Union[bool_, integer[Any]]]]]],
458-
# Sequence[Sequence[Sequence[_SupportsArray[dtype[Union[bool_,
459-
# integer[Any]]]]]]],
460-
# Sequence[Sequence[Sequence[Sequence[_SupportsArray[dtype[Union[bool_,
461-
# integer[Any]]]]]]]]], Union[bool, int, Sequence[Union[bool, int]],
462-
# Sequence[Sequence[Union[bool, int]]], Sequence[Sequence[Sequence[Union[bool,
463-
# int]]]], Sequence[Sequence[Sequence[Sequence[Union[bool, int]]]]]]],
464-
# BitGenerator]"
465-
return np.random.RandomState(state) # type: ignore[arg-type]
444+
if is_integer(state) or isinstance(state, (np.ndarray, np.random.BitGenerator)):
445+
return np.random.RandomState(state)
466446
elif isinstance(state, np.random.RandomState):
467447
return state
468448
elif isinstance(state, np.random.Generator):

0 commit comments

Comments
 (0)