Skip to content

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

Merged
merged 8 commits into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/v0.23.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Bug Fixes

- Bug in :meth:`Index.get_indexer_non_unique` with categorical key (:issue:`21448`)
- Bug in comparison operations for :class:`MultiIndex` where error was raised on equality / inequality comparison involving a MultiIndex with ``nlevels == 1`` (:issue:`21149`)
- Bug in calculation of group index causing "maximum recursion depth exceeded" errors during ``DataFrame.duplicated`` calls (:issue:`21524`).
Copy link
Contributor

Choose a reason for hiding this comment

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

just say:

bug in :func:`DataFrame.duplicated` with a large number of columns causing a 'maximum .....'

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, I'll update this

-

**I/O**
Expand Down
25 changes: 13 additions & 12 deletions pandas/core/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,17 @@ def _int64_cut_off(shape):
return i
return len(shape)

def loop(labels, shape):
def maybe_lift(lab, size): # pormote nan values
Copy link
Contributor

Choose a reason for hiding this comment

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

sp here

can you add a 1-line comment on what this is doing

Copy link
Author

Choose a reason for hiding this comment

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

Same as with loop - 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...

return (lab + 1, size + 1) if (lab == -1).any() else (lab, size)
Copy link
Member

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?

Copy link
Author

@Templarrr Templarrr Jun 20, 2018

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 >=0
Also, I did not change this in any way, it's an existing code that already in master :)


labels = map(_ensure_int64, labels)
if not xnull:
labels, shape = map(list, zip(*map(maybe_lift, labels, shape)))

labels = list(labels)
shape = list(shape)

Copy link
Contributor

Choose a reason for hiding this comment

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

can you comment on the purpose of the loop

Copy link
Author

Choose a reason for hiding this comment

The 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...

while True:
# how many levels can be done without overflow:
nlev = _int64_cut_off(shape)

Expand All @@ -74,7 +84,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
Expand All @@ -83,16 +93,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
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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.
And if something (though I don't know what in this case) throw an Exception - we will bypass return alltogether



def get_compressed_ids(labels, sizes):
Expand Down