Skip to content

Add back __array_wrap__ for dask compatibility #45451

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 5 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,41 @@ def empty(self) -> bool_t:
def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
return np.asarray(self._values, dtype=dtype)

def __array_wrap__(
self,
result: np.ndarray,
context: tuple[Callable, tuple[Any, ...], int] | None = None,
):
"""
Gets called after a ufunc and other functions.

Parameters
----------
result: np.ndarray
The result of the ufunc or other function called on the NumPy array
returned by __array__
context: tuple of (func, tuple, int)
This parameter is returned by ufuncs as a 3-element tuple: (name of the
ufunc, arguments of the ufunc, domain of the ufunc), but is not set by
other numpy functions.q

Notes
-----
Series implements __array_ufunc_ so this not called for ufunc on Series.
"""
# Note: at time of dask 2022.01.0, this is still used by dask
res = lib.item_from_zerodim(result)
if is_scalar(res):
# e.g. we get here with np.ptp(series)
# ptp also requires the item_from_zerodim
return res
d = self._construct_axes_dict(self._AXIS_ORDERS, copy=False)
# error: Argument 1 to "NDFrame" has incompatible type "ndarray";
# expected "BlockManager"
return self._constructor(res, **d).__finalize__( # type: ignore[arg-type]
self, method="__array_wrap__"
)

@final
def __array_ufunc__(
self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/base/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ def test_ndarray_compat_properties(index_or_series_obj):
assert Series([1]).item() == 1


def test_array_wrap_compat():
# Note: at time of dask 2022.01.0, this is still used by eg dask
# (https://github.com/dask/dask/issues/8580).
# This test is a small dummy ensuring coverage
orig = Series([1, 2, 3], dtype="int64", index=["a", "b", "c"])
result = orig.__array_wrap__(np.array([2, 4, 6], dtype="int64"))
expected = orig * 2
tm.assert_series_equal(result, expected)


@pytest.mark.skipif(PYPY, reason="not relevant for PyPy")
def test_memory_usage(index_or_series_obj):
obj = index_or_series_obj
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/test_downstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ def test_dask(df):
pd.set_option("compute.use_numexpr", olduse)


@pytest.mark.filterwarnings("ignore:.*64Index is deprecated:FutureWarning")
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 use the import decorator here e.g. @td.skip_if_no("xarray" (and can you fix the L39 one

i also don't know why we have a special import_module 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.

This seems to be done on purpose:

def import_module(name):
# we *only* want to skip if the module is truly not available
# and NOT just an actual import error because of pandas changes
try:
return importlib.import_module(name)
except ModuleNotFoundError:
pytest.skip(f"skipping as {name} not available")

Copy link
Contributor

Choose a reason for hiding this comment

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

ok really this should be cleaned up cc @jbrockmendel

def test_dask_ufunc():
# At the time of dask 2022.01.0, dask is still directly using __array_wrap__
# for some ufuncs (https://github.com/dask/dask/issues/8580).

# dask sets "compute.use_numexpr" to False, so catch the current value
# and ensure to reset it afterwards to avoid impacting other tests
olduse = pd.get_option("compute.use_numexpr")

try:
dask = import_module("dask") # noqa:F841
import dask.array as da
import dask.dataframe as dd

s = pd.Series([1.5, 2.3, 3.7, 4.0])
ds = dd.from_pandas(s, npartitions=2)

result = da.fix(ds).compute()
expected = np.fix(s)
tm.assert_series_equal(result, expected)
finally:
pd.set_option("compute.use_numexpr", olduse)


def test_xarray(df):

xarray = import_module("xarray") # noqa:F841
Expand Down