-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
df.to_csv ignores compression when provided with a file handle #21227
Comments
Maybe related to #21144 |
looks like an existing behaviour dating back to version 0.20 or earlier. Actually, the documentation of
so it is not supported but may be a new use case perhaps? |
Hi, Should the following code works with this merge or is it related to #22555?
3.6.7 (default, Dec 6 2019, 07:03:06) [MSC v.1900 64 bit (AMD64)] 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] Thank you |
@toninlg this worked for me import gzip
with io.StringIO() as buf:
df.to_csv(buf)
with open('test_compressed.gz', 'wb') as remote_file:
remote_file.write(gzip.compress(bytes(buf.getvalue(), 'utf-8'))) |
You can also try this: import csv
import gzip
from io import BytesIO, TextIOWrapper
gz_buffer = BytesIO()
with gzip.GzipFile(fileobj=gz_buffer, mode="w") as gz_file:
df.to_csv(
path_or_buf=TextIOWrapper(gz_file, "utf8"),
index=False,
sep=",",
quoting=csv.QUOTE_NONE,
compression="gzip",
) |
Code at issue
Problem description
The written file is not gzip compressed.
Expected Output
The output should be a gzip compressed csv file. Similar to what is obtained when using:
The text was updated successfully, but these errors were encountered: