-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix group index calculation to prevent hitting maximum recursion depth (#21524) #21541
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 4 commits
ee30e60
91a284e
fac97d5
7e9f315
727e654
befa65d
c02b188
eebb8cf
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 |
---|---|---|
|
@@ -52,7 +52,21 @@ def _int64_cut_off(shape): | |
return i | ||
return len(shape) | ||
|
||
def loop(labels, shape): | ||
def maybe_lift(lab, size): | ||
# promote nan values (assigned to -1 here) | ||
# so that all values are non-negative | ||
return (lab + 1, size + 1) if (lab == -1).any() else (lab, size) | ||
|
||
labels = map(_ensure_int64, labels) | ||
if not xnull: | ||
labels, shape = map(list, zip(*map(maybe_lift, labels, shape))) | ||
|
||
labels = list(labels) | ||
shape = list(shape) | ||
|
||
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 comment on the purpose of the loop 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. I would try, though I'm not the author of original code - I've just changed it from recursion to loop, so I can't be sure I understand 100% all the nuances here... |
||
# Iteratively process all the labels in chunks sized so less | ||
# than _INT64_MAX unique int ids will be required for each chunk | ||
while True: | ||
# how many levels can be done without overflow: | ||
nlev = _int64_cut_off(shape) | ||
|
||
|
@@ -74,7 +88,7 @@ def loop(labels, shape): | |
out[mask] = -1 | ||
|
||
if nlev == len(shape): # all levels done! | ||
return out | ||
break | ||
|
||
# compress what has been done so far in order to avoid overflow | ||
# to retain lexical ranks, obs_ids should be sorted | ||
|
@@ -83,16 +97,7 @@ def loop(labels, shape): | |
labels = [comp_ids] + labels[nlev:] | ||
shape = [len(obs_ids)] + shape[nlev:] | ||
|
||
return loop(labels, shape) | ||
|
||
def maybe_lift(lab, size): # pormote nan values | ||
return (lab + 1, size + 1) if (lab == -1).any() else (lab, size) | ||
|
||
labels = map(_ensure_int64, labels) | ||
if not xnull: | ||
labels, shape = map(list, zip(*map(maybe_lift, labels, shape))) | ||
|
||
return loop(list(labels), list(shape)) | ||
return out | ||
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. does out need a definition outside of the loop? e.g. is it always defined 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. it is always defined here - out is assigned before the exit from the loop can happen. |
||
|
||
|
||
def get_compressed_ids(labels, sizes): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1527,6 +1527,22 @@ def test_duplicated_with_misspelled_column_name(self, subset): | |
with pytest.raises(KeyError): | ||
df.drop_duplicates(subset) | ||
|
||
def test_duplicated_do_not_fail_on_wide_dataframes(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. How long does this test take to run? May be worth a slow tag 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. Given that original test case needed to hit recursion limit... it can't be super fast. |
||
# Given the wide dataframe with a lot of columns | ||
# with different (important!) values | ||
data = {} | ||
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. make this a dict comprehenseion |
||
for i in range(100): | ||
data['col_{0:02d}'.format(i)] = np.random.randint(0, 1000, 30000) | ||
df = pd.DataFrame(data).T | ||
# When we request to calculate duplicates | ||
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. don't need this comment here. |
||
dupes = df.duplicated() | ||
# Then we get the bool pd.Series as a result | ||
# and don't fail during calculation. | ||
# Actual values doesn't matter here, though usually | ||
# it's all False in this case | ||
assert isinstance(dupes, pd.Series) | ||
assert dupes.dtype == np.bool | ||
|
||
def test_drop_duplicates_with_duplicate_column_names(self): | ||
# GH17836 | ||
df = DataFrame([ | ||
|
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.
Does this not obfuscate NA and 0 values?
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.
this works with labels, not the original values. labels got here from
df.duplicate -> core.algorithms.factorize -> _factorize_array call and that method replace NA with -1 (
na_sentinel : int, default -1
) and assign all other values >=0Also, I did not change this in any way, it's an existing code that already in master :)