Skip to content

ENH: NDArrayBackedExtensionArray.__array_function__ #38068

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

Closed

Conversation

jbrockmendel
Copy link
Member

@jbrockmendel jbrockmendel commented Nov 25, 2020

motivated by getting np.delete and np.repeat working, had to implement a few others to get the tests passing.

If we go down this path, I plan to incrementally add support for others, some of which (e.g. vstack) will avoid some object-dtype casting.

cc @TomAugspurger @shoyer @seberg any suggestions to clean the implementation, particularly the fallback and the unknown-args comments?

@jbrockmendel
Copy link
Member Author

@TomAugspurger meant to cc you in the OP but made a typo

@jreback jreback added the ExtensionArray Extending pandas with custom dtypes or arrays. label Nov 26, 2020
@@ -304,6 +304,68 @@ def __repr__(self) -> str:
# ------------------------------------------------------------------------
# __array_function__ methods

def __array_function__(self, func, types, args, kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

array_ufunc now exists in pandas/core/arraylike.py we should share as much as possible here (might mean breaking up the existing code).

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the only shared code will be the checking of the types. We've standardized on a pattern like

class Foo:
    _HANDLED_TYPES = (...,)

And then ensuring that the set of types in types is a subset of _HANDLED_TYPES + (type(self),).

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.

will look

@@ -62,9 +62,25 @@ def np_array_datetime64_compat(arr, *args, **kwargs):
return np.array(arr, *args, **kwargs)


def _is_nep18_active():
Copy link
Contributor

Choose a reason for hiding this comment

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

We require NumPy>1.16.0, so this can maybe be simplified to

IS_NEP18_ACTIVE = not os.environ.get("NUMPY_EXPERIMENTAL_ARRAY_FUNCTION", "1") == "0"

See https://numpy.org/neps/nep-0018-array-function-protocol.html#implementation.

@@ -304,6 +304,68 @@ def __repr__(self) -> str:
# ------------------------------------------------------------------------
# __array_function__ methods

def __array_function__(self, func, types, args, kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the only shared code will be the checking of the types. We've standardized on a pattern like

class Foo:
    _HANDLED_TYPES = (...,)

And then ensuring that the set of types in types is a subset of _HANDLED_TYPES + (type(self),).

Comment on lines +333 to +334
res_data = func(self._ndarray, *args[1:], **kwargs)
return self._from_backing_data(res_data)
Copy link
Contributor

Choose a reason for hiding this comment

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

How do we know that there's 1 output argument from func here? Do we have a func.nout or something like that to check?

Copy link
Member Author

Choose a reason for hiding this comment

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

we don't, thats why we're implementing only a specific handful of functions here

if not issubclass(x, (np.ndarray, NDArrayBackedExtensionArray)):
return NotImplemented

if not args:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you give a code example of this path?

Copy link
Member Author

Choose a reason for hiding this comment

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

np.delete(arr=np.arange(5), obj=4)

@jbrockmendel
Copy link
Member Author

Closing to clear the queue; won't be super-useful until our min numpy version is 1.17

@jbrockmendel jbrockmendel deleted the enh-array_function branch December 21, 2020 21:37
@jbrockmendel jbrockmendel added the Mothballed Temporarily-closed PR the author plans to return to label Jan 18, 2021
@jbrockmendel jbrockmendel removed the Mothballed Temporarily-closed PR the author plans to return to label Mar 2, 2021
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants