Skip to content

Commit cf1a11c

Browse files
BUG[string]: incorrect index downcast in DataFrame.join (#61771)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 0faaf5c commit cf1a11c

File tree

3 files changed

+8
-15
lines changed

3 files changed

+8
-15
lines changed

doc/source/whatsnew/v2.3.1.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ correctly, rather than defaulting to ``object`` dtype. For example:
5757
Bug fixes
5858
^^^^^^^^^
5959
- 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`)
60+
- Bug in :meth:`DataFrame.join` incorrectly downcasting object-dtype indexes (:issue:`61771`)
6061
- 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`)
6162
- Fixed bug in :meth:`DataFrame.explode` and :meth:`Series.explode` where methods would fail with ``dtype="str"`` (:issue:`61623`)
6263
- Fixed bug in unpickling objects pickled in pandas versions pre-2.3.0 that used :class:`StringDtype` (:issue:`61763`).

pandas/core/reshape/merge.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,13 +1328,13 @@ def _maybe_add_join_keys(
13281328
# if we have an all missing left_indexer
13291329
# make sure to just use the right values or vice-versa
13301330
if left_indexer is not None and (left_indexer == -1).all():
1331-
key_col = Index(rvals)
1331+
key_col = Index(rvals, dtype=rvals.dtype, copy=False)
13321332
result_dtype = rvals.dtype
13331333
elif right_indexer is not None and (right_indexer == -1).all():
1334-
key_col = Index(lvals)
1334+
key_col = Index(lvals, dtype=lvals.dtype, copy=False)
13351335
result_dtype = lvals.dtype
13361336
else:
1337-
key_col = Index(lvals)
1337+
key_col = Index(lvals, dtype=lvals.dtype, copy=False)
13381338
if left_indexer is not None:
13391339
mask_left = left_indexer == -1
13401340
key_col = key_col.where(~mask_left, rvals)

pandas/tests/copy_view/test_functions.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import numpy as np
22
import pytest
33

4-
from pandas._config import using_string_dtype
5-
6-
from pandas.compat import HAS_PYARROW
7-
84
from pandas import (
95
DataFrame,
106
Index,
@@ -247,13 +243,9 @@ def test_merge_copy_keyword():
247243
assert np.shares_memory(get_array(df2, "b"), get_array(result, "b"))
248244

249245

250-
@pytest.mark.xfail(
251-
using_string_dtype() and HAS_PYARROW,
252-
reason="TODO(infer_string); result.index infers str dtype while both "
253-
"df1 and df2 index are object.",
254-
)
255-
def test_join_on_key():
256-
df_index = Index(["a", "b", "c"], name="key", dtype=object)
246+
@pytest.mark.parametrize("dtype", [object, "str"])
247+
def test_join_on_key(dtype):
248+
df_index = Index(["a", "b", "c"], name="key", dtype=dtype)
257249

258250
df1 = DataFrame({"a": [1, 2, 3]}, index=df_index.copy(deep=True))
259251
df2 = DataFrame({"b": [4, 5, 6]}, index=df_index.copy(deep=True))
@@ -265,7 +257,7 @@ def test_join_on_key():
265257

266258
assert np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
267259
assert np.shares_memory(get_array(result, "b"), get_array(df2, "b"))
268-
assert np.shares_memory(get_array(result.index), get_array(df1.index))
260+
assert tm.shares_memory(get_array(result.index), get_array(df1.index))
269261
assert not np.shares_memory(get_array(result.index), get_array(df2.index))
270262

271263
result.iloc[0, 0] = 0

0 commit comments

Comments
 (0)