Skip to content

Commit cd41d94

Browse files
phoflmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#54895: REGR: Merge raising when left merging on arrow string index
1 parent cf32a23 commit cd41d94

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

doc/source/whatsnew/v2.1.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ including other versions of pandas.
1313

1414
Fixed regressions
1515
~~~~~~~~~~~~~~~~~
16+
- Fixed regression in :func:`merge` when merging over a PyArrow string index (:issue:`54894`)
1617
- Fixed regression in :func:`read_csv` when ``usecols`` is given and ``dtypes`` is a dict for ``engine="python"`` (:issue:`54868`)
1718
- Fixed regression in :meth:`DataFrame.__setitem__` raising ``AssertionError`` when setting a :class:`Series` with a partial :class:`MultiIndex` (:issue:`54875`)
1819
- Fixed regression when comparing a :class:`Series` with ``datetime64`` dtype with ``None`` (:issue:`54870`)

pandas/core/reshape/merge.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -2433,8 +2433,12 @@ def _factorize_keys(
24332433
length = len(dc.dictionary)
24342434

24352435
llab, rlab, count = (
2436-
pc.fill_null(dc.indices[slice(len_lk)], length).to_numpy(),
2437-
pc.fill_null(dc.indices[slice(len_lk, None)], length).to_numpy(),
2436+
pc.fill_null(dc.indices[slice(len_lk)], length)
2437+
.to_numpy()
2438+
.astype(np.intp, copy=False),
2439+
pc.fill_null(dc.indices[slice(len_lk, None)], length)
2440+
.to_numpy()
2441+
.astype(np.intp, copy=False),
24382442
len(dc.dictionary),
24392443
)
24402444
if how == "right":

pandas/tests/reshape/merge/test_merge.py

+12
Original file line numberDiff line numberDiff line change
@@ -2870,3 +2870,15 @@ def test_merge_ea_int_and_float_numpy():
28702870

28712871
result = df2.merge(df1)
28722872
tm.assert_frame_equal(result, expected.astype("float64"))
2873+
2874+
2875+
def test_merge_arrow_string_index():
2876+
# GH#54894
2877+
pytest.importorskip("pyarrow")
2878+
left = DataFrame({"a": ["a", "b"]}, dtype="string[pyarrow]")
2879+
right = DataFrame({"b": 1}, index=Index(["a", "c"], dtype="string[pyarrow]"))
2880+
result = left.merge(right, left_on="a", right_index=True, how="left")
2881+
expected = DataFrame(
2882+
{"a": Series(["a", "b"], dtype="string[pyarrow]"), "b": [1, np.nan]}
2883+
)
2884+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)