You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The to_parquet function fails using the pyarrow engine if you have a dask dataframe with an empty partition. It may require a certain mix of data types to trigger the defect.
Support you have a csv file:
field1, field2
a,5
you then read it in, repartition to 2 partitions and then try to write as parquet.
import dask.dataframe as dd
ddf = dd.read_parquet(<path to csv>)
ddf2 = ddf.repartition(npartitions=ddf.napartitions*2)
ddf2.to_parquet(<path>, engine='pyarrow')
This will fail with a "inconsistent schema" error. I can resolve this error by using the following function to first remove the empty dataframes; the function comes from MRocklin in 2017...
def cull_empty_partitions(ddf):
ll = list(ddf.map_partitions(len).compute())
df_delayed = ddf.to_delayed()
df_delayed_new = list()
pempty = None
for ix, n in enumerate(ll):
if 0 == n:
pempty = ddf.get_partition(ix)
else:
df_delayed_new.append(df_delayed[ix])
if pempty is not None:
ddf = dd.from_delayed(df_delayed_new, meta=pempty)
return ddf
dask version: 2021.12.0
python version: 3.9
OS version: MacOS and Windows tested
install method: conda
When I run the reproducer (with typos fixed the first read_parquet changed to read_csv), I get:
`RuntimeError: Schemas are inconsistent, try using `to_parquet(..., schema="infer")`, or pass an explicit pyarrow schema. Such as `to_parquet(..., schema={"column1": pa.string()})`
If I follow the advice in the warning, and use ddf2.to_parquet(<path>, engine='pyarrow')ddf2.to_parquet(<path>, engine='pyarrow', schema='infer'), everything runs fine for me. Are you not getting this warning, or did it not resolve your issue?
Apologies for whatever the typos were!
Yes, you can follow the suggestion; I assume you used ddf2.to_parquet(, engine='pyarrow', schema='infer')?
This might work, but should not be necessary; it is not necessary if you do not have an empty partition. I believe it is only occurring because of the empty partition, and this would therefore be unintended behaviour.
MRocklin asked for reports of functions that do not work on data dataframes that contain empty partitions.
I assume you used ddf2.to_parquet(, engine='pyarrow', schema='infer')?
Yes. My mistake!
This might work, but should not be necessary; it is not necessary if you do not have an empty partition. I believe it is only occurring because of the empty partition, and this would therefore be unintended behaviour.
I agree that it would be ideal for this to work with an empty partition, but the problem has come up before and the only way to "fix" it is to change the default behavior to schema="infer". Since this blind schema inference can be expensive, the compromise was a clear error message (see 4194, #6490, and #6657).
Note that there is also another open issue asking to further improve this error message (#8619), but I don't think there is any way to avoid making the user pass a schema argument in Dask. (but we are certainly open to suggestions).
@rjzamora, perhaps a naive question, but could we just not write any file for an empty partition? This would mean that a part.#.parquet file wouldn't exist for that partition, but would avoid doing any schema merging. I suppose this would look confusing to downstream consumers (like, perhaps a file is missing), but it would avoid the issue I think.
@rjzamora, perhaps a naive question, but could we just not write any file for an empty partition? This would mean that a part.#.parquet file wouldn't exist for that partition, but would avoid doing any schema merging. I suppose this would look confusing to downstream consumers (like, perhaps a file is missing), but it would avoid the issue I think.
This is probably fine. I was actually overlooking this possibility, because I was thinking about the case of "null" partitions in addition to "empty" partitions. However, if the partition is completely empty, then I don't see any obvious problem with your suggestion (although, we may want it to be optional?).
Alternatively, is this mainly/only an issue when writing the _metadata file? If we switched to defaulting to write_metadata_file=False would that resolve this issue for most users? I know that _meta doesn't map 1:1 to a pyarrow schema, but if empty files were close-enough in schema (e.g. all columns present, proper int/float/str/... dtypes) would that be enough that most downstream readers (dask & others) be fine?
Alternatively, is this mainly/only an issue when writing the _metadata file? If we switched to defaulting to write_metadata_file=False would that resolve this issue for most users? I know that _meta doesn't map 1:1 to a pyarrow schema, but if empty files were close-enough in schema (e.g. all columns present, proper int/float/str/... dtypes) would that be enough that most downstream readers (dask & others) be fine?
I've been thinking about this a bit, and will probably need to summarize my thoughts in #8901 - However, the short version is that I would love to rip out _metadata usage from both a maintenance and stability perspective, but the clear performance benefit of using _metadata still makes me a bit hesitant to suggest this (though I am leaning toward it).
Activity
rjzamora commentedon Mar 23, 2022
When I run the reproducer (with
typos fixedthe firstread_parquetchanged toread_csv), I get:If I follow the advice in the warning, and use
ddf2.to_parquet(<path>, engine='pyarrow')ddf2.to_parquet(<path>, engine='pyarrow', schema='infer'), everything runs fine for me. Are you not getting this warning, or did it not resolve your issue?rhjmoore commentedon Mar 23, 2022
Apologies for whatever the typos were!
Yes, you can follow the suggestion; I assume you used ddf2.to_parquet(, engine='pyarrow', schema='infer')?
This might work, but should not be necessary; it is not necessary if you do not have an empty partition. I believe it is only occurring because of the empty partition, and this would therefore be unintended behaviour.
MRocklin asked for reports of functions that do not work on data dataframes that contain empty partitions.
rjzamora commentedon Mar 23, 2022
Yes. My mistake!
I agree that it would be ideal for this to work with an empty partition, but the problem has come up before and the only way to "fix" it is to change the default behavior to
schema="infer". Since this blind schema inference can be expensive, the compromise was a clear error message (see 4194, #6490, and #6657).Note that there is also another open issue asking to further improve this error message (#8619), but I don't think there is any way to avoid making the user pass a
schemaargument in Dask. (but we are certainly open to suggestions).jcrist commentedon Apr 7, 2022
@rjzamora, perhaps a naive question, but could we just not write any file for an empty partition? This would mean that a
part.#.parquetfile wouldn't exist for that partition, but would avoid doing any schema merging. I suppose this would look confusing to downstream consumers (like, perhaps a file is missing), but it would avoid the issue I think.rjzamora commentedon Apr 7, 2022
This is probably fine. I was actually overlooking this possibility, because I was thinking about the case of "null" partitions in addition to "empty" partitions. However, if the partition is completely empty, then I don't see any obvious problem with your suggestion (although, we may want it to be optional?).
jcrist commentedon Apr 7, 2022
Alternatively, is this mainly/only an issue when writing the
_metadatafile? If we switched to defaulting towrite_metadata_file=Falsewould that resolve this issue for most users? I know that_metadoesn't map 1:1 to a pyarrow schema, but if empty files were close-enough in schema (e.g. all columns present, proper int/float/str/... dtypes) would that be enough that most downstream readers (dask & others) be fine?rjzamora commentedon Apr 8, 2022
I've been thinking about this a bit, and will probably need to summarize my thoughts in #8901 - However, the short version is that I would love to rip out _metadata usage from both a maintenance and stability perspective, but the clear performance benefit of using _metadata still makes me a bit hesitant to suggest this (though I am leaning toward it).