Skip to content

BUG: Outer/right merge with EA dtypes cast to object #43152

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 20 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/v1.3.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Fixed regressions
- Fixed regression in :class:`DataFrame` constructor failing to broadcast for defined :class:`Index` and len one list of :class:`Timestamp` (:issue:`42810`)
- Performance regression in :meth:`core.window.ewm.ExponentialMovingWindow.mean` (:issue:`42333`)
- Fixed regression in :meth:`.GroupBy.agg` incorrectly raising in some cases (:issue:`42390`)
-
- Fixed regression in :meth:`merge` where columns with ``ExtensionDtype`` was cast to ``object`` in ``left`` and ``outer`` merge (:issue:`40073`)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe specify that the cast only occurs when those columns were being merged on?

Copy link
Member

Choose a reason for hiding this comment

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

IIUC what happened here was that left and outer were always broken, so fixing those could be moved down into the bug fix section of the whatsnew.

Then the regression portion could just focus on right

Copy link
Member Author

Choose a reason for hiding this comment

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

my bad, this bug was pertaining to right(regression) and outer (had mentioned in the PR subject), also discussed in the bug comments.
I will keep right in fixed regression, and move outer down to bug fix.


.. ---------------------------------------------------------------------------

Expand Down
13 changes: 8 additions & 5 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
Categorical,
Index,
MultiIndex,
Series,
)
from pandas.core import groupby
import pandas.core.algorithms as algos
Expand All @@ -81,10 +82,7 @@
from pandas.core.sorting import is_int64_overflow_possible

if TYPE_CHECKING:
from pandas import (
DataFrame,
Series,
)
from pandas import DataFrame
from pandas.core.arrays import DatetimeArray


Expand Down Expand Up @@ -904,17 +902,22 @@ def _maybe_add_join_keys(
# error: Item "bool" of "Union[Any, bool]" has no attribute "all"
if mask_left.all(): # type: ignore[union-attr]
key_col = Index(rvals)
final_dtype = rvals.dtype
# error: Item "bool" of "Union[Any, bool]" has no attribute "all"
elif (
right_indexer is not None
and mask_right.all() # type: ignore[union-attr]
):
key_col = Index(lvals)
final_dtype = lvals.dtype
else:
key_col = Index(lvals).where(~mask_left, rvals)
final_dtype = lvals.dtype

if result._is_label_reference(name):
result[name] = key_col
result[name] = Series(
key_col, dtype=final_dtype, index=result.index
)
elif result._is_level_reference(name):
if isinstance(result.index, MultiIndex):
key_col.name = name
Expand Down
43 changes: 41 additions & 2 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ def test_merge_join_key_dtype_cast(self):
df = merge(df1, df2, how="outer")

# GH13169
# this really should be bool
assert df["key"].dtype == "object"
# GH#40073
assert df["key"].dtype == "bool"

df1 = DataFrame({"val": [1]})
df2 = DataFrame({"val": [2]})
Expand Down Expand Up @@ -2487,3 +2487,42 @@ def test_mergeerror_on_left_index_mismatched_dtypes():
df_2 = DataFrame(data=["X"], columns=["C"], index=[999])
with pytest.raises(MergeError, match="Can only pass argument"):
merge(df_1, df_2, on=["C"], left_index=True)


@pytest.mark.parametrize(
"expected_data, how",
[
([1, 2], "outer"),
([], "inner"),
([2], "right"),
([1], "left"),
],
)
@pytest.mark.parametrize(
"dtype", ["Float64", "Float32", "Int64", "Int32", "UInt64", "UInt32"]
)
def test_merge_EA_dtype(dtype, how, expected_data):
# GH#40073
d1 = DataFrame([(1,)], columns=["id"], dtype=dtype)
d2 = DataFrame([(2,)], columns=["id"], dtype=dtype)
result = merge(d1, d2, how=how)
expected = DataFrame(expected_data, columns=["id"], dtype=dtype)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"expected_data, how",
[
(["a", "b"], "outer"),
([], "inner"),
(["b"], "right"),
(["a"], "left"),
],
)
def test_merge_string_dtype(how, expected_data):
# GH#40073
d1 = DataFrame([("a",)], columns=["id"], dtype="string")
d2 = DataFrame([("b",)], columns=["id"], dtype="string")
result = merge(d1, d2, how=how)
expected = DataFrame(expected_data, columns=["id"], dtype="string")
tm.assert_frame_equal(result, expected)