Skip to content

PERF: future_stack=True with non-MulitIndex columns #58817

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 10 commits into from
Apr 14, 2025

Conversation

rhshadrach
Copy link
Member

@rhshadrach rhshadrach commented May 24, 2024

df = pd.DataFrame(np.random.random(size=(10000, 100)))
%timeit df.unstack()
17.2 ms ± 159 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)  <--- PR
205 ms ± 3.37 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)     <--- main

@rhshadrach rhshadrach added Performance Memory or execution speed performance Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels May 24, 2024
@rhshadrach rhshadrach added this to the 3.0 milestone May 24, 2024
@rhshadrach
Copy link
Member Author

@jbrockmendel - the failing tests are:

FAILED pandas/tests/extension/test_numpy.py::TestNumpyExtensionArray::test_stack[float-True-columns0] - AssertionError: (<class 'numpy.dtypes.Float64DType'>, dtype('float64')) vs (<class 'pandas.core.dtypes.dtypes.NumpyEADtype'>, NumpyEADtype('float64'))
FAILED pandas/tests/extension/test_numpy.py::TestNumpyExtensionArray::test_stack[object-True-columns0] - AssertionError: (<class 'numpy.dtypes.ObjectDType'>, dtype('O')) vs (<class 'pandas.core.dtypes.dtypes.NumpyEADtype'>, NumpyEADtype('object'))

This happens because of some monkey-patching

https://github.com/pandas-dev/pandas/blame/3b48b17e52f3f3837b9ba8551c932f44633b5ff8/pandas/tests/extension/test_numpy.py#L72-L73

It's not clear to me if the code should handle this case, or if this behavior is okay and the test should be modified. Was wondering if you had any experience dealing with this.

Comment on lines +944 to +945
if len(frame.columns) > 0 and frame._is_homogeneous_type:
dtype = frame._mgr.blocks[0].dtype
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if there is a more canonical way to get the dtype in this case.

@jbrockmendel
Copy link
Member

The behavior is fine, the test should be patched

Copy link
Contributor

github-actions bot commented Jul 3, 2024

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 Jul 3, 2024
@rhshadrach rhshadrach requested a review from mroeschke April 13, 2025 17:18
@rhshadrach rhshadrach marked this pull request as ready for review April 13, 2025 17:18
@rhshadrach rhshadrach modified the milestones: 3.0, 2.3 Apr 13, 2025
@mroeschke mroeschke merged commit ca20ea9 into pandas-dev:main Apr 14, 2025
42 checks passed
@mroeschke
Copy link
Member

Thanks @rhshadrach

@rhshadrach rhshadrach deleted the perf_stack_index branch April 14, 2025 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Performance Memory or execution speed performance Reshaping Concat, Merge/Join, Stack/Unstack, Explode Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PERF: df.unstack() is 500 times slower since pandas>=2.1
3 participants