Skip to content

Commit eeff07f

Browse files
CJStadlerjreback
authored andcommitted
Accept empty dataframes in DataFrame.to_parquet (#27341)
1 parent d3e84b7 commit eeff07f

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,7 @@ I/O
11021102
- 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`)
11031103
- Bug in :meth:`read_hdf` where reading a timezone aware :class:`DatetimeIndex` would raise a ``TypeError`` (:issue:`11926`)
11041104
- Bug in :meth:`to_msgpack` and :meth:`read_msgpack` which would raise a ``ValueError`` rather than a ``FileNotFoundError`` for an invalid path (:issue:`27160`)
1105+
- Fixed bug in :meth:`DataFrame.to_parquet` which would raise a ``ValueError`` when the dataframe had no columns (:issue:`27339`)
11051106
11061107
Plotting
11071108
^^^^^^^^

pandas/io/parquet.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def validate_dataframe(df):
5555
raise ValueError("to_parquet only supports IO with DataFrames")
5656

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

6161
# index level names must be strings

pandas/tests/io/test_parquet.py

+12
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,11 @@ def test_partition_cols_supported(self, pa, df_full):
471471
assert len(dataset.partitions.partition_names) == 2
472472
assert dataset.partitions.partition_names == set(partition_cols)
473473

474+
def test_empty_dataframe(self, pa):
475+
# GH #27339
476+
df = pd.DataFrame()
477+
check_round_trip(df, pa)
478+
474479

475480
class TestParquetFastParquet(Base):
476481
@td.skip_if_no("fastparquet", min_version="0.2.1")
@@ -566,3 +571,10 @@ def test_error_on_using_partition_cols_and_partition_on(self, fp, df_full):
566571
partition_on=partition_cols,
567572
partition_cols=partition_cols,
568573
)
574+
575+
def test_empty_dataframe(self, fp):
576+
# GH #27339
577+
df = pd.DataFrame()
578+
expected = df.copy()
579+
expected.index.name = "index"
580+
check_round_trip(df, fp, expected=expected)

0 commit comments

Comments
 (0)