|
10 | 10 | import functools
|
11 | 11 | import itertools
|
12 | 12 | import re
|
| 13 | +import warnings |
13 | 14 |
|
14 | 15 | import numpy as np
|
15 | 16 | import numpy.ma as ma
|
@@ -999,7 +1000,17 @@ def test_constructor_maskedarray_nonfloat(self):
|
999 | 1000 | assert isna(frame).values.all()
|
1000 | 1001 |
|
1001 | 1002 | # cast type
|
1002 |
| - frame = DataFrame(mat, columns=["A", "B", "C"], index=[1, 2], dtype=np.int64) |
| 1003 | + msg = r"datetime64\[ns\] values and dtype=int64" |
| 1004 | + with tm.assert_produces_warning(FutureWarning, match=msg): |
| 1005 | + with warnings.catch_warnings(): |
| 1006 | + warnings.filterwarnings( |
| 1007 | + "ignore", |
| 1008 | + category=DeprecationWarning, |
| 1009 | + message="elementwise comparison failed", |
| 1010 | + ) |
| 1011 | + frame = DataFrame( |
| 1012 | + mat, columns=["A", "B", "C"], index=[1, 2], dtype=np.int64 |
| 1013 | + ) |
1003 | 1014 | assert frame.values.dtype == np.int64
|
1004 | 1015 |
|
1005 | 1016 | # Check non-masked values
|
@@ -2484,6 +2495,27 @@ def test_nested_list_columns(self):
|
2484 | 2495 | tm.assert_frame_equal(result, expected)
|
2485 | 2496 |
|
2486 | 2497 |
|
| 2498 | +class TestDataFrameConstructorWithDtypeCoercion: |
| 2499 | + def test_floating_values_integer_dtype(self): |
| 2500 | + # GH#40110 make DataFrame behavior with arraylike floating data and |
| 2501 | + # inty dtype match Series behavior |
| 2502 | + |
| 2503 | + arr = np.random.randn(10, 5) |
| 2504 | + |
| 2505 | + msg = "if they cannot be cast losslessly" |
| 2506 | + with tm.assert_produces_warning(FutureWarning, match=msg): |
| 2507 | + DataFrame(arr, dtype="i8") |
| 2508 | + |
| 2509 | + with tm.assert_produces_warning(None): |
| 2510 | + # if they can be cast losslessly, no warning |
| 2511 | + DataFrame(arr.round(), dtype="i8") |
| 2512 | + |
| 2513 | + # with NaNs, we already have the correct behavior, so no warning |
| 2514 | + arr[0, 0] = np.nan |
| 2515 | + with tm.assert_produces_warning(None): |
| 2516 | + DataFrame(arr, dtype="i8") |
| 2517 | + |
| 2518 | + |
2487 | 2519 | class TestDataFrameConstructorWithDatetimeTZ:
|
2488 | 2520 | @pytest.mark.parametrize("tz", ["US/Eastern", "dateutil/US/Eastern"])
|
2489 | 2521 | def test_construction_preserves_tzaware_dtypes(self, tz):
|
|
0 commit comments