Skip to content

PERF: sparse take #43654

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 21, 2021
Merged

PERF: sparse take #43654

merged 6 commits into from
Sep 21, 2021

Conversation

mzeitlin11
Copy link
Member

@mzeitlin11 mzeitlin11 commented Sep 19, 2021

This helps some on #41023 because __getitem__ uses take internally for SparseArray. However, the big gain there can follow after this to move towards not densifying on __getitem__ with a slice

Benchmarks:

       before           after         ratio
     [e7e7b407]       [4de1fe27]
     <master>         <perf_sparse_take>
-        72.7±6μs         38.8±4μs     0.53  sparse.Take.time_take(array([0]), False)
-     1.00±0.06ms          350±4μs     0.35  sparse.Take.time_take(array([-1, -1, -1, ..., -1, -1, -1]), False)
-        737±20μs          206±6μs     0.28  sparse.Take.time_take(array([    0,     1,     2, ..., 99997, 99998, 99999]), False)

@mzeitlin11 mzeitlin11 added Indexing Related to indexing on series/frames, not to indexes themselves Performance Memory or execution speed performance Sparse Sparse Data Type labels Sep 19, 2021
tm.assert_sp_array_equal(result, expected)

result = sparse.take(np.array([1, 0, -1]), fill_value=True)
expected = SparseArray([np.nan, np.nan, np.nan], kind="block")
expected = SparseArray([np.nan, np.nan, np.nan], kind=kind)
Copy link
Member Author

Choose a reason for hiding this comment

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

In all other take cases, we preserve the kind of SparseIndex, so I think it makes sense to do so here (otherwise is surprising value-dependent behavior IMO, somewhat expressed in the comment originally here).


return taken
new_sp_index = make_sparse_index(len(indices), value_indices, kind=self.kind)
return type(self)._simple_new(new_sp_values, new_sp_index, dtype=self.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.

Main reasoning for the simplifications here keeping same behavior is that take with allow_fill=False should give a result with the same dtype as our SparseArray

@jreback jreback added this to the 1.4 milestone Sep 20, 2021
@jreback
Copy link
Contributor

jreback commented Sep 20, 2021

lgtm. cc @jbrockmendel

@@ -354,6 +354,7 @@ Performance improvements
- Performance improvement in indexing with a :class:`MultiIndex` indexer on another :class:`MultiIndex` (:issue:43370`)
- Performance improvement in :meth:`GroupBy.quantile` (:issue:`43469`)
- :meth:`SparseArray.min` and :meth:`SparseArray.max` no longer require converting to a dense array (:issue:`43526`)
- Performance improvement in :meth:`SparseArray.take` with ``allow_fill=False`` (:issue:`?`)
Copy link
Member

Choose a reason for hiding this comment

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

fill in ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks!

@jreback jreback merged commit a5264b8 into pandas-dev:master Sep 21, 2021
@jreback
Copy link
Contributor

jreback commented Sep 21, 2021

thanks @mzeitlin11

@mzeitlin11 mzeitlin11 deleted the perf_sparse_take branch September 21, 2021 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Performance Memory or execution speed performance Sparse Sparse Data Type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants