Skip to content
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

BUG: outer join on equal indexes not sorting #56111

Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4653,9 +4653,8 @@ def _join_via_get_indexer(
elif how == "inner":
join_index = self.intersection(other, sort=sort)
elif how == "outer":
# TODO: sort=True here for backwards compat. It may
# be better to use the sort parameter passed into join
join_index = self.union(other)
join_index = _maybe_try_sort(join_index, sort=None)

if sort and how in ["left", "right"]:
join_index = join_index.sort_values()
Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/indexes/datetimes/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def test_does_not_convert_mixed_integer(self):
r_idx_type="i",
c_idx_type="dt",
)
cols = df.columns.join(df.index, how="outer")
with tm.assert_produces_warning(RuntimeWarning):
cols = df.columns.join(df.index, how="outer")
joined = cols.join(df.columns)
assert cols.dtype == np.dtype("O")
assert cols.dtype == joined.dtype
Expand All @@ -53,8 +54,11 @@ def test_join_with_period_index(self, join_type):
)
s = df.iloc[:5, 0]

expected = df.columns.astype("O").join(s.index, how=join_type)
result = df.columns.join(s.index, how=join_type)
warning = RuntimeWarning if join_type == "outer" else None
with tm.assert_produces_warning(warning):
expected = df.columns.astype("O").join(s.index, how=join_type)
with tm.assert_produces_warning(warning):
result = df.columns.join(s.index, how=join_type)
tm.assert_index_equal(expected, result)

def test_join_object_index(self):
Expand Down
15 changes: 11 additions & 4 deletions pandas/tests/indexes/multi/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ def test_join_level_corner_case(idx):


def test_join_self(idx, join_type):
joined = idx.join(idx, how=join_type)
tm.assert_index_equal(joined, idx)
result = idx.join(idx, how=join_type)
expected = idx
if join_type == "outer":
expected = expected.sort_values()
tm.assert_index_equal(result, expected)


def test_join_multi():
Expand Down Expand Up @@ -91,8 +94,12 @@ def test_join_multi():

def test_join_self_unique(idx, join_type):
if idx.is_unique:
joined = idx.join(idx, how=join_type)
assert (idx == joined).all()
result = idx.join(idx, how=join_type)
if join_type == "outer":
expected = idx.sort_values()
else:
expected = idx
tm.assert_index_equal(result, expected)


def test_join_multi_wrong_order():
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexes/period/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ def test_join_does_not_recur(self):
)
ser = df.iloc[:2, 0]

res = ser.index.join(df.columns, how="outer")
with tm.assert_produces_warning(RuntimeWarning):
result = ser.index.join(df.columns, how="outer")
expected = Index(
[ser.index[0], ser.index[1], df.columns[0], df.columns[1]], object
)
tm.assert_index_equal(res, expected)
tm.assert_index_equal(result, expected)

def test_join_mismatched_freq_raises(self):
index = period_range("1/1/2000", "1/20/2000", freq="D")
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,11 @@ def test_slice_keep_name(self):
indirect=True,
)
def test_join_self(self, index, join_type):
joined = index.join(index, how=join_type)
assert index is joined
result = index.join(index, how=join_type)
expected = index
if join_type == "outer":
expected = expected.sort_values()
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("method", ["strip", "rstrip", "lstrip"])
def test_str_attribute(self, method):
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/indexes/test_old_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
period_range,
)
import pandas._testing as tm
import pandas.core.algorithms as algos
from pandas.core.arrays import BaseMaskedArray


Expand Down Expand Up @@ -646,7 +647,10 @@ def test_join_self_unique(self, join_type, simple_index):
idx = simple_index
if idx.is_unique:
joined = idx.join(idx, how=join_type)
assert (idx == joined).all()
expected = simple_index
if join_type == "outer":
expected = algos.safe_sort(expected)
tm.assert_index_equal(joined, expected)

def test_map(self, simple_index):
# callable
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/timedeltas/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def test_does_not_convert_mixed_integer(self):
c_idx_type="td",
)

cols = df.columns.join(df.index, how="outer")
with tm.assert_produces_warning(RuntimeWarning):
cols = df.columns.join(df.index, how="outer")
joined = cols.join(df.columns)
assert cols.dtype == np.dtype("O")
assert cols.dtype == joined.dtype
Expand Down
17 changes: 7 additions & 10 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2913,16 +2913,13 @@ def test_merge_combinations(
expected = expected["key"].repeat(repeats.values)
expected = expected.to_frame()
elif how == "outer":
if on_index and left_unique and left["key"].equals(right["key"]):
expected = DataFrame({"key": left["key"]})
else:
left_counts = left["key"].value_counts()
right_counts = right["key"].value_counts()
expected_counts = left_counts.mul(right_counts, fill_value=1)
expected_counts = expected_counts.astype(np.intp)
expected = expected_counts.index.values.repeat(expected_counts.values)
expected = DataFrame({"key": expected})
expected = expected.sort_values("key")
left_counts = left["key"].value_counts()
right_counts = right["key"].value_counts()
expected_counts = left_counts.mul(right_counts, fill_value=1)
expected_counts = expected_counts.astype(np.intp)
expected = expected_counts.index.values.repeat(expected_counts.values)
expected = DataFrame({"key": expected})
expected = expected.sort_values("key")

if on_index:
expected = expected.set_index("key")
Expand Down
Loading