Skip to content

Enhancement dataframe to csv use zip method #39662

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

Conversation

CyberQin
Copy link

@CyberQin CyberQin commented Feb 8, 2021

Enhancement dataframe to csv use zip method,now df.to_csv('myfile.csv.zip') works like df.to_csv('myfile.csv.gz'), generate a zip file with a myfile.csv as content

test code

import pandas as pd
a=pd.DataFrame({'a':[1,2,3,4,5],'b':[4,5,6,7,8]})
a.to_csv(r'z:/myfile.csv.zip')

Copy link
Member

@arw2019 arw2019 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this breaks some tests

=========================== short test summary info ============================
FAILED pandas/tests/io/formats/test_to_csv.py::TestToCSV::test_to_csv_zip_arguments[None-zip]
FAILED pandas/tests/io/formats/test_to_csv.py::TestToCSV::test_to_csv_zip_arguments[None-infer]

@@ -726,6 +726,12 @@ def __init__(
self.archive_name = archive_name
self.multiple_write_buffer: Optional[Union[StringIO, BytesIO]] = None

# add csv file name like gz or bz2
if archive_name is None and isinstance(file, (os.PathLike, str)):
archive_name = os.path.basename(file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably better to replace os.path.basename with stringify_path (to handle PathLike objects). That might also fix the failing test_to_csv_compression_encoding_gcs test.

Copy link
Author

@CyberQin CyberQin Feb 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its strange that this test passed on my win10 laptop.
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, you probably still need basename to avoid having the entire path within the archive

Copy link
Author

@CyberQin CyberQin Feb 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert to os.path.basename , but start get different ci errors, does my commit affect it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a small test case. For example, write a compressed CSV file and then open it with zipfile.ZipFile to check the name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test case and finish pre commit, a new PR is opened #40387

@pep8speaks
Copy link

pep8speaks commented Mar 10, 2021

Hello @HQDragon! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 1336:1: W391 blank line at end of file

Line 651:21: E126 continuation line over-indented for hanging indent

Comment last updated at 2021-03-11 07:51:12 UTC

…_zip_method' into enhancement_dataframe_to_csv_use_zip_method

# Conflicts:
#	pandas/tests/frame/methods/test_to_csv.py
@CyberQin CyberQin closed this Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: DataFrame to_csv compression with 'zip' use zipfilename as archive name
4 participants