diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index c002832cd89bb..693b4827acf19 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4896,6 +4896,11 @@ def _concat(self, to_concat: list[Index], name: Hashable) -> Index: to_concat_vals = [x._values for x in to_concat] result = concat_compat(to_concat_vals) + + is_numeric = result.dtype.kind in ["i", "u", "f"] + if self._is_backward_compat_public_numeric_index and is_numeric: + return type(self)._simple_new(result, name=name) + return Index._with_infer(result, name=name) @final diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 7e43664c6b3de..38127a0e255bd 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -760,6 +760,19 @@ def test_index_groupby(self, simple_index): expected = {ex_keys[0]: idx[[0, 4]], ex_keys[1]: idx[[1, 3]]} tm.assert_dict_equal(idx.groupby(to_groupby), expected) + def test_append_preserves_dtype(self, simple_index): + # In particular NumericIndex with dtype float32 + index = simple_index + N = len(index) + + result = index.append(index) + assert result.dtype == index.dtype + tm.assert_index_equal(result[:N], index, check_exact=True) + tm.assert_index_equal(result[N:], index, check_exact=True) + + alt = index.take(list(range(N)) * 2) + tm.assert_index_equal(result, alt, check_exact=True) + class NumericBase(Base): """