Repository navigation
Description
What happened:
@ncclementi and I were tracking down this error on her fork with upstream dependencies:
https://github.com/ncclementi/dask/runs/5148303425?check_suite_focus=true#step:5:11490
Following a recent pandas bug enhancement (pandas-dev/pandas#45061), groupby results with bool-typed indexes are better respected. In dask, the index type generally remains bool, but groupby.mean() returns a dataframe with an object-dtyped index.
If you look at the example below, most operations return a bool index. Mean returns an object index. Interestingly, doing mathematical operations with two dataframes with bool-typed indexes returns a dataframe with an object-typed index
cc: @jrbourbeau
We were not able to track down where the fix for this would occur and need some guidance.
What you expected to happen:
Groupby.mean should preserve the index dtypes, which in turn will allow the tests to pass
Minimal Complete Verifiable Example:
import pandas as pd
import dask.dataframe as dd
pdf = pd.DataFrame(
{"a": [1, 2, 6, 4, 4, 6, 4, 3, 7], "b": [4, 2, 7, 3, 3, 1, 1, 1, 2]},
index=[0, 1, 3, 5, 6, 8, 9, 9, 9],
)
ddf = dd.from_pandas(pdf, npartitions=3)
gb = ddf.groupby(ddf.b > 4).a
assert gb.count().index.dtype == bool # good
assert gb.sum().index.dtype == bool # good
assert gb.mean().index.dtype == object # bad (implemented as sum() / count(), see link below)
## and also:
assert (gb.sum() / gb.count()).index.dtype == object # baddask/dask/dataframe/groupby.py
Lines 1424 to 1429 in d670db6
| def mean(self, split_every=None, split_out=1): | |
| s = self.sum(split_every=split_every, split_out=split_out) | |
| c = self.count(split_every=split_every, split_out=split_out) | |
| if is_dataframe_like(s): | |
| c = c[s.columns] | |
| return s / c |
Anything else we need to know?:
Environment:
- Dask version: main (d670db6)
- Pandas: upstream (1.5.0.dev0+350.g0631637205)
- Python version: 3.9.10
- Operating System: Mac OS 12.2.1 (21D62)
- Install method (conda, pip, source): source
Activity
jrbourbeau commentedon Feb 15, 2022
Thanks @phobson @ncclementi, I'm able to reproduce the dtype mismatch issue. Experimenting a bit locally, I was able to reproduce the same issue with a slightly simpler example that just divides two Dask Series (so avoiding
groupbyaltogether):Interestingly, if we actually
compute()the Dask Index, then we get the same dtype as what we would expect from pandas (i.e.assert z.index.dtype is dz.index.compute().dtype). This makes me think the issue is an issue with our DataFramemetainference. The next place I'd look would be our Seriestruedivimplementation (that should be the method that gets called when we use the/operator)phobson commentedon Feb 15, 2022
Thanks for the direction, @jrbourbeau. I'll coordinate with Naty.
jrbourbeau commentedon Feb 16, 2022
@ncclementi @phobson and I did some pairing on this issue and narrowed things down to this change being useful
Just posting this here for completeness. This may break other tests, I'm not sure yet. @ncclementi would you mind pushing up a PR to see if the test suite is happy with this change?
Fix upstream to include `bool` type for `Index` (#8732)