@@ -168,8 +168,10 @@ def _to_numpy(data: Any) -> np.ndarray:
168
168
"date64[ms][pyarrow]" : "datetime64[ms]" ,
169
169
}
170
170
171
- # The expected numpy dtype for the result numpy array, but can be None.
172
- dtype = dtypes .get (str (getattr (data , "dtype" , getattr (data , "type" , "" ))))
171
+ # The dtype for the input object.
172
+ dtype = getattr (data , "dtype" , getattr (data , "type" , "" ))
173
+ # The numpy dtype for the result numpy array, but can be None.
174
+ numpy_dtype = dtypes .get (str (dtype ))
173
175
174
176
# pandas numeric dtypes were converted to np.object_ dtype prior pandas 2.2, and are
175
177
# converted to suitable NumPy dtypes since pandas 2.2. Refer to the following link
@@ -183,20 +185,20 @@ def _to_numpy(data: Any) -> np.ndarray:
183
185
and hasattr (data .dtype , "numpy_dtype" ) # pandas dtypes only.
184
186
and data .dtype .kind in "iuf" # Numeric dtypes only.
185
187
): # pandas Series/Index with pandas nullable numeric dtypes.
186
- dtype = data .dtype .numpy_dtype # The expected numpy dtype.
188
+ # The numpy dtype of the result numpy array.
189
+ numpy_dtype = data .dtype .numpy_dtype
187
190
if getattr (data , "hasnans" , False ):
188
191
if data .dtype .kind in "iu" :
189
192
# Integers with missing values are converted to float64.
190
- dtype = np .float64
193
+ numpy_dtype = np .float64
191
194
data = data .to_numpy (na_value = np .nan )
192
195
193
- array = np .ascontiguousarray (data , dtype = dtype )
196
+ array = np .ascontiguousarray (data , dtype = numpy_dtype )
194
197
195
198
# Check if a np.object_ array can be converted to np.str_.
196
199
if array .dtype == np .object_ :
197
200
with contextlib .suppress (TypeError , ValueError ):
198
201
return np .ascontiguousarray (array , dtype = np .str_ )
199
-
200
202
return array
201
203
202
204
0 commit comments