Skip to content

Spark compatibility with pandas extension dtypes - #9590

Open
jrbourbeau wants to merge 9 commits into
dask:mainfrom
jrbourbeau:spark-extension-dtypes
Open

jrbourbeau wants to merge 9 commits into
dask:mainfrom
jrbourbeau:spark-extension-dtypes

Conversation

@jrbourbeau

Copy link
Copy Markdown
Member

This test demonstrates that, when Parquet datasets with extension dtypes are written with Spark, Dask isn't currently able to read in extension dtypes as expected. Instead, things get dtypes get downcasted (e.g. a column that starts out as Float64 is read in by Dask as float)


echo "py.test dask --runslow $XTRATESTARGS"
py.test dask --runslow $XTRATESTARGS
py.test dask/tests/test_spark_compat.py --runslow $XTRATESTARGS

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.

This is just a temporary change for convenience. This should be reverted before merging this PR

@th3ed

th3ed commented Oct 27, 2022 •

Copy link
Copy Markdown

here's a reproducible example of the issue using pyarrow

import pyarrow as pa
import dask.dataframe as dd
import pandas as pd

# Generate a parquet with a nullable int dtype
pq.write_to_dataset(
  pa.Table.from_arrays([
    [0, 1, 2, None, 4, 5, 6, 7, 8, 9],
    ['a'] * 5 + ['b'] * 5
    ], names=['x1', 'part']),
  "test.parquet",
  partition_cols = ['part']
)

# Read in parquet and call a function that requires an int col
df = dd.read_parquet("test.parquet") \
  .assign(
    some_date = pd.to_datetime('2022-01-01').to_period('M'),
    inc_date = lambda d: d['some_date'] + d['x1']
  )
df.head() # fails, partition 1 casts x1 as a float
df.tail() # works, partition 2 casts x2 as an int

TypeError: unsupported operand type(s) for +: 'Period' and 'float'

When converting pyarrow to pandas via to_pandas() pyarrow will convert a series of pyarrow ints to a pandas int if there are no missing values in the array or a series of floats otherwise. Pandas read_parquet has a use_nullable_dtypes arg that may make sense here.

@mrocklin

Copy link
Copy Markdown
Member

@jrbourbeau I'm curious, what is the status here? Do you think it would be possible to hand this off to someone else? If so, what would you suggest as a course of action?

@github-actions github-actions Bot added the needs attention It's been a while since this was pushed on. Needs attention from the owner or a maintainer. label May 6, 2024

This branch has not been deployed

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

Labels

needs attention It's been a while since this was pushed on. Needs attention from the owner or a maintainer.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants