Skip to content

Commit 3fbb030

Browse files
authored
REGR: Merge raising when left merging on arrow string index (#54895)
1 parent 4b31302 commit 3fbb030

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:`.GroupBy.get_group` raising for ``axis=1`` (:issue:`54858`)
1819
- Fixed regression in :meth:`DataFrame.__setitem__` raising ``AssertionError`` when setting a :class:`Series` with a partial :class:`MultiIndex` (:issue:`54875`)

pandas/core/reshape/merge.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -2437,8 +2437,12 @@ def _factorize_keys(
24372437
length = len(dc.dictionary)
24382438

24392439
llab, rlab, count = (
2440-
pc.fill_null(dc.indices[slice(len_lk)], length).to_numpy(),
2441-
pc.fill_null(dc.indices[slice(len_lk, None)], length).to_numpy(),
2440+
pc.fill_null(dc.indices[slice(len_lk)], length)
2441+
.to_numpy()
2442+
.astype(np.intp, copy=False),
2443+
pc.fill_null(dc.indices[slice(len_lk, None)], length)
2444+
.to_numpy()
2445+
.astype(np.intp, copy=False),
24422446
len(dc.dictionary),
24432447
)
24442448
if how == "right":

pandas/tests/reshape/merge/test_merge.py

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

29482948
result = df2.merge(df1)
29492949
tm.assert_frame_equal(result, expected.astype("float64"))
2950+
2951+
2952+
def test_merge_arrow_string_index():
2953+
# GH#54894
2954+
pytest.importorskip("pyarrow")
2955+
left = DataFrame({"a": ["a", "b"]}, dtype="string[pyarrow]")
2956+
right = DataFrame({"b": 1}, index=Index(["a", "c"], dtype="string[pyarrow]"))
2957+
result = left.merge(right, left_on="a", right_index=True, how="left")
2958+
expected = DataFrame(
2959+
{"a": Series(["a", "b"], dtype="string[pyarrow]"), "b": [1, np.nan]}
2960+
)
2961+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)