diff --git a/doc/source/whatsnew/v2.3.1.rst b/doc/source/whatsnew/v2.3.1.rst index eb3ad72f6a59f..2ee9cf9263768 100644 --- a/doc/source/whatsnew/v2.3.1.rst +++ b/doc/source/whatsnew/v2.3.1.rst @@ -57,6 +57,7 @@ correctly, rather than defaulting to ``object`` dtype. For example: Bug fixes ^^^^^^^^^ - Bug in :meth:`.DataFrameGroupBy.min`, :meth:`.DataFrameGroupBy.max`, :meth:`.Resampler.min`, :meth:`.Resampler.max` where all NA values of string dtype would return float instead of string dtype (:issue:`60810`) +- Bug in :meth:`DataFrame.join` incorrectly downcasting object-dtype indexes (:issue:`61771`) - Bug in :meth:`DataFrame.sum` with ``axis=1``, :meth:`.DataFrameGroupBy.sum` or :meth:`.SeriesGroupBy.sum` with ``skipna=True``, and :meth:`.Resampler.sum` with all NA values of :class:`StringDtype` resulted in ``0`` instead of the empty string ``""`` (:issue:`60229`) - Fixed bug in :meth:`DataFrame.explode` and :meth:`Series.explode` where methods would fail with ``dtype="str"`` (:issue:`61623`) - Fixed bug in unpickling objects pickled in pandas versions pre-2.3.0 that used :class:`StringDtype` (:issue:`61763`). diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index dc2df25c3f786..a1e003c2001d2 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1076,13 +1076,13 @@ def _maybe_add_join_keys( # if we have an all missing left_indexer # make sure to just use the right values or vice-versa if left_indexer is not None and (left_indexer == -1).all(): - key_col = Index(rvals) + key_col = Index(rvals, dtype=rvals.dtype, copy=False) result_dtype = rvals.dtype elif right_indexer is not None and (right_indexer == -1).all(): - key_col = Index(lvals) + key_col = Index(lvals, dtype=lvals.dtype, copy=False) result_dtype = lvals.dtype else: - key_col = Index(lvals) + key_col = Index(lvals, dtype=lvals.dtype, copy=False) if left_indexer is not None: mask_left = left_indexer == -1 key_col = key_col.where(~mask_left, rvals) @@ -1112,7 +1112,8 @@ def _maybe_add_join_keys( result.set_index(idx_list, inplace=True) else: - result.index = Index(key_col, name=name) + key_col.name = name + result.index = key_col else: result.insert(i, name or f"key_{i}", key_col) diff --git a/pandas/tests/copy_view/test_functions.py b/pandas/tests/copy_view/test_functions.py index eefd27964e6ae..ce444ac3571fa 100644 --- a/pandas/tests/copy_view/test_functions.py +++ b/pandas/tests/copy_view/test_functions.py @@ -1,10 +1,6 @@ import numpy as np import pytest -from pandas._config import using_string_dtype - -from pandas.compat import HAS_PYARROW - from pandas import ( DataFrame, Index, @@ -317,13 +313,9 @@ def test_merge_copy_keyword(using_copy_on_write, copy): assert not np.shares_memory(get_array(df2, "b"), get_array(result, "b")) -@pytest.mark.xfail( - using_string_dtype() and HAS_PYARROW, - reason="TODO(infer_string); result.index infers str dtype while both " - "df1 and df2 index are object.", -) -def test_join_on_key(using_copy_on_write): - df_index = Index(["a", "b", "c"], name="key", dtype=object) +@pytest.mark.parametrize("dtype", [object, "str"]) +def test_join_on_key(dtype, using_copy_on_write): + df_index = Index(["a", "b", "c"], name="key", dtype=dtype) df1 = DataFrame({"a": [1, 2, 3]}, index=df_index.copy(deep=True)) df2 = DataFrame({"b": [4, 5, 6]}, index=df_index.copy(deep=True)) @@ -336,7 +328,7 @@ def test_join_on_key(using_copy_on_write): if using_copy_on_write: assert np.shares_memory(get_array(result, "a"), get_array(df1, "a")) assert np.shares_memory(get_array(result, "b"), get_array(df2, "b")) - assert np.shares_memory(get_array(result.index), get_array(df1.index)) + assert tm.shares_memory(get_array(result.index), get_array(df1.index)) assert not np.shares_memory(get_array(result.index), get_array(df2.index)) else: assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a"))