Skip to content

PERF: 34% faster Series.to_dict #50089

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 1 commit into from
Dec 8, 2022
Merged

Conversation

staadecker
Copy link
Contributor

@staadecker staadecker commented Dec 6, 2022

Improve the performance of Series.to_dict() by not including a generator that is not necessary. Local tests showed significant performance improvements.

Note this builds off the changes of #46487 from @RogerThomas

Improve the performance of Series.to_dict() by not including a generator that is not necessary. Local tests showed significant performance improvements.
@rhshadrach
Copy link
Member

Nice! Can you post the results from your local test?

@rhshadrach rhshadrach added IO Data IO issues that don't fit into a more specific label Enhancement Performance Memory or execution speed performance and removed Enhancement labels Dec 6, 2022
@rhshadrach rhshadrach added this to the 2.0 milestone Dec 6, 2022
@staadecker
Copy link
Contributor Author

Here is the test. Performance improves by 34%

import pandas as pd
import random
from timeit import timeit
df = pd.Series([random.random() for _ in range(1000)], dtype=float)
print(timeit("df.to_dict()", globals=globals(), number=10000))

Before change: 2.49s
After change: 1.64s

@staadecker staadecker changed the title PERF: Faster Series.to_dict PERF: 34% faster Series.to_dict Dec 7, 2022
@staadecker
Copy link
Contributor Author

Note the performance gain only applies when the series uses Python native types. Also out of curiosity, what's the release timeline for Pandas 2.0?

Copy link
Contributor

@RogerThomas RogerThomas left a comment

Choose a reason for hiding this comment

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

LGTM, sorry, should have noticed that the generator was no longer necessary in my PR

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

lgtm. No need for an entry in the whatsnew, the whatsnew from the PR linked in the OP here covers this as well.

@rhshadrach rhshadrach merged commit e61e58e into pandas-dev:main Dec 8, 2022
@rhshadrach
Copy link
Member

Thanks @staadecker - very nice!

@staadecker staadecker deleted the patch-1 branch December 9, 2022 02:34
@staadecker
Copy link
Contributor Author

Thank you all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO Data IO issues that don't fit into a more specific label Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants