Skip to content

BUG: Regression in Resample.apply raised error when apply affected only a Series #37191

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

Closed
wants to merge 4 commits into from
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ Reshaping
- Bug in func :meth:`crosstab` when using multiple columns with ``margins=True`` and ``normalize=True`` (:issue:`35144`)
- Bug in :meth:`DataFrame.agg` with ``func={'name':<FUNC>}`` incorrectly raising ``TypeError`` when ``DataFrame.columns==['Name']`` (:issue:`36212`)
- Bug in :meth:`Series.transform` would give incorrect results or raise when the argument ``func`` was dictionary (:issue:`35811`)
- Bug in :func:`join` returned a non deterministic level-order for the resulting :class:`MultiIndex` (:issue:`36910`)
-

Sparse
Expand Down
12 changes: 8 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3581,8 +3581,12 @@ def _join_multi(self, other, how, return_indexers=True):
from pandas.core.reshape.merge import restore_dropped_levels_multijoin

# figure out join names
self_names = set(com.not_none(*self.names))
other_names = set(com.not_none(*other.names))
self_names = list(com.not_none(*self.names))
other_names = list(com.not_none(*other.names))
self_names_order = self_names.index
other_names_order = other_names.index
self_names = set(self_names)
other_names = set(other_names)
overlap = self_names & other_names

# need at least 1 in common
Expand All @@ -3592,8 +3596,8 @@ def _join_multi(self, other, how, return_indexers=True):
if isinstance(self, MultiIndex) and isinstance(other, MultiIndex):

# Drop the non-matching levels from left and right respectively
ldrop_names = list(self_names - overlap)
rdrop_names = list(other_names - overlap)
ldrop_names = sorted(list(self_names - overlap), key=self_names_order)
rdrop_names = sorted(list(other_names - overlap), key=other_names_order)

# if only the order differs
if not len(ldrop_names + rdrop_names):
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/reshape/merge/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,3 +879,20 @@ def _join_by_hand(a, b, how="left"):
for col, s in b_re.items():
a_re[col] = s
return a_re.reindex(columns=result_columns)


def test_join_inner_multiindex_deterministic_order():
# GH: 36910
left = pd.DataFrame(
data={"e": 5},
index=pd.MultiIndex.from_tuples([(1, 2, 4)], names=("a", "b", "d")),
)
right = pd.DataFrame(
data={"f": 6}, index=pd.MultiIndex.from_tuples([(2, 3)], names=("b", "c"))
)
result = left.join(right, how="inner")
expected = pd.DataFrame(
{"e": [5], "f": [6]},
index=pd.MultiIndex.from_tuples([(2, 1, 4, 3)], names=("b", "a", "d", "c")),
)
tm.assert_frame_equal(result, expected)