Skip to content

BUG: to_csv with mode 'a' and zip compression if write by chunk creates multiple files insead of appending content #56778

Closed
@manicko

Description

@manicko

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

# enough data to cause chunking into multiple files
n_data = 1300
df = pd.DataFrame(
    {'name': ["Raphael"] * n_data,
     'mask': ["red"] * n_data,
     'weapon': ["sai"] * n_data,
     }
)
df.to_csv('in.csv', index=False)
compression_opts = dict(method='zip')
for chunk in pd.read_csv(filepath_or_buffer='in.csv', chunksize=1000):
    chunk.to_csv('out.csv.gz', mode= 'a', index=False,  compression=compression_opts)

Issue Description

If read data by chunk from any source (tested with csv, sql) and then export them with zip compression and mode 'a' (append data) the data is not appended, but new files added to the zip file with the same name.
Thus if you read 10 chunks and export them to zip, you'll have 10 files in a zip file instead of 1.
The problem relates to zip files only. The 'gz' and without compression modes are ok

Expected Behavior

same behaviour as without compression - 1 file in a zip archive

Installed Versions

INSTALLED VERSIONS

commit : a671b5a
python : 3.10.8.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.17763
machine : AMD64
processor : Intel64 Family 6 Model 140 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
pandas : 2.1.4
numpy : 1.26.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 69.0.3
pip : 23.3.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO DataIO issues that don't fit into a more specific labelWarningsWarnings that appear or should be added to pandas

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions