Skip to content

Commit 2a591f3

Browse files
ENH: add warning when export to tar, zip in append mode (#57875)
* add warning * add test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5837042 commit 2a591f3

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pandas/io/common.py

+10
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,16 @@ def _get_filepath_or_buffer(
361361
stacklevel=find_stack_level(),
362362
)
363363

364+
if "a" in mode and compression_method in ["zip", "tar"]:
365+
# GH56778
366+
warnings.warn(
367+
"zip and tar do not support mode 'a' properly. "
368+
"This combination will result in multiple files with same name "
369+
"being added to the archive.",
370+
RuntimeWarning,
371+
stacklevel=find_stack_level(),
372+
)
373+
364374
# Use binary mode when converting path-like objects to file-like objects (fsspec)
365375
# except when text mode is explicitly requested. The original mode is returned if
366376
# fsspec is not used.

pandas/tests/frame/methods/test_to_csv.py

+17
Original file line numberDiff line numberDiff line change
@@ -1405,3 +1405,20 @@ def test_to_csv_categorical_and_interval(self):
14051405
expected_rows = [",a", '0,"[2020-01-01 00:00:00, 2020-01-02 00:00:00]"']
14061406
expected = tm.convert_rows_list_to_csv_str(expected_rows)
14071407
assert result == expected
1408+
1409+
def test_to_csv_warn_when_zip_tar_and_append_mode(self):
1410+
# GH57875
1411+
df = DataFrame({"a": [1, 2, 3]})
1412+
msg = (
1413+
"zip and tar do not support mode 'a' properly. This combination will "
1414+
"result in multiple files with same name being added to the archive"
1415+
)
1416+
with tm.assert_produces_warning(
1417+
RuntimeWarning, match=msg, raise_on_extra_warnings=False
1418+
):
1419+
df.to_csv("test.zip", mode="a")
1420+
1421+
with tm.assert_produces_warning(
1422+
RuntimeWarning, match=msg, raise_on_extra_warnings=False
1423+
):
1424+
df.to_csv("test.tar", mode="a")

0 commit comments

Comments
 (0)