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

ENH: add warning when export to tar, zip in append mode #57875

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
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
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")