Skip to content

Commit cacc73f

Browse files
committed
BUG[string]: incorrect index downcast in DataFrame.join
1 parent 391107a commit cacc73f

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,8 @@ Reshaping
866866
- Bug in :meth:`concat` where concatenating DataFrame and Series with ``ignore_index = True`` drops the series name (:issue:`60723`, :issue:`56257`)
867867
- Bug in :func:`melt` where calling with duplicate column names in ``id_vars`` raised a misleading ``AttributeError`` (:issue:`61475`)
868868
- Bug in :meth:`DataFrame.merge` where user-provided suffixes could result in duplicate column names if the resulting names matched existing columns. Now raises a :class:`MergeError` in such cases. (:issue:`61402`)
869+
- Bug in :meth:`DataFrame.join` incorrectly downcasting object-dtype indexes (:issue:`??`)
870+
-
869871

870872
Sparse
871873
^^^^^^

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)