|
39 | 39 | from pandas.util import testing as tm
|
40 | 40 |
|
41 | 41 |
|
| 42 | +@pytest.fixture(params=[True, False], ids=lambda val: str(val)) |
| 43 | +def coerce(request): |
| 44 | + return request.param |
| 45 | + |
| 46 | + |
42 | 47 | def test_is_sequence():
|
43 | 48 | is_seq = inference.is_sequence
|
44 | 49 | assert (is_seq((1, 2)))
|
@@ -340,44 +345,38 @@ def test_convert_numeric_uint64(self):
|
340 | 345 | exp = np.array([2**63], dtype=np.uint64)
|
341 | 346 | tm.assert_numpy_array_equal(lib.maybe_convert_numeric(arr, set()), exp)
|
342 | 347 |
|
343 |
| - def test_convert_numeric_uint64_nan(self): |
344 |
| - msg = 'uint64 array detected' |
345 |
| - cases = [(np.array([2**63, np.nan], dtype=object), set()), |
346 |
| - (np.array([str(2**63), np.nan], dtype=object), set()), |
347 |
| - (np.array([np.nan, 2**63], dtype=object), set()), |
348 |
| - (np.array([np.nan, str(2**63)], dtype=object), set()), |
349 |
| - (np.array([2**63, 2**63 + 1], dtype=object), set([2**63])), |
350 |
| - (np.array([str(2**63), str(2**63 + 1)], |
351 |
| - dtype=object), set([2**63]))] |
352 |
| - |
353 |
| - for coerce in (True, False): |
354 |
| - for arr, na_values in cases: |
355 |
| - if coerce: |
356 |
| - with tm.assert_raises_regex(ValueError, msg): |
357 |
| - lib.maybe_convert_numeric(arr, na_values, |
358 |
| - coerce_numeric=coerce) |
359 |
| - else: |
360 |
| - tm.assert_numpy_array_equal(lib.maybe_convert_numeric( |
361 |
| - arr, na_values), arr) |
362 |
| - |
363 |
| - def test_convert_numeric_int64_uint64(self): |
364 |
| - msg = 'uint64 and negative values detected' |
365 |
| - cases = [np.array([2**63, -1], dtype=object), |
366 |
| - np.array([str(2**63), -1], dtype=object), |
367 |
| - np.array([str(2**63), str(-1)], dtype=object), |
368 |
| - np.array([-1, 2**63], dtype=object), |
369 |
| - np.array([-1, str(2**63)], dtype=object), |
370 |
| - np.array([str(-1), str(2**63)], dtype=object)] |
371 |
| - |
372 |
| - for coerce in (True, False): |
373 |
| - for case in cases: |
374 |
| - if coerce: |
375 |
| - with tm.assert_raises_regex(ValueError, msg): |
376 |
| - lib.maybe_convert_numeric(case, set(), |
377 |
| - coerce_numeric=coerce) |
378 |
| - else: |
379 |
| - tm.assert_numpy_array_equal(lib.maybe_convert_numeric( |
380 |
| - case, set()), case) |
| 348 | + @pytest.mark.parametrize("arr,na_values", [ |
| 349 | + (np.array([2**63, np.nan], dtype=object), set()), |
| 350 | + (np.array([str(2**63), np.nan], dtype=object), set()), |
| 351 | + (np.array([np.nan, 2**63], dtype=object), set()), |
| 352 | + (np.array([np.nan, str(2**63)], dtype=object), set())]) |
| 353 | + def test_convert_numeric_uint64_nan(self, coerce, arr, na_values): |
| 354 | + expected = arr.astype(float) if coerce else arr.copy() |
| 355 | + result = lib.maybe_convert_numeric(arr, na_values, |
| 356 | + coerce_numeric=coerce) |
| 357 | + tm.assert_almost_equal(result, expected) |
| 358 | + |
| 359 | + def test_convert_numeric_uint64_nan_values(self, coerce): |
| 360 | + arr = np.array([2**63, 2**63 + 1], dtype=object) |
| 361 | + na_values = set([2**63]) |
| 362 | + |
| 363 | + expected = (np.array([np.nan, 2**63 + 1], dtype=float) |
| 364 | + if coerce else arr.copy()) |
| 365 | + result = lib.maybe_convert_numeric(arr, na_values, |
| 366 | + coerce_numeric=coerce) |
| 367 | + tm.assert_almost_equal(result, expected) |
| 368 | + |
| 369 | + @pytest.mark.parametrize("case", [ |
| 370 | + np.array([2**63, -1], dtype=object), |
| 371 | + np.array([str(2**63), -1], dtype=object), |
| 372 | + np.array([str(2**63), str(-1)], dtype=object), |
| 373 | + np.array([-1, 2**63], dtype=object), |
| 374 | + np.array([-1, str(2**63)], dtype=object), |
| 375 | + np.array([str(-1), str(2**63)], dtype=object)]) |
| 376 | + def test_convert_numeric_int64_uint64(self, case, coerce): |
| 377 | + expected = case.astype(float) if coerce else case.copy() |
| 378 | + result = lib.maybe_convert_numeric(case, set(), coerce_numeric=coerce) |
| 379 | + tm.assert_almost_equal(result, expected) |
381 | 380 |
|
382 | 381 | def test_maybe_convert_objects_uint64(self):
|
383 | 382 | # see gh-4471
|
|
0 commit comments