From 5a4b50b3772cb3db48a0c603cef5a39ff96f442c Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Sun, 17 Mar 2024 20:52:49 +0700 Subject: [PATCH 1/3] add warning --- pandas/io/common.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/io/common.py b/pandas/io/common.py index 35c3a24d8e8f6..abb26b0ffefaa 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -361,6 +361,19 @@ 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. From 091a513bf7d9e3dd57e42815e327d7e131adf7e3 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Wed, 20 Mar 2024 10:14:03 +0700 Subject: [PATCH 2/3] add test --- pandas/tests/frame/methods/test_to_csv.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index 5b9ced8d47ed7..f87fa4137d62d 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -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") From b6920aabee7dc3ace30eb599eed40f3722318df6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 20 Mar 2024 03:35:59 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pandas/io/common.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index abb26b0ffefaa..4507a7d08c8ba 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -361,10 +361,7 @@ def _get_filepath_or_buffer( stacklevel=find_stack_level(), ) - if ( - "a" in mode - and compression_method in ["zip", "tar"] - ): + if "a" in mode and compression_method in ["zip", "tar"]: # GH56778 warnings.warn( "zip and tar do not support mode 'a' properly. "