|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pandas.compat import HAS_PYARROW |
| 5 | + |
| 6 | +from pandas.core.dtypes.cast import find_common_type |
| 7 | + |
| 8 | +import pandas as pd |
| 9 | +import pandas._testing as tm |
| 10 | +from pandas.util.version import Version |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + "to_concat_dtypes, result_dtype", |
| 15 | + [ |
| 16 | + # same types |
| 17 | + ([("pyarrow", pd.NA), ("pyarrow", pd.NA)], ("pyarrow", pd.NA)), |
| 18 | + ([("pyarrow", np.nan), ("pyarrow", np.nan)], ("pyarrow", np.nan)), |
| 19 | + ([("python", pd.NA), ("python", pd.NA)], ("python", pd.NA)), |
| 20 | + ([("python", np.nan), ("python", np.nan)], ("python", np.nan)), |
| 21 | + # pyarrow preference |
| 22 | + ([("pyarrow", pd.NA), ("python", pd.NA)], ("pyarrow", pd.NA)), |
| 23 | + # NA preference |
| 24 | + ([("python", pd.NA), ("python", np.nan)], ("python", pd.NA)), |
| 25 | + ], |
| 26 | +) |
| 27 | +def test_concat_series(request, to_concat_dtypes, result_dtype): |
| 28 | + if any(storage == "pyarrow" for storage, _ in to_concat_dtypes) and not HAS_PYARROW: |
| 29 | + pytest.skip("Could not import 'pyarrow'") |
| 30 | + |
| 31 | + ser_list = [ |
| 32 | + pd.Series(["a", "b", None], dtype=pd.StringDtype(storage, na_value)) |
| 33 | + for storage, na_value in to_concat_dtypes |
| 34 | + ] |
| 35 | + |
| 36 | + result = pd.concat(ser_list, ignore_index=True) |
| 37 | + expected = pd.Series( |
| 38 | + ["a", "b", None, "a", "b", None], dtype=pd.StringDtype(*result_dtype) |
| 39 | + ) |
| 40 | + tm.assert_series_equal(result, expected) |
| 41 | + |
| 42 | + # order doesn't matter for result |
| 43 | + result = pd.concat(ser_list[::1], ignore_index=True) |
| 44 | + tm.assert_series_equal(result, expected) |
| 45 | + |
| 46 | + |
| 47 | +def test_concat_with_object(string_dtype_arguments): |
| 48 | + # _get_common_dtype cannot inspect values, so object dtype with strings still |
| 49 | + # results in object dtype |
| 50 | + result = pd.concat( |
| 51 | + [ |
| 52 | + pd.Series(["a", "b", None], dtype=pd.StringDtype(*string_dtype_arguments)), |
| 53 | + pd.Series(["a", "b", None], dtype=object), |
| 54 | + ] |
| 55 | + ) |
| 56 | + assert result.dtype == np.dtype("object") |
| 57 | + |
| 58 | + |
| 59 | +def test_concat_with_numpy(string_dtype_arguments): |
| 60 | + # common type with a numpy string dtype always preserves the pandas string dtype |
| 61 | + dtype = pd.StringDtype(*string_dtype_arguments) |
| 62 | + assert find_common_type([dtype, np.dtype("U")]) == dtype |
| 63 | + assert find_common_type([np.dtype("U"), dtype]) == dtype |
| 64 | + assert find_common_type([dtype, np.dtype("U10")]) == dtype |
| 65 | + assert find_common_type([np.dtype("U10"), dtype]) == dtype |
| 66 | + |
| 67 | + # with any other numpy dtype -> object |
| 68 | + assert find_common_type([dtype, np.dtype("S")]) == np.dtype("object") |
| 69 | + assert find_common_type([dtype, np.dtype("int64")]) == np.dtype("object") |
| 70 | + |
| 71 | + if Version(np.__version__) >= Version("2"): |
| 72 | + assert find_common_type([dtype, np.dtypes.StringDType()]) == dtype |
| 73 | + assert find_common_type([np.dtypes.StringDType(), dtype]) == dtype |
0 commit comments