-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Issue 28518 multiindex interesection #28735
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
Changes from 2 commits
db15036
565f982
67af7ec
0fce75e
2530335
716820e
426768b
c094506
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -365,6 +365,15 @@ def _verify_integrity(self, codes=None, levels=None): | |
"Level values must be unique: {values} on " | ||
"level {level}".format(values=[value for value in level], level=i) | ||
) | ||
if self.sortorder is not None: | ||
if int(self.sortorder) > self._lexsort_depth(): | ||
raise ValueError( | ||
"Value for sortorder must be inferior or equal " | ||
"to actual lexsort_depth: " | ||
"sortorder {sortorder} with lexsort_depth {lexsort_depth}".format( | ||
sortorder=self.sortorder, lexsort_depth=self._lexsort_depth() | ||
) | ||
) | ||
|
||
codes = [ | ||
self._validate_codes(level, code) for level, code in zip(levels, codes) | ||
|
@@ -1783,16 +1792,15 @@ def is_lexsorted(self): | |
@cache_readonly | ||
def lexsort_depth(self): | ||
if self.sortorder is not None: | ||
if self.sortorder == 0: | ||
return self.nlevels | ||
else: | ||
return 0 | ||
return int(self.sortorder) | ||
|
||
return self._lexsort_depth() | ||
nrebena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def _lexsort_depth(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right was wanting a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok sorry i did'nt get the "hint". |
||
int64_codes = [ensure_int64(level_codes) for level_codes in self.codes] | ||
for k in range(self.nlevels, 0, -1): | ||
if libalgos.is_lexsorted(int64_codes[:k]): | ||
return k | ||
|
||
return 0 | ||
|
||
def _sort_levels_monotonic(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2094,3 +2094,33 @@ def test_merge_equal_cat_dtypes2(): | |
|
||
# Categorical is unordered, so don't check ordering. | ||
tm.assert_frame_equal(result, expected, check_categorical=False) | ||
|
||
|
||
def test_merge_multiindex_columns(): | ||
# Issue #28518 | ||
# Verify that merging two dataframes give the expected labels | ||
# The original cause of this issue come from a bug lexsort_depth and is tested in | ||
# test_lexsort_depth | ||
|
||
index_tuples = [] | ||
letters = ["a", "b", "c", "d"] | ||
numbers = ["1", "2", "3"] | ||
|
||
for l in letters: | ||
for n in numbers: | ||
index_tuples.append([l, n]) | ||
|
||
index = pd.MultiIndex.from_tuples(index_tuples, names=["outer", "inner"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you not just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes cleary. |
||
|
||
frame_x = pd.DataFrame(columns=index) | ||
frame_x["id"] = "" | ||
|
||
frame_y = pd.DataFrame(columns=index) | ||
frame_y["id"] = "" | ||
|
||
l_suf = "_x" | ||
r_suf = "_y" | ||
expected_labels = sum(([l + l_suf, l + r_suf] for l in letters), []) | ||
merged_frame = frame_x.merge(frame_y, on="id", suffixes=((l_suf, r_suf))).columns | ||
for label in expected_labels: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of asserting within a loop would be easier if you built a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did'nt think about this way. It does make more sense. |
||
assert label in merged_frame |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason for
int(...)
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the line from the constructor, line 278, but if it is already cast in the constructor (maybe to handle passing False for sortorder), it may no be necessary here. I look if it passes the test without it.