Skip to content

BUG: merge with arrow and numpy dtypes raises #52422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Fixed bug in :func:`merge` when merging with ``ArrowDtype`` one one and a NumPy dtype on the other side (:issue:`52406`)

.. ---------------------------------------------------------------------------
.. _whatsnew_201.other:
Expand Down
18 changes: 17 additions & 1 deletion pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@
)

from pandas import (
ArrowDtype,
Categorical,
Index,
MultiIndex,
Series,
)
import pandas.core.algorithms as algos
from pandas.core.arrays import (
ArrowExtensionArray,
BaseMaskedArray,
ExtensionArray,
)
Expand Down Expand Up @@ -2377,7 +2379,10 @@ def _factorize_keys(
rk = ensure_int64(rk.codes)

elif isinstance(lk, ExtensionArray) and is_dtype_equal(lk.dtype, rk.dtype):
if not isinstance(lk, BaseMaskedArray):
if not isinstance(lk, BaseMaskedArray) and not (
isinstance(lk.dtype, ArrowDtype)
and is_numeric_dtype(lk.dtype.numpy_dtype.type)
Copy link
Member

@mroeschke mroeschke Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
and is_numeric_dtype(lk.dtype.numpy_dtype.type)
and lk.dtype._is_numeric

Might be simpler?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't work unfortunately, we have to exclude all arrow dtypes whose numpy dtype is object

):
lk, _ = lk._values_for_factorize()

# error: Item "ndarray" of "Union[Any, ndarray]" has no attribute
Expand All @@ -2392,6 +2397,15 @@ def _factorize_keys(
assert isinstance(rk, BaseMaskedArray)
llab = rizer.factorize(lk._data, mask=lk._mask)
rlab = rizer.factorize(rk._data, mask=rk._mask)
elif isinstance(lk, ArrowExtensionArray) or isinstance(rk, ArrowExtensionArray):
# we can only get here with numeric dtypes
# TODO: Remove when we have a Factorizer for Arrow
llab = rizer.factorize(
lk.to_numpy(na_value=1, dtype=lk.dtype.numpy_dtype), mask=lk.isna()
)
rlab = rizer.factorize(
rk.to_numpy(na_value=1, dtype=lk.dtype.numpy_dtype), mask=rk.isna()
)
else:
# Argument 1 to "factorize" of "ObjectFactorizer" has incompatible type
# "Union[ndarray[Any, dtype[signedinteger[_64Bit]]],
Expand Down Expand Up @@ -2450,6 +2464,8 @@ def _convert_arrays_and_get_rizer_klass(
# Invalid index type "type" for "Dict[Type[object], Type[Factorizer]]";
# expected type "Type[object]"
klass = _factorizers[lk.dtype.type] # type: ignore[index]
elif isinstance(lk.dtype, ArrowDtype):
klass = _factorizers[lk.dtype.numpy_dtype.type]
else:
klass = _factorizers[lk.dtype.type]

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import numpy as np
import pytest

from pandas.compat import pa_version_under7p0

from pandas.core.dtypes.common import (
is_categorical_dtype,
is_object_dtype,
Expand Down Expand Up @@ -2761,3 +2763,18 @@ def test_merge_ea_and_non_ea(any_numeric_ea_dtype, join_type):
}
)
tm.assert_frame_equal(result, expected)


@pytest.mark.skipif(pa_version_under7p0, reason="need pyarrow")
@pytest.mark.parametrize("dtype", ["int64", "int64[pyarrow]"])
def test_merge_arrow_and_numpy_dtypes(dtype):
# GH#52406
df = DataFrame({"a": [1, 2]}, dtype=dtype)
df2 = DataFrame({"a": [1, 2]}, dtype="int64[pyarrow]")
result = df.merge(df2)
expected = df.copy()
tm.assert_frame_equal(result, expected)

result = df2.merge(df)
expected = df2.copy()
tm.assert_frame_equal(result, expected)