Skip to content

Accept empty dataframes in DataFrame.to_parquet #27341

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

Merged
merged 2 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,7 @@ I/O
- Fixed bug in :func:`DataFrame.to_excel()` where custom objects (i.e. `PeriodIndex`) inside merged cells were not being converted into types safe for the Excel writer (:issue:`27006`)
- Bug in :meth:`read_hdf` where reading a timezone aware :class:`DatetimeIndex` would raise a ``TypeError`` (:issue:`11926`)
- Bug in :meth:`to_msgpack` and :meth:`read_msgpack` which would raise a ``ValueError`` rather than a ``FileNotFoundError`` for an invalid path (:issue:`27160`)
- Fixed bug in :meth:`DataFrame.to_parquet` which would raise a ``ValueError`` when the dataframe had no columns (:issue:`27339`)

Plotting
^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def validate_dataframe(df):
raise ValueError("to_parquet only supports IO with DataFrames")

# must have value column names (strings only)
if df.columns.inferred_type not in {"string", "unicode"}:
if df.columns.inferred_type not in {"string", "unicode", "empty"}:
raise ValueError("parquet must have string column names")

# index level names must be strings
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ def test_partition_cols_supported(self, pa, df_full):
assert len(dataset.partitions.partition_names) == 2
assert dataset.partitions.partition_names == set(partition_cols)

def test_empty_dataframe(self, pa):
# GH #27339
df = pd.DataFrame()
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add the github issue number as a comment

check_round_trip(df, pa)


class TestParquetFastParquet(Base):
@td.skip_if_no("fastparquet", min_version="0.2.1")
Expand Down Expand Up @@ -566,3 +571,10 @@ def test_error_on_using_partition_cols_and_partition_on(self, fp, df_full):
partition_on=partition_cols,
partition_cols=partition_cols,
)

def test_empty_dataframe(self, fp):
# GH #27339
df = pd.DataFrame()
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

expected = df.copy()
expected.index.name = "index"
check_round_trip(df, fp, expected=expected)