Skip to content

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
snitish pushed a commit to snitish/pandas that referenced this pull request Jun 9, 2025
* PERF: stack on non-MultiIndex columns

* WIP

* Use reshape instead of ravel

* arrays -> blocks

* Update test

* whatsnew
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