Skip to content
Merged
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
10 changes: 10 additions & 0 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
@@ -361,6 +361,16 @@ def _get_filepath_or_buffer(
stacklevel=find_stack_level(),
)

if "a" in mode and compression_method in ["zip", "tar"]:
# GH56778
warnings.warn(
"zip and tar do not support mode 'a' properly. "
"This combination will result in multiple files with same name "
"being added to the archive.",
RuntimeWarning,
stacklevel=find_stack_level(),
)

# Use binary mode when converting path-like objects to file-like objects (fsspec)
# except when text mode is explicitly requested. The original mode is returned if
# fsspec is not used.
17 changes: 17 additions & 0 deletions pandas/tests/frame/methods/test_to_csv.py
Original file line number Diff line number Diff line change
@@ -1405,3 +1405,20 @@ def test_to_csv_categorical_and_interval(self):
expected_rows = [",a", '0,"[2020-01-01 00:00:00, 2020-01-02 00:00:00]"']
expected = tm.convert_rows_list_to_csv_str(expected_rows)
assert result == expected

def test_to_csv_warn_when_zip_tar_and_append_mode(self):
# GH57875
df = DataFrame({"a": [1, 2, 3]})
msg = (
"zip and tar do not support mode 'a' properly. This combination will "
"result in multiple files with same name being added to the archive"
)
with tm.assert_produces_warning(
RuntimeWarning, match=msg, raise_on_extra_warnings=False
):
df.to_csv("test.zip", mode="a")

with tm.assert_produces_warning(
RuntimeWarning, match=msg, raise_on_extra_warnings=False
):
df.to_csv("test.tar", mode="a")