Skip to content

Loading of partitioned parquet should result in known categories - #3216

Merged
martindurant merged 4 commits into
dask:masterfrom
martindurant:fastparquet_known_partitioning
Mar 14, 2018
Merged

martindurant merged 4 commits into
dask:masterfrom
martindurant:fastparquet_known_partitioning

Conversation

@martindurant

@martindurant martindurant commented Feb 26, 2018 •

Copy link
Copy Markdown
Member

Fix for the fastparquet side

  • Tests added / passed
  • Passes flake8 dask
  • Fully documented, including docs/source/changelog.rst for all changes
    and one of the docs/source/*-api.rst files for new API

@martindurant

Copy link
Copy Markdown
Member Author

Fixes #3186

@andrethrill , would you mind checking if this works for you? If yes, I'll add tests here.

Comment thread dask/dataframe/io/parquet.py Outdated
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])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may be right, I'll check. I guess the partitioned column could also end up as the index?

@martindurant

Copy link
Copy Markdown
Member Author

@jcrist back to you - I think this is good now.

@erdnaavlis

Copy link
Copy Markdown
Contributor

@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.

@martindurant

Copy link
Copy Markdown
Member Author

I would merge this if there are no further comments. Leaving it live until ~Monday evening.

@erdnaavlis

Copy link
Copy Markdown
Contributor

@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 test_partition_on_cats(tmpdir):

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.

@martindurant

martindurant commented Mar 5, 2018 •

Copy link
Copy Markdown
Member Author

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:

{col: df[col].cat.categories for col in df.columns 
    if df[col].dtype.name == 'category' and df[col].cat.known}

(I think you were referring to the cats attribute of a ParquetFile instance, not a pandas dataframe - where we have more of the luxury to provide whatever information we find useful)

@jcrist

jcrist commented Mar 5, 2018

Copy link
Copy Markdown
Member

In pandas, one can do something like:

assert set(df.cats) == [('b', {'x', 'y', 'z'}), ('c', {'x', 'y', 'z'}) ]

Are you sure? AFAIK pandas only has a .cat attribute on series, I don't think there's a .cats attribute on dataframes. Since we're trying to mimic the pandas API I'd be reluctant to add this unless they already have it.

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'

@erdnaavlis

Copy link
Copy Markdown
Contributor

@martindurant You are right. Sorry! I was, indeed, referring to the cats attribute of a ParquetFile. Your suggestion works fine for finding out which partitions are available.

@jcrist you are right, I was confused with DataFrame and ParquetFile

Comment thread dask/dataframe/io/parquet.py Outdated
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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can both branches here ever be taken (I don't think so?). If not, should use elif.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread dask/dataframe/io/parquet.py Outdated
index_names, column_names, storage_name_mapping, column_index_names = (
_parse_pandas_metadata(json.loads(pandas_md[0]))
)
column_names.extend(list(pf.cats))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary list call.

@jcrist

jcrist commented Mar 14, 2018

Copy link
Copy Markdown
Member

I think this should be good to go now? @martindurant, I'll leave the merge up to you.

@martindurant

Copy link
Copy Markdown
Member Author

Yes, thanks @jcrist , forgot about this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants