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

REF: check monotonicity inside _can_use_libjoin #55342

Merged
merged 12 commits into from
Dec 27, 2023

Conversation

jbrockmendel
Copy link
Member

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

and self.is_monotonic_increasing
and other.is_monotonic_increasing
and not (self.has_duplicates and other.has_duplicates)
and (self.is_unique or other.is_unique)
Copy link
Member

Choose a reason for hiding this comment

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

I think the order here can have a difference. is_monotonic_increasing will provide is_unique "for free" if the index is both monotonic and unique and it hasn't already been cached.

import pandas as pd
import numpy as np

arr = np.arange(1_000_000)

%timeit idx=pd.Index(arr); idx.is_unique and idx.is_monotonic_increasing
# 68.7 ms ± 5.13 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit idx=pd.Index(arr); idx.is_monotonic_increasing and idx.is_unique
# 2.91 ms ± 121 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. i had expected the cache for all of these to get populated at the same time inside IndexEngine, will take a closer look

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, looks like engine.is_monotonic_increasing will populate the engine.is_unique cache but not vice-versa. All of the paths within the engine that check is_unique do that check after checking self.is_monotonic_increasing. im inclined to update the engine code so that the unique check always populates the is_monotonic_increasing cache in order to 1) make the perf not dependent on the order these are accessed and 2) avoid populating engine.mapping in these cases (which allocates memory)

Copy link
Member

Choose a reason for hiding this comment

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

im inclined to update the engine code so that the unique check always populates the is_monotonic_increasing cache in order to 1) make the perf not dependent on the order these are accessed and 2) avoid populating engine.mapping in these cases (which allocates memory)

that sounds good to me

@mroeschke mroeschke added Refactor Internal refactoring of code Internals Related to non-user accessible pandas implementation labels Oct 2, 2023
@jbrockmendel jbrockmendel requested a review from WillAyd as a code owner October 17, 2023 01:22
@jbrockmendel
Copy link
Member Author

@lukemanley any interest in taking over on this one? im at risk of letting it fall through the cracks

@lukemanley
Copy link
Member

@lukemanley any interest in taking over on this one? im at risk of letting it fall through the cracks

Sure, I'll take this one.

@lukemanley
Copy link
Member

It looks like this is now down to one test failure: test_setitem_mix_of_nan_and_interval

The underlying issue currently exists on main and is similar to #54847.

Now that Index.is_monotonic_increasing gets called as part of Index.is_unique we're now hitting this behavior in the test:

import pandas as pd
from decimal import Decimal

idx = pd.Index([Decimal("NaN"), Decimal("NaN")])

# raises InvalidOperation: [<class 'decimal.InvalidOperation'>]
idx.is_monotonic_increasing

We could either check for nulls in algos.is_monotonic or put a try/except around Index.is_unique to catch this case. Kind of annoying either way it seems. Any other options?

@jbrockmendel
Copy link
Member Author

or put a try/except around Index.is_unique to catch this case

Where would this live? some places would be uglier than others.

@@ -280,7 +286,7 @@ cdef class IndexEngine:
values = self.values
self.monotonic_inc, self.monotonic_dec, is_strict_monotonic = \
self._call_monotonic(values)
except TypeError:
except (TypeError, InvalidOperation):
Copy link
Member

Choose a reason for hiding this comment

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

added to catch the Decimal("NaN") < Decimal("NaN") case

@lukemanley
Copy link
Member

Where would this live? some places would be uglier than others.

Added to the existing try/except in IndexEngine._do_monotonic_check

This is now passing all tests.

Copy link
Contributor

github-actions bot commented Dec 2, 2023

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Dec 2, 2023
@lukemanley lukemanley removed the Stale label Dec 7, 2023
@lukemanley lukemanley added this to the 2.2 milestone Dec 7, 2023
@lukemanley
Copy link
Member

gentle ping @jbrockmendel if you have any thoughts on this

@jbrockmendel
Copy link
Member Author

LGTM. Is the perf issue you noted addressed? One more merge main and ping on green pls

@lukemanley
Copy link
Member

@jbrockmendel - merged main and now green. I think this is ready

@mroeschke mroeschke modified the milestones: 2.2, 3.0 Dec 27, 2023
@mroeschke mroeschke merged commit ee8f335 into pandas-dev:main Dec 27, 2023
45 checks passed
@mroeschke
Copy link
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the ref-can_use_libjoin branch December 28, 2023 01:59
@jbrockmendel
Copy link
Member Author

thanks @lukemanley

cbpygit pushed a commit to cbpygit/pandas that referenced this pull request Jan 2, 2024
* REF: fix can_use_libjoin check

* DOC: docstring for can_use_libjoin

* Make can_use_libjoin checks more-correct

* avoid allocating mapping in monotonic cases

* fix categorical memory usage tests

* catch decimal.InvalidOperation

---------

Co-authored-by: Luke Manley <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Internals Related to non-user accessible pandas implementation Refactor Internal refactoring of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants