Skip to content

PERF: ArrowExtensionArray.searchsorted #50447

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 11 commits into from
Jan 5, 2023

Conversation

lukemanley
Copy link
Member

@lukemanley lukemanley commented Dec 27, 2022

Perf improvement in ArrowExtensionArray.searchsorted:

import pandas as pd
import numpy as np

arr = pd.array(np.arange(10**6), dtype="int64[pyarrow]")

%timeit arr.searchsorted(500)

# 367 ms ± 2.52 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)         <- main
# 21.3 µs ± 1.18 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)  <- PR

@lukemanley lukemanley added Performance Memory or execution speed performance Arrow pyarrow functionality labels Dec 27, 2022
sorter: NumpySorter = None,
) -> npt.NDArray[np.intp] | np.intp:
if self._hasna:
raise ValueError(
Copy link
Member

Choose a reason for hiding this comment

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

Is this the same error the base class would raise? Are there tests for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, the base class raises:

TypeError: boolean value of NA is ambiguous

The ArrowExtensionArray.searchsorted method added here is pretty much a copy of BaseMaskedArray.searchsorted. I will add a test. (I'll add one for BaseMaskedArray as well - I don't see it being tested there)

"which is impossible with NAs present."
)
with pytest.raises(ValueError, match=err_msg):
print(arr_with_na.dtype)
Copy link
Member

Choose a reason for hiding this comment

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

Could you remove this print?

Copy link
Member Author

Choose a reason for hiding this comment

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

oops, removed. thx

@@ -438,6 +438,18 @@ def test_searchsorted(self, data_for_sorting, as_series):
sorter = np.array([1, 2, 0])
assert data_for_sorting.searchsorted(a, sorter=sorter) == 0

# arr containing na value
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 since this is applicable to pandas specific masked arrays + arrow array, this test should live in those specific test files. Technically these base extension tests should be passable for any EA developer who might not define searchsorted to error in this way

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense, I've moved the tests.

@mroeschke mroeschke added this to the 2.0 milestone Jan 5, 2023
@mroeschke mroeschke merged commit a82f905 into pandas-dev:main Jan 5, 2023
@mroeschke
Copy link
Member

Thanks @lukemanley

@lukemanley lukemanley deleted the arrow-ea-searchsorted branch January 19, 2023 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants