@@ -214,16 +214,17 @@ def test_coerce_to_array_from_boolean_array():
214
214
215
215
216
216
def test_coerce_to_numpy_array ():
217
- # with missing values -> object dtype
217
+ # with missing values -> tries but fails to convert
218
218
arr = pd .array ([True , False , None ], dtype = "boolean" )
219
- result = np .array (arr )
220
- expected = np .array ([True , False , pd .NA ], dtype = "object" )
221
- tm .assert_numpy_array_equal (result , expected )
219
+ with pytest .raises (
220
+ ValueError , match = r"specify an appropriate 'na_value' for this dtype"
221
+ ):
222
+ result = np .array (arr )
222
223
223
- # also with no missing values -> object dtype
224
+ # also with no missing values -> successfully converts to bool
224
225
arr = pd .array ([True , False , True ], dtype = "boolean" )
225
226
result = np .array (arr )
226
- expected = np .array ([True , False , True ], dtype = "object " )
227
+ expected = np .array ([True , False , True ], dtype = "bool " )
227
228
tm .assert_numpy_array_equal (result , expected )
228
229
229
230
# force bool dtype
@@ -233,8 +234,12 @@ def test_coerce_to_numpy_array():
233
234
# with missing values will raise error
234
235
arr = pd .array ([True , False , None ], dtype = "boolean" )
235
236
msg = (
236
- "cannot convert to 'bool'-dtype NumPy array with missing values. "
237
- "Specify an appropriate 'na_value' for this dtype."
237
+ "^cannot convert to 'bool'-dtype NumPy array with missing values.\n "
238
+ "Please either:\n "
239
+ "- convert to 'float'\n "
240
+ "- convert to 'object'\n "
241
+ "- specify an appropriate 'na_value' for this dtype\n "
242
+ "for this dtype.\n $"
238
243
)
239
244
with pytest .raises (ValueError , match = msg ):
240
245
np .array (arr , dtype = "bool" )
@@ -260,16 +265,17 @@ def test_to_boolean_array_from_strings_invalid_string():
260
265
@pytest .mark .parametrize ("box" , [True , False ], ids = ["series" , "array" ])
261
266
def test_to_numpy (box ):
262
267
con = pd .Series if box else pd .array
263
- # default (with or without missing values) -> object dtype
268
+ # default (with or without missing values) -> bool dtype
264
269
arr = con ([True , False , True ], dtype = "boolean" )
265
270
result = arr .to_numpy ()
266
- expected = np .array ([True , False , True ], dtype = "object " )
271
+ expected = np .array ([True , False , True ], dtype = "bool " )
267
272
tm .assert_numpy_array_equal (result , expected )
268
273
269
274
arr = con ([True , False , None ], dtype = "boolean" )
270
- result = arr .to_numpy ()
271
- expected = np .array ([True , False , pd .NA ], dtype = "object" )
272
- tm .assert_numpy_array_equal (result , expected )
275
+ with pytest .raises (
276
+ ValueError , match = "specify an appropriate 'na_value' for this dtype"
277
+ ):
278
+ arr .to_numpy ()
273
279
274
280
arr = con ([True , False , None ], dtype = "boolean" )
275
281
result = arr .to_numpy (dtype = "str" )
@@ -304,11 +310,13 @@ def test_to_numpy(box):
304
310
expected = np .array ([1 , 0 , np .nan ], dtype = "float64" )
305
311
tm .assert_numpy_array_equal (result , expected )
306
312
307
- # converting to int or float without specifying na_value raises
313
+ # converting to int without specifying na_value raises
308
314
with pytest .raises (ValueError , match = "cannot convert to 'int64'-dtype" ):
309
315
arr .to_numpy (dtype = "int64" )
310
- with pytest .raises (ValueError , match = "cannot convert to 'float64'-dtype" ):
311
- arr .to_numpy (dtype = "float64" )
316
+ # converting to float without specifying na_value converts NA to nan
317
+ result = arr .to_numpy (dtype = "float64" )
318
+ expected = np .array ([1 , 0 , np .nan ], dtype = "float64" )
319
+ tm .assert_numpy_array_equal (result , expected )
312
320
313
321
314
322
def test_to_numpy_copy ():
0 commit comments