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

Adding support for writing empty dataframe #8490

Merged
merged 3 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 6 additions & 4 deletions cpp/src/io/csv/writer_impl.cu
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ void writer::impl::write_chunked_begin(table_view const& table,
if ((metadata != nullptr) && (options_.is_enabled_include_header())) {
CUDF_EXPECTS(metadata->column_names.size() == static_cast<size_t>(table.num_columns()),
"Mismatch between number of column headers and table columns.");

std::string delimiter_str{options_.get_inter_column_delimiter()};

// avoid delimiter after last element:
Expand All @@ -295,7 +294,12 @@ void writer::impl::write_chunked_begin(table_view const& table,
std::copy(metadata->column_names.begin(),
metadata->column_names.end() - 1,
std::ostream_iterator<std::string>(ss, delimiter_str.c_str()));
ss << metadata->column_names.back() << options_.get_line_terminator();

if (metadata->column_names.size() > 0) {
ss << metadata->column_names.back() << options_.get_line_terminator();
} else {
ss << options_.get_line_terminator();
}

out_sink_->host_write(ss.str().data(), ss.str().size());
}
Expand Down Expand Up @@ -355,8 +359,6 @@ void writer::impl::write(table_view const& table,
const table_metadata* metadata,
rmm::cuda_stream_view stream)
{
CUDF_EXPECTS(table.num_columns() > 0, "Empty table.");

// write header: column names separated by delimiter:
// (even for tables with no rows)
//
Expand Down
1 change: 0 additions & 1 deletion python/cudf/cudf/_lib/csv.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ cpdef write_csv(
--------
cudf.io.csv.to_csv
"""

cdef table_view input_table_view = \
table.view() if index is True else table.data_view()
cdef bool include_header_c = header
Expand Down
16 changes: 12 additions & 4 deletions python/cudf/cudf/tests/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,10 +1583,8 @@ def test_csv_writer_column_and_header_options(

def test_csv_writer_empty_columns_parameter(cudf_mixed_dataframe):
df = cudf_mixed_dataframe

buffer = BytesIO()
with pytest.raises(RuntimeError):
df.to_csv(buffer, columns=[], index=False)
write_str = df.to_csv(columns=[], index=False)
assert_eq(write_str, "\n")


def test_csv_writer_multiindex(tmpdir):
Expand Down Expand Up @@ -1979,3 +1977,13 @@ def test_to_csv_compression_error():
error_message = "Writing compressed csv is not currently supported in cudf"
with pytest.raises(NotImplementedError, match=re.escape(error_message)):
df.to_csv("test.csv", compression=compression)


def test_empty_df_no_index():
actual = cudf.DataFrame({})
buffer = BytesIO()
actual.to_csv(buffer, index=False)

result = cudf.read_csv(buffer)

assert_eq(actual, result)