Repository navigation
Infer arrow schema in to_parquet (for ArrowEngine) - #6490
Conversation
|
It's rather similar to the "object_encoding=" argument from fastparquet, i.e., specify the parquet encoding type only for specific object-type columns, or for all object columns in general. This still works today, I think I also think that there is an opportunity for the dataframe meta to carry more information than the basic pandas dtypes. That would be a deeper change, but worth thinking about. On the other hand, the object type may be increasingly superseded by specific extension types that would provide more obvious behaviour. |
This comparison makes sense. I just updated the PR to do things a bit differently:
|
|
cc @jorisvandenbossche and @TomAugspurger (Since you may have thoughts here) |
TomAugspurger
left a comment
There was a problem hiding this comment.
Makes sense to me, thanks.
|
And because I sort of left it out, here's my modified workaround for this problem: import dask.dataframe as dd
import pyarrow as pa
NULL_TYPE = pa.null()
def get_nonnull_schema_for_dataframe(df: dd.DataFrame) -> pa.Schema:
schema = pa.schema([pa.field(c, NULL_TYPE) for c in df.columns])
for i in range(df.npartitions):
part_df = df.get_partition(i)
if len(part_df):
_schema = pa.Schema.from_pandas(part_df.compute())
for name, typ in zip(_schema.names, _schema.types):
if typ != "null":
i = schema.get_field_index(name)
j = _schema.get_field_index(name)
schema = schema.set(i, _schema.field(j))
has_null_col = NULL_TYPE in [f.type for f in schema]
if not has_null_col:
break
if has_null_col:
raise ValueError("cannot infer schema with no null-type columns")
return schema |
|
Having to "get" each partition of a dataframe into the client is likely prohibitive for most large datasets. You could do the same work in the workers, but could still means two passes of the data for a write. |
My assumption is that the reason @rjzamora is looking at doing it this way is because the necessary information to determine the correct arrow type is simply not statically available in the dask dataframe's metadata. Is that correct? If so, it seems like there's no avoiding inspecting the data or at least enough data to determine a concrete type for each column. For most datasets, I would hope that this is possible by only computing a small fraction of the partitions. |
|
@davesque , yes of course you are right. I wanted to get the caveat in ahead. This kind of thing would need to be user specified, I think. |
|
@davesque - Are you saying the solution in this PR fails for your example? (sorry - I'm not really at my computer long enough to check) @martindurant - With this PR, we will only sample partitions for "object" columns if the user specifies |
Ah - Thats a good point. |
Yes, and sorry I didn't include a demonstration of exactly how this was failing for me. This notebook gist contains an error that's being thrown in |
Hmm - In this PR, we will start with the |
|
Right but as I pointed out, the dtype in Update: Actually, I guess I didn't point out the issue with |
Oops - This shouldn't be the case - there was a bug in my code (the |
Ahh, funny. Yeah I remember running into that issue and correcting it in my version. But maybe I didn't realize it was the real root problem. |
|
@rjzamora Although won't it also cause issues that you're checking specifically for "object" type columns? So anything that is marked as "string" in dask/dask/dataframe/io/parquet/arrow.py Line 864 in 65c7c56 |
|
Actually, I guess it won't technically be ignored since it may appear in a partition that you compute. However, if it does not appear in one of those partitions, it won't get the right data type. This is because the So I think you still need to resolve those columns somehow so the issue with the wrong data type in |
jorisvandenbossche
left a comment
There was a problem hiding this comment.
The general approach looks good to me!
I may be confused here. For me, the arrow schema starts off with a "string" field for the datetime example,because the |
|
@rjzamora Actually, you're totally right! Sorry! I forgot that the |
davesque
left a comment
There was a problem hiding this comment.
Sorry to inject myself again into this discussion :). I just had a thought about the last update I saw so I thought I'd share. Feel free to ignore.
|
@martindurant - Thanks for your help with this. Let me know if you have any other thoughts/concerns |
|
It looks like there are failures on master after this merge: ==================================== ERRORS ====================================
___________ ERROR collecting dask/dataframe/io/tests/test_parquet.py ___________
dask/dataframe/io/tests/test_parquet.py:1077: in <module>
"schema", ["infer", {"index": pa.string(), "amount": pa.int64()}]
E AttributeError: 'bool' object has no attribute 'string'
___________ ERROR collecting dask/dataframe/io/tests/test_parquet.py ___________
dask/dataframe/io/tests/test_parquet.py:1077: in <module>
"schema", ["infer", {"index": pa.string(), "amount": pa.int64()}]
E AttributeError: 'bool' object has no attribute 'string'
___________ ERROR collecting dask/dataframe/io/tests/test_parquet.py ___________
dask/dataframe/io/tests/test_parquet.py:1077: in <module>
"schema", ["infer", {"index": pa.string(), "amount": pa.int64()}]
E AttributeError: 'bool' object has no attribute 'string' |
|
It looks like this is the arrow-not-installed case. The test ought to have been skipped. |
* infer schema for to_parquet in ArrowEngine * make sampling more thorough (when necessary) * handle dict input for schema * add test coverage for dict input and update docstring * handle pre-0.15 pyarrow * missing assignment * missing assignment * fix test * improve docstring * get schema on worker * avoid lambda

Closes #6243
As of the 1.0 release, pyarrow now (understandably) compains when you try to compose a shared
_metadatafile from partitions with inconsistent schemas. This PR adds a publicschemaargument toto_parquet(although it is only used by the ArrowEngine).By default (if the user does not specify an actual arrow schema), this new argument is set to"sample", which means the client will find the first partition with data, and use theheadof this partition to infer the global arrow schema. The user can also passschema="meta"to use the_meta_nonemptyfor inference, orschema=Noneto allow the backend (pyarrow) to infer the schema for each output partition separately.The advantage of using"sample"over"meta"as the default is that we are much less likely to set an incorrect global schema. For example, using"meta"will not correcly detect the difference between string and binary (they are both "object" types in_meta_nonempty). The obvious disadvantage is the need to sample "real" data (requiring worker-client data transfer in a distributed environment).Given the competing factors described above: Do we want to infer the schema by default? An alternative to the current solution is to set the default toNone, and only perform inference (via sampling) if the user passesschema="infer"As stated in a comment below, the following approach is now used:
black dask/flake8 dask