Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable writing column names with mixed dtype in parquet writer when mode.pandas_compatible=True #13505

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/cudf/source/user_guide/pandas-comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@ module, which allow you to compare values up to a desired precision.
Unlike Pandas, cuDF does not support duplicate column names.
It is best to use unique strings for column names.

## Writing dataframe to parquet with mixed column names
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved

When there is a dataframe with mixed column names, pandas type-casts each
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
column name to `string` before writing to parquet file. `cudf` raises an
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
error by default if this is attempted. However, to achieve similar behavior
as pandas you can enable pandas compatibility mode option, which will
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
enable `cudf` to type-cast the column names to `string` just like pandas.
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved

```python
>>> import cudf
>>> df = cudf.DataFrame({1: [1, 2, 3], "1": ["a", "b", "c"]})
>>> df.to_parquet("df.parquet")

Traceback (most recent call last):
ValueError: parquet must have string column names
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
>>> cudf.set_option("mode.pandas_compatible", True)
>>> df.to_parquet("df.parquet")

UserWarning: The DataFrame has column names of mixed type. They will be converted to strings and not roundtrip correctly.
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
```

## No true `"object"` data type

In Pandas and NumPy, the `"object"` data type is used for
Expand Down
9 changes: 6 additions & 3 deletions python/cudf/cudf/_lib/parquet.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,12 @@ def write_parquet(

for i, name in enumerate(table._column_names, num_index_cols_meta):
if not isinstance(name, str):
raise ValueError("parquet must have string column names")

tbl_meta.get().column_metadata[i].set_name(name.encode())
if cudf.get_option("mode.pandas_compatible"):
tbl_meta.get().column_metadata[i].set_name(str(name).encode())
else:
raise ValueError("parquet must have string column names")
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
else:
tbl_meta.get().column_metadata[i].set_name(name.encode())
_set_col_metadata(
table[name]._column,
tbl_meta.get().column_metadata[i],
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/_lib/utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ cpdef generate_pandas_metadata(table, index):
for col in table._columns
],
df=table,
column_names=col_names,
column_names=map(str, col_names),
index_levels=index_levels,
index_descriptors=index_descriptors,
preserve_index=index,
Expand Down
29 changes: 21 additions & 8 deletions python/cudf/cudf/tests/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from cudf.testing._utils import (
TIMEDELTA_TYPES,
assert_eq,
assert_exceptions_equal,
expect_warning_if,
set_random_null_mask_inplace,
)
Expand Down Expand Up @@ -2528,15 +2527,29 @@ def test_parquet_writer_decimal(decimal_type, data):


def test_parquet_writer_column_validation():
df = cudf.DataFrame({1: [1, 2, 3], "1": ["a", "b", "c"]})
df = cudf.DataFrame({1: [1, 2, 3], "a": ["a", "b", "c"]})
pdf = df.to_pandas()

assert_exceptions_equal(
lfunc=df.to_parquet,
rfunc=pdf.to_parquet,
lfunc_args_and_kwargs=(["cudf.parquet"],),
rfunc_args_and_kwargs=(["pandas.parquet"],),
)
with cudf.option_context("mode.pandas_compatible", True):
with pytest.warns(UserWarning):
df.to_parquet("cudf.parquet")

if PANDAS_GE_200:
with pytest.warns(UserWarning):
pdf.to_parquet("pandas.parquet")

assert_eq(
pd.read_parquet("cudf.parquet"),
cudf.read_parquet("pandas.parquet"),
)
assert_eq(
cudf.read_parquet("cudf.parquet"),
pd.read_parquet("pandas.parquet"),
)

with cudf.option_context("mode.pandas_compatible", False):
with pytest.raises(ValueError):
df.to_parquet("cudf.parquet")


def test_parquet_writer_nulls_pandas_read(tmpdir, pdf):
Expand Down