Skip to content

PERF: Performance improvement value_counts for masked arrays #48338

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 6 commits into from
Sep 9, 2022

Conversation

phofl
Copy link
Member

@phofl phofl commented Aug 31, 2022

  • 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.
ser = pd.Series([1, 2, pd.NA] + list(range(1_000_000)), dtype="Int64")
ser.value_counts(dropna=True/False)

# old dropna=False
# 99.6 ms ± 599 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

# new dropna=False
# 46.1 ms ± 349 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

# old dropna=True
# 138 ms ± 639 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

# new dropna=True
# 44.6 ms ± 439 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

data = np.array(list(range(1_000_000)))
Series(data, dtype="Int64")

# old Series(numpy_array, dtype="Int64")
# 45.8 ms ± 499 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

# new
# 63.8 µs ± 329 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

cc @jorisvandenbossche

@phofl phofl added Performance Memory or execution speed performance Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Series Series data structure Constructors Series/DataFrame/Index/pd.array Constructors NA - MaskedArrays Related to pd.NA and nullable extension arrays labels Aug 31, 2022
Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Thanks, looks good!

self.data = np.array(list(range(1_000_000)))

def time_constructor(self):
Series(data=self.data, dtype="Int64")
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 we already have a benchmark for this in array.py (IntegerArray::time_from_integer_array), except that that one is with a tiny array and thus won't cover this aspect. But maybe add a version with a larger array in the existing benchmark? (or just make the array in that benchmark bigger)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thx, forgot to check for series after I found nothing for value_counts. Increased the array size for the existing benchmark. 4 values is probably a bit small to see anything with overhead from other calls that don't depend on array size

@@ -166,6 +175,19 @@ def time_value_counts(self, N, dtype):
self.s.value_counts()


class ValueCountsEa:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
class ValueCountsEa:
class ValueCountsEA:

? Small nitpick: not sure if we have some consistency around this, but if not, since we typically use EA as abbreviation, would keep it capitalized here

Copy link
Member Author

@phofl phofl Sep 2, 2022

Choose a reason for hiding this comment

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

Done, will keep in mind for future prs

@phofl phofl merged commit 191557d into pandas-dev:main Sep 9, 2022
@phofl phofl deleted the perf_nullable_value_counts branch September 9, 2022 19:10
@phofl phofl added this to the 1.6 milestone Sep 9, 2022
@mroeschke mroeschke modified the milestones: 1.6, 2.0 Oct 13, 2022
noatamir pushed a commit to noatamir/pandas that referenced this pull request Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Constructors Series/DataFrame/Index/pd.array Constructors NA - MaskedArrays Related to pd.NA and nullable extension arrays Performance Memory or execution speed performance Series Series data structure
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants