Repository navigation
Add __array_ufunc__ protocol to DataFrame and Series (#3044) - #3370
Conversation
|
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. |
| result = dsk._elemwise(self._ufunc, *args, **kwargs) | ||
| if type(result) != type(NotImplemented): | ||
| return result | ||
| return NotImplemented |
There was a problem hiding this comment.
Calling a ufunc shouldn't ever return NotImplemented. Instead, if every implementation returns NotImplemented you should raise TypeError (like how Python handles arithmetic).
| out = None | ||
|
|
||
| if out is not None and type(out) != type(result): | ||
| raise NotImplementedError( |
There was a problem hiding this comment.
I think this should be TypeError?
|
|
||
| if isinstance(out, DataFrame): | ||
| if len(out.columns) != len(result.columns): | ||
| raise NotImplementedError( |
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
I think that this should probably raise a ValueError in both cases. My apologies for the earlier implementation.
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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
```
30edd72 to
9b85874
Compare
9b85874 to
71243ae
Compare
|
@shoyer thank you for your attentive reivew. It looks like the issues that you raised have been addressed. |
| out = kwargs.get('out', ()) | ||
| for x in inputs + out: | ||
| if not isinstance(x, (Number, Scalar, _Frame, Array, | ||
| pd.DataFrame, pd.Series, pd.Index)): |
There was a problem hiding this comment.
Maybe include np.ndarray in this list?
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
We could also merge this now and handle np.ndarray in future work. The decision for this is probably up to you @almaleksia .
There was a problem hiding this comment.
@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 :)
There was a problem hiding this comment.
OK great, lets leave that for future work then
|
Merging. Thanks @almaleksia ! |
|
@mrocklin thank you :) |
|
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 :) |
|
@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 :) |
|
Happy to point you to good issues. What are your interests? Just
dataframes? Also arrays? Machine learning algorithms? Network
communication and process handling?
…On Thu, Apr 5, 2018 at 1:49 PM, Ksenia ***@***.***> wrote:
@mrocklin <https://github.com/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
:)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3370 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszK-SfpR_8gl1dFBIYFrXrdBuuSduks5tllk6gaJpZM4TFJcC>
.
|
|
@mrocklin I'm not picky but smth related to ML algos would be great! |
|
@TomAugspurger might want to chime in here
…On Thu, Apr 5, 2018 at 2:07 PM, Ksenia ***@***.***> wrote:
@mrocklin <https://github.com/mrocklin> I'm not picky but smth related to
ML algos would be great!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3370 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszBswYmAc8QvXgxtWJSkyorrhhdjDks5tll1egaJpZM4TFJcC>
.
|
|
@almaleksia you might have a look at the dask-ml repository then: https://github.com/dask/dask-ml My short-term wishlist includes
|
|
@TomAugspurger thanks a lot, I‘ll take a look :) |
|
Some additional Dask issues that may be interesting. They are either dataframe related, or are good introductions to other topics.
|
|
@mrocklin great list, thanks! |
Add array_ufunc protocol to dask.dataframe
Example
Add out parameter support
Example
flake8 daskdocs/source/changelog.rstfor all changesand one of the
docs/source/*-api.rstfiles for new API