Skip to content

Commit

Permalink
ENH: add warning when export to tar, zip in append mode (#57875)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
quangngd and pre-commit-ci[bot] committed Apr 1, 2024
1 parent 5837042 commit 2a591f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/frame/methods/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 2a591f3

Please sign in to comment.