108
108
# --------------- #
109
109
# dtype access #
110
110
# --------------- #
111
- def _ensure_data (
112
- values : ArrayLike , dtype : Optional [DtypeObj ] = None
113
- ) -> Tuple [np .ndarray , DtypeObj ]:
111
+ def _ensure_data (values : ArrayLike ) -> Tuple [np .ndarray , DtypeObj ]:
114
112
"""
115
113
routine to ensure that our data is of the correct
116
114
input dtype for lower-level routines
@@ -126,43 +124,33 @@ def _ensure_data(
126
124
Parameters
127
125
----------
128
126
values : array-like
129
- dtype : pandas_dtype, optional
130
- coerce to this dtype
131
127
132
128
Returns
133
129
-------
134
130
values : ndarray
135
131
pandas_dtype : np.dtype or ExtensionDtype
136
132
"""
137
133
138
- if dtype is not None :
139
- # We only have non-None dtype when called from `isin`, and
140
- # both Datetimelike and Categorical dispatch before getting here.
141
- assert not needs_i8_conversion (dtype )
142
- assert not is_categorical_dtype (dtype )
143
-
144
134
if not isinstance (values , ABCMultiIndex ):
145
135
# extract_array would raise
146
136
values = extract_array (values , extract_numpy = True )
147
137
148
138
# we check some simple dtypes first
149
- if is_object_dtype (dtype ):
150
- return ensure_object (np .asarray (values )), np .dtype ("object" )
151
- elif is_object_dtype (values ) and dtype is None :
139
+ if is_object_dtype (values ):
152
140
return ensure_object (np .asarray (values )), np .dtype ("object" )
153
141
154
142
try :
155
- if is_bool_dtype (values ) or is_bool_dtype ( dtype ) :
143
+ if is_bool_dtype (values ):
156
144
# we are actually coercing to uint64
157
145
# until our algos support uint8 directly (see TODO)
158
146
return np .asarray (values ).astype ("uint64" ), np .dtype ("bool" )
159
- elif is_signed_integer_dtype (values ) or is_signed_integer_dtype ( dtype ) :
147
+ elif is_signed_integer_dtype (values ):
160
148
return ensure_int64 (values ), np .dtype ("int64" )
161
- elif is_unsigned_integer_dtype (values ) or is_unsigned_integer_dtype ( dtype ) :
149
+ elif is_unsigned_integer_dtype (values ):
162
150
return ensure_uint64 (values ), np .dtype ("uint64" )
163
- elif is_float_dtype (values ) or is_float_dtype ( dtype ) :
151
+ elif is_float_dtype (values ):
164
152
return ensure_float64 (values ), np .dtype ("float64" )
165
- elif is_complex_dtype (values ) or is_complex_dtype ( dtype ) :
153
+ elif is_complex_dtype (values ):
166
154
167
155
# ignore the fact that we are casting to float
168
156
# which discards complex parts
@@ -177,12 +165,12 @@ def _ensure_data(
177
165
return ensure_object (values ), np .dtype ("object" )
178
166
179
167
# datetimelike
180
- if needs_i8_conversion (values .dtype ) or needs_i8_conversion ( dtype ) :
181
- if is_period_dtype (values .dtype ) or is_period_dtype ( dtype ) :
168
+ if needs_i8_conversion (values .dtype ):
169
+ if is_period_dtype (values .dtype ):
182
170
from pandas import PeriodIndex
183
171
184
172
values = PeriodIndex (values )._data
185
- elif is_timedelta64_dtype (values .dtype ) or is_timedelta64_dtype ( dtype ) :
173
+ elif is_timedelta64_dtype (values .dtype ):
186
174
from pandas import TimedeltaIndex
187
175
188
176
values = TimedeltaIndex (values )._data
@@ -202,9 +190,7 @@ def _ensure_data(
202
190
dtype = values .dtype
203
191
return values .asi8 , dtype
204
192
205
- elif is_categorical_dtype (values .dtype ) and (
206
- is_categorical_dtype (dtype ) or dtype is None
207
- ):
193
+ elif is_categorical_dtype (values .dtype ):
208
194
values = cast ("Categorical" , values )
209
195
values = values .codes
210
196
dtype = pandas_dtype ("category" )
0 commit comments