|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pandas.core.dtypes.cast import find_common_type |
| 7 | +from pandas.core.dtypes.dtypes import ( |
| 8 | + CategoricalDtype, DatetimeTZDtype, PeriodDtype) |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize("source_dtypes,expected_common_dtype", [ |
| 12 | + ((np.int64,), np.int64), |
| 13 | + ((np.uint64,), np.uint64), |
| 14 | + ((np.float32,), np.float32), |
| 15 | + ((np.object,), np.object), |
| 16 | +
|
| 17 | + # Into ints. |
| 18 | + ((np.int16, np.int64), np.int64), |
| 19 | + ((np.int32, np.uint32), np.int64), |
| 20 | + ((np.uint16, np.uint64), np.uint64), |
| 21 | +
|
| 22 | + # Into floats. |
| 23 | + ((np.float16, np.float32), np.float32), |
| 24 | + ((np.float16, np.int16), np.float32), |
| 25 | + ((np.float32, np.int16), np.float32), |
| 26 | + ((np.uint64, np.int64), np.float64), |
| 27 | + ((np.int16, np.float64), np.float64), |
| 28 | + ((np.float16, np.int64), np.float64), |
| 29 | +
|
| 30 | + # Into others. |
| 31 | + ((np.complex128, np.int32), np.complex128), |
| 32 | + ((np.object, np.float32), np.object), |
| 33 | + ((np.object, np.int16), np.object), |
| 34 | +
|
| 35 | + # Bool with int. |
| 36 | + ((np.dtype("bool"), np.int64), np.object), |
| 37 | + ((np.dtype("bool"), np.int32), np.object), |
| 38 | + ((np.dtype("bool"), np.int16), np.object), |
| 39 | + ((np.dtype("bool"), np.int8), np.object), |
| 40 | + ((np.dtype("bool"), np.uint64), np.object), |
| 41 | + ((np.dtype("bool"), np.uint32), np.object), |
| 42 | + ((np.dtype("bool"), np.uint16), np.object), |
| 43 | + ((np.dtype("bool"), np.uint8), np.object), |
| 44 | +
|
| 45 | + # Bool with float. |
| 46 | + ((np.dtype("bool"), np.float64), np.object), |
| 47 | + ((np.dtype("bool"), np.float32), np.object), |
| 48 | +
|
| 49 | + ((np.dtype("datetime64[ns]"), np.dtype("datetime64[ns]")), |
| 50 | + np.dtype("datetime64[ns]")), |
| 51 | + ((np.dtype("timedelta64[ns]"), np.dtype("timedelta64[ns]")), |
| 52 | + np.dtype("timedelta64[ns]")), |
| 53 | +
|
| 54 | + ((np.dtype("datetime64[ns]"), np.dtype("datetime64[ms]")), |
| 55 | + np.dtype("datetime64[ns]")), |
| 56 | + ((np.dtype("timedelta64[ms]"), np.dtype("timedelta64[ns]")), |
| 57 | + np.dtype("timedelta64[ns]")), |
| 58 | +
|
| 59 | + ((np.dtype("datetime64[ns]"), np.dtype("timedelta64[ns]")), np.object), |
| 60 | + ((np.dtype("datetime64[ns]"), np.int64), np.object) |
| 61 | +]) |
| 62 | +def test_numpy_dtypes(source_dtypes, expected_common_dtype): |
| 63 | + assert find_common_type(source_dtypes) == expected_common_dtype |
| 64 | + |
| 65 | + |
| 66 | +def test_raises_empty_input(): |
| 67 | + with pytest.raises(ValueError, match="no types given"): |
| 68 | + find_common_type([]) |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize("dtypes,exp_type", [ |
| 72 | + ([CategoricalDtype()], "category"), |
| 73 | + ([np.object, CategoricalDtype()], np.object), |
| 74 | + ([CategoricalDtype(), CategoricalDtype()], "category"), |
| 75 | +]) |
| 76 | +def test_categorical_dtype(dtypes, exp_type): |
| 77 | + assert find_common_type(dtypes) == exp_type |
| 78 | + |
| 79 | + |
| 80 | +def test_datetimetz_dtype_match(): |
| 81 | + dtype = DatetimeTZDtype(unit="ns", tz="US/Eastern") |
| 82 | + assert find_common_type([dtype, dtype]) == "datetime64[ns, US/Eastern]" |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize("dtype2", [ |
| 86 | + DatetimeTZDtype(unit="ns", tz="Asia/Tokyo"), |
| 87 | + np.dtype("datetime64[ns]"), np.object, np.int64 |
| 88 | +]) |
| 89 | +def test_datetimetz_dtype_mismatch(dtype2): |
| 90 | + dtype = DatetimeTZDtype(unit="ns", tz="US/Eastern") |
| 91 | + assert find_common_type([dtype, dtype2]) == np.object |
| 92 | + assert find_common_type([dtype2, dtype]) == np.object |
| 93 | + |
| 94 | + |
| 95 | +def test_period_dtype_match(): |
| 96 | + dtype = PeriodDtype(freq="D") |
| 97 | + assert find_common_type([dtype, dtype]) == "period[D]" |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize("dtype2", [ |
| 101 | + DatetimeTZDtype(unit="ns", tz="Asia/Tokyo"), |
| 102 | + PeriodDtype(freq="2D"), PeriodDtype(freq="H"), |
| 103 | + np.dtype("datetime64[ns]"), np.object, np.int64 |
| 104 | +]) |
| 105 | +def test_period_dtype_mismatch(dtype2): |
| 106 | + dtype = PeriodDtype(freq="D") |
| 107 | + assert find_common_type([dtype, dtype2]) == np.object |
| 108 | + assert find_common_type([dtype2, dtype]) == np.object |
0 commit comments