You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Different categories in different partitions #1836
Categoricals are currently difficult in dask.dataframe because all partitions must have the same set of categories. This synchronization can be very expensive, often requiring full table scans. Operations like .astype('category') or read_csv(..., dtypes={'x': 'category'}) don't work as expected.
Recently pandas started to support union-ing categoricals on concatenation. This ability might allow dask.dataframe to instead maintain categories per-partition, which removes the burden of synchronization. We would only combine categoricals when different partitions need to interact. This would help us in data loading (csv, parquet) and in aggregations (groupbys, etc.) but not for operations like pivot_table which expect to know the categories ahead of time.
This might be as easy as adding the right call to union_categoricals in dask.dataframe.core._concat, but there are probably other nuances.
Quick note: getting the full set of categories is very fast if the data is in memory (df.col.cat.categories as opposed to unique).
Also, specifically for parquet: if the data was written by fastparquet (via dask or not), then we can guarantee that the categories will be the same for each chunk.
If we remove the requirement that Dask.dataframe partitions all agree on categories then we might want to change the dask.dataframe parquet writer so that it writes individual partitions without synchronizing categories beforehand.
I think this is a good idea... This should be transparent to the user, correct? It'll be up to dask to know when to union categoricals in the background?
Operations like .astype('category') or read_csv(..., dtypes={'x': 'category'}) don't work as expected.
This should be less painful once I finish the CategoricalDtype refactoring in pandas, for the cases where you know the set of categories ahead of time. The proposal here would still be useful for the unknown categories case.
short version: move categories and ordered into the dtype itself. Then add that to the public API and let users say pd.read_csv('...', dtype={'a': pd.CategoricalDtype(['a', 'b', 'c'], ordered=True)}). Should be entirely backwards compatible.
@TomAugspurger can you help me create a list of operations that this change will affect?
Operations that become harder
pivot_table
df.x.cat.codes
df.x.cat.categories
Operations that requires intelligent merging
Anything relying on apply-concat-apply (groupby-aggregations, reductions, drop_duplicates, etc.)
merge?
groupby-apply?
What is the right way to merge two dataframes with categories?
In [1]: import pandas as pd
In [2]: a = pd.DataFrame({'x': [1, 2, 3], 'y': ['a', 'b', 'a']})
In [3]: b = pd.DataFrame({'x': [4, 5, 6], 'y': ['a', 'c', 'c']})
In [4]: a['y'] = a.y.astype('category')
In [5]: b['y'] = b.y.astype('category')
In [6]: c = ... # concatenate a and b, respecting categories
@martindurant this change might affect dask's parquet reading and writing, which no longer needs to have a consistent set of categories in the metadata of all partitions.
Writing will not be affected, since each partition is treated separately.
On reading, this should actually simplify things a bit, I think. Am I right in thinking that the change here just means that the labels are not the same everywhere, but we do guarantee that the given column is categorical everywhere?
Am I right in thinking that the change here just means that the labels are not the same everywhere, but we do guarantee that the given column is categorical everywhere?
I have a good feeling that removing L80-84, 123-124 in dask.io.parquet will do the right thing. I'll test that (and make a specific test) when I get to it.
Does the metadata now not need to know about category labels at all?
Unknown categoricals should include dd.utils.UNKNOWN_CATEGORIES in their categories. You could either build each categorical yourself, or use dd.utils.clear_known_categories, which can be used to set known categorical columns to unknown.
The writing and reading seem to magically just work right now, although the lines above cause an unnecessary preemptive read.
I realise, though, that reading categoricals in fastparquet itself does require the same labels in every row-group, which I think is documented, but may be surprising.
Activity
martindurant commentedon Dec 8, 2016
Quick note: getting the full set of categories is very fast if the data is in memory (df.col.cat.categories as opposed to unique).
Also, specifically for parquet: if the data was written by fastparquet (via dask or not), then we can guarantee that the categories will be the same for each chunk.
mrocklin commentedon Dec 8, 2016
If we remove the requirement that Dask.dataframe partitions all agree on categories then we might want to change the dask.dataframe parquet writer so that it writes individual partitions without synchronizing categories beforehand.
TomAugspurger commentedon Dec 12, 2016
I think this is a good idea... This should be transparent to the user, correct? It'll be up to dask to know when to union categoricals in the background?
This should be less painful once I finish the CategoricalDtype refactoring in pandas, for the cases where you know the set of categories ahead of time. The proposal here would still be useful for the unknown categories case.
martindurant commentedon Dec 12, 2016
"CategoricalDtype refactoring" - is there a link for this?
TomAugspurger commentedon Dec 12, 2016
@martindurant pandas-dev/pandas#14711
short version: move
categoriesandorderedinto the dtype itself. Then add that to the public API and let users saypd.read_csv('...', dtype={'a': pd.CategoricalDtype(['a', 'b', 'c'], ordered=True)}). Should be entirely backwards compatible.mrocklin commentedon Dec 12, 2016
@TomAugspurger can you help me create a list of operations that this change will affect?
Operations that become harder
pivot_tabledf.x.cat.codesdf.x.cat.categoriesOperations that requires intelligent merging
What is the right way to merge two dataframes with categories?
martindurant commentedon Dec 12, 2016
http://pandas.pydata.org/pandas-docs/stable/categorical.html#unioning
The function, not yet in concat, is
(series by series)
mrocklin commentedon Jan 23, 2017
@martindurant this change might affect dask's parquet reading and writing, which no longer needs to have a consistent set of categories in the metadata of all partitions.
martindurant commentedon Jan 23, 2017
Writing will not be affected, since each partition is treated separately.
On reading, this should actually simplify things a bit, I think. Am I right in thinking that the change here just means that the labels are not the same everywhere, but we do guarantee that the given column is categorical everywhere?
mrocklin commentedon Jan 23, 2017
Correct
martindurant commentedon Jan 23, 2017
I have a good feeling that removing L80-84, 123-124 in dask.io.parquet will do the right thing. I'll test that (and make a specific test) when I get to it.
Does the metadata now not need to know about category labels at all?
mrocklin commentedon Jan 23, 2017
I believe that @jcrist established a convention around what to specify if categories are unknown.
jcrist commentedon Jan 23, 2017
Unknown categoricals should include
dd.utils.UNKNOWN_CATEGORIESin their categories. You could either build each categorical yourself, or usedd.utils.clear_known_categories, which can be used to set known categorical columns to unknown.martindurant commentedon Jan 23, 2017
The writing and reading seem to magically just work right now, although the lines above cause an unnecessary preemptive read.
I realise, though, that reading categoricals in fastparquet itself does require the same labels in every row-group, which I think is documented, but may be surprising.
mrocklin commentedon Jan 23, 2017
Here is a fail case:
I suggest that we don't use the categories of the first partition in read_parquet, but instead use the unknown categories convention.
martindurant commentedon Jan 23, 2017
OK, so read_parquet should call dd.utils.clear_known_categories, and then will have the same behaviour.
mrocklin commentedon Jan 23, 2017
In these lines:
We probably need to replace
cats[cat]with[dd.utils.UNKNOWN_CATEGORIES].