Skip to content
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

Map partitions on a series that yields a scalar per partition cannot be computed #625

Open
fjetter opened this issue Dec 22, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@fjetter
Copy link
Member

fjetter commented Dec 22, 2023

from dask_expr.datasets import timeseries
from dask.utils import M

df = timeseries()
df.x.map_partitions(M.min).compute()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 df.x.map_partitions(M.min).compute()

File ~/workspace/dask-expr/dask_expr/_collection.py:265, in FrameBase.compute(self, fuse, **kwargs)
    263     out = out.repartition(npartitions=1)
    264 out = out.optimize(fuse=fuse)
--> 265 return DaskMethodsMixin.compute(out, **kwargs)

File ~/workspace/dask/dask/base.py:342, in DaskMethodsMixin.compute(self, **kwargs)
    318 def compute(self, **kwargs):
    319     """Compute this dask collection
    320
    321     This turns a lazy Dask collection into its in-memory equivalent.
   (...)
    340     dask.compute
    341     """
--> 342     (result,) = compute(self, traverse=False, **kwargs)
    343     return result

File ~/workspace/dask/dask/base.py:628, in compute(traverse, optimize_graph, scheduler, get, *args, **kwargs)
    625     postcomputes.append(x.__dask_postcompute__())
    627 with shorten_traceback():
--> 628     results = schedule(dsk, keys, **kwargs)
    630 return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])

File ~/workspace/dask/dask/dataframe/dispatch.py:67, in concat(dfs, axis, join, uniform, filter_warning, ignore_index, **kwargs)
     65     return dfs[0]
     66 else:
---> 67     func = concat_dispatch.dispatch(type(dfs[0]))
     68     return func(
     69         dfs,
     70         axis=axis,
   (...)
     75         **kwargs,
     76     )

File ~/workspace/dask/dask/utils.py:635, in Dispatch.dispatch(self, cls)
    633         self._lazy.pop(toplevel, None)
    634         return self.dispatch(cls)  # recurse
--> 635 raise TypeError(f"No dispatch for {cls}")

TypeError: No dispatch for <class 'numpy.float64'>

So far, I believe the reason for this is that the min is returning a scalar float (the type that is tried to be dispatched) instead of a Series with one element. This then fails in the concat operation when computing the series.

@fjetter fjetter added the bug Something isn't working label Dec 22, 2023
@phofl
Copy link
Collaborator

phofl commented Dec 22, 2023

Yes that's correct. The underlying issue is that we inject a df.repartition(npartitions=1) when computing and this uses methods.concat instead of _concat that post compute uses. A repro for dask/dask:

import pandas as pd

from dask.utils import M
from dask.dataframe import from_pandas

df = from_pandas(pd.DataFrame({"x": [1, 2, 3]}), npartitions=2)
df.x.map_partitions(M.min).repartition(npartitions=1).compute()

We should probably ensure that map_partitions returns a Series at least, wdyt?

@fjetter
Copy link
Member Author

fjetter commented Dec 22, 2023

Yeah, I just found the issue. This debugging experience wasn't pleasant.

Why can't repartition just use _concat?

@phofl
Copy link
Collaborator

phofl commented Dec 22, 2023

I do think that having integers as partitions is not ideal, this will most like cause all kinds of issues

df = from_pandas(pd.DataFrame({"x": [1, 2, 3]}), npartitions=2)
df.x.map_partitions(M.min).count().compute()

But not averse to using _concat as well

@fjetter
Copy link
Member Author

fjetter commented Dec 22, 2023

Yes, that makes sense.

@phofl
Copy link
Collaborator

phofl commented Dec 22, 2023

We have 2 options here:

  • raise in apply_and_enforce if meta is a Series and we are a scalar
  • Convert scalars to a Series

I think I am leaning slightly towards just making this work

@fjetter
Copy link
Member Author

fjetter commented Dec 22, 2023

Convert scalars to a Series
I think I am leaning slightly towards just making this work

Agreed, I'm already on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants