Skip to content

Support categories= argument for pyarrow - #3177

Merged
jcrist merged 4 commits into
dask:masterfrom
xhochy:pyarrow-0.9.0-categories
Feb 26, 2018
Merged

jcrist merged 4 commits into
dask:masterfrom
xhochy:pyarrow-0.9.0-categories

Conversation

@xhochy

@xhochy xhochy commented Feb 18, 2018 •

Copy link
Copy Markdown
Contributor
  • 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

Depends on apache/arrow#1620 (should be part of the upcoming 0.9 release)

fastparquet -> pyarrow roundtrip fails due to dask/fastparquet#305

@xhochy
xhochy force-pushed the pyarrow-0.9.0-categories branch from 30242cb to 7bdb3d3 Compare February 18, 2018 19:57
@jcrist

jcrist commented Feb 20, 2018

Copy link
Copy Markdown
Member

Any way this can be made to be backwards compatible with older versions of pyarrow? I'm fine with either:

  • Erroring nicely if categories are provided (preferably at graph build time) if an older version of pyarrow is used.
  • Backporting this functionality to work even with pyarrow 0.8.0.

The first one is probably easier, not sure. Up to you.

@xhochy

xhochy commented Feb 24, 2018

Copy link
Copy Markdown
Contributor Author

@jcrist I went the long way and implemented three paths. Each one being the fastest for the given PyArrow version. While the one for 0.8 is quite lengthy, it should be much faster (mostly due to less memory copies) than the one that falls back to Pandas.

Comment thread dask/dataframe/io/parquet.py Outdated
table = table.add_column(i, new_col)
df = table.to_pandas()
for cat in remaining_cats:
df[cat] = df[cat].astype('category')

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.

Is there a strong benefit to doing it this way? I'm half tempted to just fall back to the simple pandas method due to maintenance costs. I suspect people using pyarrow are fairly quick to upgrade to new releases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Given that we will hopefully release pyarrow in the next two weeks, I'm going to remove this again

dtypes = _get_pyarrow_dtypes(schema, categories)
dtypes = {storage_name_mapping.get(k, k): v for k, v in dtypes.items()}

meta = _meta_from_dtypes(all_columns, dtypes, index_names, column_index_names)

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.

Since we don't know the actual categories at graph build time, we need to make sure any categorical columns/indices are marked as unknown categories (see http://dask.pydata.org/en/latest/dataframe-design.html#categoricals). The fastparquet reader handles this here: https://github.com/dask/dask/pull/3177/files#diff-410a8a0995091d42fbbbc5a50efda3aeL295, but this looks incorrect for the no-data case, as meta is returned directly on compute and would then have the string '__UNKNOWN_CATEGORIES__' in its categories.

Ideally we'd have two objects:

  • An empty frame/series that can be returned directly to the user on compute in the case of no data
  • An empty frame/series to be used as metadata that properly sets unknown categories

These should ideally be handled using the same code path (perhaps move logic to _meta_from_dtypes). You might also find dask.dataframe.utils.clear_known_categories (sets all categories to be unknown) and dask.dataframe.utils.strip_unknown_categories (removes unknown categories flags) to be useful.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Adjusted it hopefully in the correct way. Moving the code to _meta_from_dtype depends on the pyarrow path also supporting known categoricals. I'm not sure how soon I'll get to that.

@xhochy
xhochy force-pushed the pyarrow-0.9.0-categories branch from d3fd17d to b56a021 Compare February 25, 2018 19:13
@xhochy

xhochy commented Feb 26, 2018

Copy link
Copy Markdown
Contributor Author

Travis failure is due to a job exceeding the maximal execution time.

@mrocklin

Copy link
Copy Markdown
Member

Restarted. Tests pass

@jcrist
jcrist merged commit 9d8ee97 into dask:master Feb 26, 2018
@jcrist

jcrist commented Feb 26, 2018

Copy link
Copy Markdown
Member

Thanks @xhochy!

@xhochy
xhochy deleted the pyarrow-0.9.0-categories branch February 27, 2018 09:28
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