Skip to content

ENH: implement EA.delete #39405

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 4 commits into from
Jan 26, 2021
Merged

Conversation

jbrockmendel
Copy link
Member

  • closes #xxxx
  • tests added / passed
  • Ensure all linting tests pass, see here for how to run them
  • whatsnew entry

@jreback jreback added ExtensionArray Extending pandas with custom dtypes or arrays. Refactor Internal refactoring of code labels Jan 25, 2021
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

following comment

@@ -257,6 +257,17 @@ def searchsorted(self, value, side="left", sorter=None) -> np.ndarray:
def _get_engine_target(self) -> np.ndarray:
return np.asarray(self._data)

def delete(self, loc):
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

prob could add types back here

Copy link
Member Author

Choose a reason for hiding this comment

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

mypy complained so i punted

@jreback jreback added this to the 1.3 milestone Jan 26, 2021
@jreback jreback merged commit 3f07983 into pandas-dev:master Jan 26, 2021
@jbrockmendel jbrockmendel deleted the ref-share-methods branch January 26, 2021 03:01
@TomAugspurger
Copy link
Contributor

Mmm @jbrockmendel what was the motivation for this change (can you just always provide that, for future readers?) In general I think we should carefully consider adding new methods, since that increases the burden on extension array maintainers to write efficient implementations.

If we are adding methods to the public API then they should be documented. This would need a docstring and it should be included in the class docstring.

@jbrockmendel
Copy link
Member Author

An arbitrary EA-backed Index will need a delete implementation.

@TomAugspurger
Copy link
Contributor

I assume there are calls to np.delete in core/indexes or core/indexing? Can those calls be rewritten to use take instead of np.delete?

@jbrockmendel
Copy link
Member Author

There is an Index.delete method that would need to be implemented. So de-facto we have to implement a delete method or something equivalent to it.

(np.delete wont work directly until we get __array_function__ up and running)

@TomAugspurger
Copy link
Contributor

There is an Index.delete method that would need to be implemented. So de-facto we have to implement a delete method or something equivalent to it.

meta-comment: this type of context is useful for evaluating PRs. Can you include motivation when creating a PR?

My preference would be to rewrite Index.delete to use a take or masked-based implementation, if it's not slower than the np.delete implementation. And np.delete seems to be implemented in Python, so I imagine we can make it equally fast.

@jbrockmendel
Copy link
Member Author

if it's not slower than the np.delete implementation

Maybe there's a faster take/getitem-based imlpementation, but

In [3]: arr = np.arange(1000)

In [4]: %timeit np.delete(arr, 500)
4.49 µs ± 35.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [5]: %timeit mask = np.delete(np.arange(1000), 500); arr.take(mask.nonzero()[0])
13.6 µs ± 284 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [6]: %timeit mask = np.delete(np.arange(1000), 500); arr[mask]
7.83 µs ± 279 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. Refactor Internal refactoring of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants