Repository navigation
Loading of partitioned parquet should result in known categories - #3216
Conversation
Fix for the fastparquet side
|
Fixes #3186 @andrethrill , would you mind checking if this works for you? If yes, I'll add tests here. |
| pf.file_scheme, storage_name_mapping) | ||
| for i, rg in enumerate(rgs)} | ||
| for catcol in pf.cats: | ||
| meta[catcol] = meta[catcol].cat.set_categories(pf.cats[catcol]) |
There was a problem hiding this comment.
I don't think this works if columns isn't a superset of partition_on, as meta wouldn't contain those column names originally (e.g. if I wrote a file with partition_on=['a', 'b'], and read with columns=['b'], this would error as 'a' isn't in the metadata, but is in pf.cats).
If my intuition is correct, then a test should be expanded to hit a combination of partition_on and columns that would have triggered this error, as currently this code passed our test suite.
There was a problem hiding this comment.
You may be right, I'll check. I guess the partitioned column could also end up as the index?
|
@jcrist back to you - I think this is good now. |
|
@martindurant I'm sorry but I'm just not having the time this week. I will try this next week if it's still relevant. |
|
I would merge this if there are no further comments. Leaving it live until ~Monday evening. |
|
@martindurant if I understood correctly, this is already very helpful. But it is still no the same behavior as in Pandas. For instance, taking from the function d.to_parquet(tmp, partition_on=['b', 'c'], engine='fastparquet')
df = dd.read_parquet(tmp, engine='fastparquet')
assert set(df.b.cat.categories) == {'x', 'y', 'z'}
assert set(df.c.cat.categories) == {'x', 'y', 'z'}this assumes the user knows which columns is the file partitioned on. In pandas, one can do something like: assert set(df.cats) == [('b', {'x', 'y', 'z'}), ('c', {'x', 'y', 'z'}) ]which is great because you have a birds eye view of what all the partitions are, and what are their partitioning values. If this is something easy to add quickly, it would be great to have it available as well. |
|
I don't think it's a good idea to add extra attributes to the data-frame to cover this - you can get the information by looking at the dtypes for columns with known categoricals: (I think you were referring to the |
Are you sure? AFAIK pandas only has a In [1]: import pandas as pd
In [2]: df = pd.DataFrame({'a': ['x', 'y', 'y'], 'b': ['x', 'y', 'z'], 'c': [1, 2, 3]})
In [3]: df.a = df.a.astype('category')
In [4]: df.b = df.b.astype('category')
In [5]: df.cats
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-f3ac149913d6> in <module>()
----> 1 df.cats
~/anaconda/envs/dask/lib/python3.5/site-packages/pandas/core/generic.py in __getattr__(self, name)
3612 if name in self._info_axis:
3613 return self[name]
-> 3614 return object.__getattribute__(self, name)
3615
3616 def __setattr__(self, name, value):
AttributeError: 'DataFrame' object has no attribute 'cats'
In [6]: pd.__version__
Out[6]: '0.22.0' |
|
@martindurant You are right. Sorry! I was, indeed, referring to the @jcrist you are right, I was confused with |
| for catcol in pf.cats: | ||
| if catcol in meta.columns: | ||
| meta[catcol] = meta[catcol].cat.set_categories(pf.cats[catcol]) | ||
| if meta.index.name == catcol: |
There was a problem hiding this comment.
Can both branches here ever be taken (I don't think so?). If not, should use elif.
There was a problem hiding this comment.
Also, can you move this up to where meta is created? Otherwise there's a bit of space in between where meta exists but is incorrect for the given frame.
I also believe this currently fails if the output is a Series, as meta at this point would have no .columns attribute. Would be good to add a test for this as well.
| index_names, column_names, storage_name_mapping, column_index_names = ( | ||
| _parse_pandas_metadata(json.loads(pandas_md[0])) | ||
| ) | ||
| column_names.extend(list(pf.cats)) |
|
I think this should be good to go now? @martindurant, I'll leave the merge up to you. |
|
Yes, thanks @jcrist , forgot about this! |
Fix for the fastparquet side
flake8 daskdocs/source/changelog.rstfor all changesand one of the
docs/source/*-api.rstfiles for new API