Skip to content

BUG: NumericIndex.append retain dtype #43938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down