@@ -136,7 +136,6 @@ def _ensure_data(values: ArrayLike) -> np.ndarray:
136
136
# extract_array would raise
137
137
values = extract_array (values , extract_numpy = True )
138
138
139
- # we check some simple dtypes first
140
139
if is_object_dtype (values .dtype ):
141
140
return ensure_object (np .asarray (values ))
142
141
@@ -149,17 +148,19 @@ def _ensure_data(values: ArrayLike) -> np.ndarray:
149
148
return _ensure_data (values ._data )
150
149
return np .asarray (values )
151
150
151
+ elif is_categorical_dtype (values .dtype ):
152
+ # NB: cases that go through here should NOT be using _reconstruct_data
153
+ # on the back-end.
154
+ values = cast ("Categorical" , values )
155
+ return values .codes
156
+
152
157
elif is_bool_dtype (values .dtype ):
153
158
if isinstance (values , np .ndarray ):
154
159
# i.e. actually dtype == np.dtype("bool")
155
160
return np .asarray (values ).view ("uint8" )
156
161
else :
157
- # i.e. all-bool Categorical, BooleanArray
158
- try :
159
- return np .asarray (values ).astype ("uint8" , copy = False )
160
- except (TypeError , ValueError ):
161
- # GH#42107 we have pd.NAs present
162
- return np .asarray (values )
162
+ # e.g. Sparse[bool, False] # TODO: no test cases get here
163
+ return np .asarray (values ).astype ("uint8" , copy = False )
163
164
164
165
elif is_integer_dtype (values .dtype ):
165
166
return np .asarray (values )
@@ -174,10 +175,7 @@ def _ensure_data(values: ArrayLike) -> np.ndarray:
174
175
return np .asarray (values )
175
176
176
177
elif is_complex_dtype (values .dtype ):
177
- # Incompatible return value type (got "Tuple[Union[Any, ExtensionArray,
178
- # ndarray[Any, Any]], Union[Any, ExtensionDtype]]", expected
179
- # "Tuple[ndarray[Any, Any], Union[dtype[Any], ExtensionDtype]]")
180
- return values # type: ignore[return-value]
178
+ return cast (np .ndarray , values )
181
179
182
180
# datetimelike
183
181
elif needs_i8_conversion (values .dtype ):
@@ -187,11 +185,6 @@ def _ensure_data(values: ArrayLike) -> np.ndarray:
187
185
npvalues = cast (np .ndarray , npvalues )
188
186
return npvalues
189
187
190
- elif is_categorical_dtype (values .dtype ):
191
- values = cast ("Categorical" , values )
192
- values = values .codes
193
- return values
194
-
195
188
# we have failed, return object
196
189
values = np .asarray (values , dtype = object )
197
190
return ensure_object (values )
@@ -218,7 +211,8 @@ def _reconstruct_data(
218
211
return values
219
212
220
213
if not isinstance (dtype , np .dtype ):
221
- # i.e. ExtensionDtype
214
+ # i.e. ExtensionDtype; note we have ruled out above the possibility
215
+ # that values.dtype == dtype
222
216
cls = dtype .construct_array_type ()
223
217
224
218
values = cls ._from_sequence (values , dtype = dtype )
@@ -938,9 +932,8 @@ def mode(values: ArrayLike, dropna: bool = True) -> ArrayLike:
938
932
if needs_i8_conversion (values .dtype ):
939
933
# Got here with ndarray; dispatch to DatetimeArray/TimedeltaArray.
940
934
values = ensure_wrapped_if_datetimelike (values )
941
- # error: Item "ndarray[Any, Any]" of "Union[ExtensionArray,
942
- # ndarray[Any, Any]]" has no attribute "_mode"
943
- return values ._mode (dropna = dropna ) # type: ignore[union-attr]
935
+ values = cast ("ExtensionArray" , values )
936
+ return values ._mode (dropna = dropna )
944
937
945
938
values = _ensure_data (values )
946
939
0 commit comments