Skip to content

Add __array_ufunc__ protocol to DataFrame and Series (#3044) - #3370

Merged
mrocklin merged 2 commits into
dask:masterfrom
almaleksia:df_array_ufunc
Apr 5, 2018
Merged

mrocklin merged 2 commits into
dask:masterfrom
almaleksia:df_array_ufunc

Conversation

@almaleksia

@almaleksia almaleksia commented Apr 3, 2018 •

Copy link
Copy Markdown
Contributor
  • Add array_ufunc protocol to dask.dataframe

    Example

    In [1]: import numpy as np, dask.dataframe as dd, pandas as pd
    In [2]: numpy_arr = np.random.randn(5)
    In [3]: ps = pd.Series(numpy_arr)
    In [5]: np.sum(np.ceil(ps))
    Out[5]: 2.0
    In [6]: ds = dd.from_pandas(ps,3)
    In [9]: np.sum(np.ceil(ds))
    Out[5]: dd.Scalar<series-..., dtype=float64>
    In [6]: _.compute()
    Out[6]: 2.0
  • Add out parameter support

    Example

    In [1]: import numpy as np, dask.dataframe as dd, pandas as pd
    In [2]: ps = pd.Series(np.random.randn(5))
    In [3]: ds = dd.from_pandas(ps, 3)
    In [4]: dsc_out = dd.core.Scalar({('s', 0): 0}, 's', 'i8')
    In [5]: np.sum(ds, out=dsc_out)
    In [6]: dsc_out
    Out[6]: dd.Scalar<series-..., dtype=float64>
    In [7]: dsc_out.compute()
    Out[7]: 0.2768562049182187
    In [8]: np.sum(ps)
    Out[8]: 0.2768562049182187
  • Tests added / passed
  • Passes flake8 dask
  • Fully documented, including docs/source/changelog.rst for all changes
    and one of the docs/source/*-api.rst files for new API

@mrocklin

mrocklin commented Apr 3, 2018

Copy link
Copy Markdown
Member

I've only briefly gone through this, but in general this looks excellent to me. I'm really looking forward to playing around with it.

cc @TomAugspurger @shoyer you may both find this interesting. Also cc @sinhrks if he's around.

Comment thread dask/array/ufunc.py Outdated
result = dsk._elemwise(self._ufunc, *args, **kwargs)
if type(result) != type(NotImplemented):
return result
return NotImplemented

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Calling a ufunc shouldn't ever return NotImplemented. Instead, if every implementation returns NotImplemented you should raise TypeError (like how Python handles arithmetic).

Comment thread dask/dataframe/core.py Outdated
out = None

if out is not None and type(out) != type(result):
raise NotImplementedError(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this should be TypeError?

Comment thread dask/dataframe/core.py Outdated

if isinstance(out, DataFrame):
if len(out.columns) != len(result.columns):
raise NotImplementedError(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ValueError?

@almaleksia almaleksia Apr 3, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@shoyer I've made it similar to handle_out version for array that raises NotImplementedError in case of shape mismatch. Should I then change it for array as well to keep behaviour consistent?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that this should probably raise a ValueError in both cases. My apologies for the earlier implementation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, a fix for dask.array would also be appreciated!

@pytest.mark.parametrize('redfunc', ['sum', 'prod', 'min', 'max', 'mean', 'var', 'std'])
def test_reductions_out(frame, axis, out, redfunc):
dsk_in = dd.from_pandas(frame, 3)
dsk_out = dd.core.Scalar({('a', 0): np.int64(0)}, 'a', 'i8')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I recommend avoiding creating Dask objects by hand. This API is less reliable than the user/pandas API. Instead I recommend creating a scalar using Pandas API on a dask dataframe:

dsk_in = dd.from_pandas(frame, 3)
dsk_out = dsk_in.a.sum()

  * Add __array_ufunc__ protocol to dask.dataframe

    Example
    -------

    ```python
    In [1]: import numpy as np, dask.dataframe as dd, pandas as pd
    In [2]: numpy_arr = np.random.randn(5)
    In [3]: ps = pd.Series(numpy_arr)
    In [5]: np.sum(np.ceil(ps))
    Out[5]: 2.0
    In [6]: ds = dd.from_pandas(ps,3)
    In [9]: np.sum(np.ceil(ds))
    Out[5]: dd.Scalar<series-..., dtype=float64>
    In [6]: _.compute()
    Out[6]: 2.0
    ```
  * Add out parameter support

    Example
    -------

    ```python
    In [1]: import numpy as np, dask.dataframe as dd, pandas as pd
    In [2]: ps = pd.Series(np.random.randn(5))
    In [3]: ds = dd.from_pandas(ps, 3)
    In [4]: dsc_out = dd.core.Scalar({('s', 0): 0}, 's', 'i8')
    In [5]: np.sum(ds, out=dsc_out)
    In [6]: dsc_out
    Out[6]: dd.Scalar<series-..., dtype=float64>
    In [7]: dsc_out.compute()
    Out[7]: 0.2768562049182187
    In [8]: np.sum(ps)
    Out[8]: 0.2768562049182187
    ```
@mrocklin

mrocklin commented Apr 4, 2018

Copy link
Copy Markdown
Member

@shoyer thank you for your attentive reivew. It looks like the issues that you raised have been addressed.
Do you have any further comments on this?

@shoyer shoyer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me!

Comment thread dask/dataframe/core.py
out = kwargs.get('out', ())
for x in inputs + out:
if not isinstance(x, (Number, Scalar, _Frame, Array,
pd.DataFrame, pd.Series, pd.Index)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe include np.ndarray in this list?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@shoyer sure. Currently ndarray is not handled as expected in ufunc wrapper for dataframes. I will need to dig a bit more into it to get it work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could also merge this now and handle np.ndarray in future work. The decision for this is probably up to you @almaleksia .

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@mrocklin thank you. I debugged a bit and discovered several problems with np.ndarray there. The only approach that kind of worked (at least didn't break any tests) is converting ndarray to dask array in elemwise method (the chunksize for array in case first arg is dataframe would be (np.ceil(arr.shape[0] / df.npartitions), len(df.columns)) ). But I'm not yet confident enough to modify this method as it does a lot of manipulations I don't yet fully understand :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK great, lets leave that for future work then

@mrocklin

mrocklin commented Apr 5, 2018

Copy link
Copy Markdown
Member

Merging. Thanks @almaleksia !

@almaleksia

Copy link
Copy Markdown
Contributor Author

@mrocklin thank you :)

@mrocklin
mrocklin merged commit b64e694 into dask:master Apr 5, 2018
@mrocklin

mrocklin commented Apr 5, 2018

Copy link
Copy Markdown
Member

This is one of the nicer first PRs I've seen.

If there is anything we can do to keep you busy here please don't hesitate to speak up :)

@almaleksia

Copy link
Copy Markdown
Contributor Author

@mrocklin thank you for your feedback, I really appreciate it. I'll be happy to contribute further as soon as I find another issue that I could do. If you have something on mind I can take it :)

@mrocklin

mrocklin commented Apr 5, 2018 via email

Copy link
Copy Markdown
Member

@almaleksia

Copy link
Copy Markdown
Contributor Author

@mrocklin I'm not picky but smth related to ML algos would be great!

@mrocklin

mrocklin commented Apr 5, 2018 via email

Copy link
Copy Markdown
Member

@TomAugspurger

Copy link
Copy Markdown
Member

@almaleksia you might have a look at the dask-ml repository then: https://github.com/dask/dask-ml

My short-term wishlist includes

@almaleksia

Copy link
Copy Markdown
Contributor Author

@TomAugspurger thanks a lot, I‘ll take a look :)

@mrocklin

mrocklin commented Apr 5, 2018

Copy link
Copy Markdown
Member

Some additional Dask issues that may be interesting. They are either dataframe related, or are good introductions to other topics.

@almaleksia

Copy link
Copy Markdown
Contributor Author

@mrocklin great list, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants