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 14 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: 2 additions & 0 deletions doc/source/whatsnew/v1.3.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +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 ``on`` columns with few data types (``ExtensionDtype`` and ``bool``) was cast to ``object`` in ``right`` merge (:issue:`40073`)
- Fixed regression in :meth:`RangeIndex.where` and :meth:`RangeIndex.putmask` raising ``AssertionError`` when result did not represent a :class:`RangeIndex` (:issue:`43240`)

.. ---------------------------------------------------------------------------
Expand All @@ -35,6 +36,7 @@ Performance improvements

Bug fixes
~~~~~~~~~
- Fixed bug in :meth:`merge` where ``on`` columns with few data types (``ExtensionDtype`` and ``bool``) was cast to ``object`` in ``outer`` merge (:issue:`40073`)
- Bug in :meth:`.DataFrameGroupBy.agg` and :meth:`.DataFrameGroupBy.transform` with ``engine="numba"`` where ``index`` data was not being correctly passed into ``func`` (:issue:`43133`)
-

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)
result_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)
result_dtype = lvals.dtype
else:
key_col = Index(lvals).where(~mask_left, rvals)
result_dtype = lvals.dtype

if result._is_label_reference(name):
result[name] = key_col
result[name] = Series(
key_col, dtype=result_dtype, index=result.index
)
elif result._is_level_reference(name):
if isinstance(result.index, MultiIndex):
key_col.name = name
Expand Down
63 changes: 61 additions & 2 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import numpy as np
import pytest

from pandas.compat import (
IS64,
is_platform_windows,
)

from pandas.core.dtypes.common import (
is_categorical_dtype,
is_object_dtype,
Expand Down Expand Up @@ -354,8 +359,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 All @@ -364,6 +369,9 @@ def test_merge_join_key_dtype_cast(self):
df = merge(df1, df2, left_on=lkey, right_on=rkey, how="outer")
assert df["key_0"].dtype == "int64"

@pytest.mark.xfail(
Copy link
Contributor

Choose a reason for hiding this comment

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

? what is this for

Copy link
Member Author

Choose a reason for hiding this comment

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

not able to figure why this test is failing in windows and 32bit in azure pipelines here.

Was just wondering if xfailing is an option..

Copy link
Contributor

Choose a reason for hiding this comment

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

note generally, is the comparison failing? construct the initial dataframe with int64 (rather than int) should help.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, that worked. Thanks!

Let me know if this looks fine..

(is_platform_windows() or not IS64), reason="GH#40073: fail on Windows/32bit"
)
def test_handle_join_key_pass_array(self):
left = DataFrame(
{"key": [1, 1, 2, 2, 3], "value": np.arange(5)}, columns=["value", "key"]
Expand Down Expand Up @@ -1642,6 +1650,57 @@ def test_merge_incompat_dtypes_error(self, df1_vals, df2_vals):
with pytest.raises(ValueError, match=msg):
merge(df2, df1, on=["A"])

@pytest.mark.parametrize(
"expected_data, how",
[
([1, 2], "outer"),
([], "inner"),
([2], "right"),
([1], "left"),
],
)
def test_merge_EA_dtype(self, any_numeric_ea_dtype, how, expected_data):
# GH#40073
d1 = DataFrame([(1,)], columns=["id"], dtype=any_numeric_ea_dtype)
d2 = DataFrame([(2,)], columns=["id"], dtype=any_numeric_ea_dtype)
result = merge(d1, d2, how=how)
expected = DataFrame(expected_data, columns=["id"], dtype=any_numeric_ea_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(self, how, expected_data, any_string_dtype):
# GH#40073
d1 = DataFrame([("a",)], columns=["id"], dtype=any_string_dtype)
d2 = DataFrame([("b",)], columns=["id"], dtype=any_string_dtype)
result = merge(d1, d2, how=how)
expected = DataFrame(expected_data, columns=["id"], dtype=any_string_dtype)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"how, expected_data",
[
("inner", [[True, 1, 4], [False, 5, 3]]),
("outer", [[True, 1, 4], [False, 5, 3]]),
("left", [[True, 1, 4], [False, 5, 3]]),
("right", [[False, 5, 3], [True, 1, 4]]),
],
)
def test_merge_bool_dtype(self, how, expected_data):
# GH#40073
df1 = DataFrame({"A": [True, False], "B": [1, 5]})
df2 = DataFrame({"A": [False, True], "C": [3, 4]})
result = merge(df1, df2, how=how)
expected = DataFrame(expected_data, columns=["A", "B", "C"])
tm.assert_frame_equal(result, expected)


@pytest.fixture
def left():
Expand Down