From 52b886958842ea34a9087c3f724eecbb3b54ba4f Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Mon, 8 Feb 2021 09:48:48 +0800 Subject: [PATCH 01/22] enhancement to dataframe to_csv zip method,add this step to perform like gz or bz2 --- pandas/io/common.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/io/common.py b/pandas/io/common.py index e5a1f58ec6cd2..429df94271693 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -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) + if archive_name.endswith('.zip'): + self.archive_name = archive_name[:-4] + kwargs_zip: Dict[str, Any] = {"compression": zipfile.ZIP_DEFLATED} kwargs_zip.update(kwargs) From 40af4986ed57bd5a24f6487e4b4b3f4f3899f4d3 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Mon, 8 Feb 2021 15:00:25 +0800 Subject: [PATCH 02/22] fix test test_to_csv_zip_arguments --- pandas/tests/io/formats/test_to_csv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index ef4de5961a696..5bfbc17af2604 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -546,7 +546,7 @@ def test_to_csv_zip_arguments(self, compression, archive_name): path, compression={"method": compression, "archive_name": archive_name} ) with ZipFile(path) as zp: - expected_arcname = path if archive_name is None else archive_name + expected_arcname = path[:-4] if archive_name is None else archive_name expected_arcname = os.path.basename(expected_arcname) assert len(zp.filelist) == 1 archived_file = os.path.basename(zp.filelist[0].filename) From f69cb82aac42d6cfdb7dba01216e207450df6609 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Tue, 9 Feb 2021 09:00:44 +0800 Subject: [PATCH 03/22] test_to_csv_compression_encoding_gcs --- pandas/io/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index 429df94271693..80268a3b12093 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -728,7 +728,7 @@ def __init__( # 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) + archive_name = stringify_path(file) if archive_name.endswith('.zip'): self.archive_name = archive_name[:-4] From 3a4fb928eb2cf3a707031641c7a4a765324a234d Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Tue, 9 Feb 2021 09:14:59 +0800 Subject: [PATCH 04/22] fix pre-commit test failed --- pandas/io/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index 80268a3b12093..0a143a90499c2 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -729,7 +729,7 @@ def __init__( # add csv file name like gz or bz2 if archive_name is None and isinstance(file, (os.PathLike, str)): archive_name = stringify_path(file) - if archive_name.endswith('.zip'): + if archive_name.endswith(".zip"): self.archive_name = archive_name[:-4] kwargs_zip: Dict[str, Any] = {"compression": zipfile.ZIP_DEFLATED} From 6b1314df7fe28f9091a2d59ca6c3ef5127207c9b Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Tue, 9 Feb 2021 09:57:47 +0800 Subject: [PATCH 05/22] fix pre-commit test failed --- pandas/io/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index 0a143a90499c2..6dc91624e90f8 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -728,7 +728,7 @@ def __init__( # add csv file name like gz or bz2 if archive_name is None and isinstance(file, (os.PathLike, str)): - archive_name = stringify_path(file) + archive_name = os.path.basename(file) if archive_name.endswith(".zip"): self.archive_name = archive_name[:-4] From 2343eb43af903de7727f8fb348cff38f4d4ef06c Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Tue, 9 Feb 2021 12:27:01 +0800 Subject: [PATCH 06/22] try fix ci errors --- pandas/io/common.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index 6dc91624e90f8..1dc03ee29e291 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -725,13 +725,10 @@ def __init__( mode = mode.replace("b", "") 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) if archive_name.endswith(".zip"): self.archive_name = archive_name[:-4] - kwargs_zip: Dict[str, Any] = {"compression": zipfile.ZIP_DEFLATED} kwargs_zip.update(kwargs) From 2b5c645764ed046edd8924a800886a6dbdc42490 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Wed, 10 Mar 2021 18:34:48 +0800 Subject: [PATCH 07/22] add test for to_csv with filename=='xxxx.csv.zip',filename in zipfile expected to be 'xxxx.cvs' --- pandas/tests/frame/methods/test_to_csv.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index aed784a6e4c3c..1e35f04efaa2b 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -1333,3 +1333,26 @@ def test_to_csv_numpy_16_bug(self): result = buf.getvalue() assert "2000-01-01" in result + + + @pytest.mark.parametrize( + "df,csv_name", [ + ( + DataFrame({'a': [1, 2, 3, 4, 5], 'b': [4, 5, 6, 7, 8]}), + "test_to_csv_zipped_content_name.csv" + ) + ] + ) + def test_to_csv_content_name_in_zipped_file(self, df, csv_name): + from zipfile import ZipFile + from pathlib import Path + suffix_zip_name = csv_name + '.zip' + with tm.ensure_clean(suffix_zip_name) as pth: + '''ensure_clean will add random str before suffix_zip_name,need Path.stem to get real file name''' + df.to_csv(pth) + zf = ZipFile(pth) + pp = Path(pth) + result = zf.filelist[0].filename + expected = pp.stem + zf.close() + assert result == expected \ No newline at end of file From 8bd26193137587ff02999add10deb0a0fc830439 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Wed, 10 Mar 2021 18:34:48 +0800 Subject: [PATCH 08/22] add test for to_csv with filename=='xxxx.csv.zip',filename in zipfile expected to be 'xxxx.cvs' --- pandas/tests/frame/methods/test_to_csv.py | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index aed784a6e4c3c..06d09a1d5e6f4 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -1333,3 +1333,28 @@ def test_to_csv_numpy_16_bug(self): result = buf.getvalue() assert "2000-01-01" in result + + @pytest.mark.parametrize( + "df,csv_name", [ + ( + DataFrame({'a': [1, 2, 3, 4, 5], 'b': [4, 5, 6, 7, 8]}), + "test_to_csv_zipped_content_name.csv" + ) + ] + ) + def test_to_csv_content_name_in_zipped_file(self, df, csv_name): + from zipfile import ZipFile + from pathlib import Path + suffix_zip_name = csv_name + '.zip' + with tm.ensure_clean(suffix_zip_name) as pth: + ''' + ensure_clean will add random str before suffix_zip_name, + need Path.stem to get real file name + ''' + df.to_csv(pth) + zf = ZipFile(pth) + pp = Path(pth) + result = zf.filelist[0].filename + expected = pp.stem + zf.close() + assert result == expected From f9a97bd45e1b47b4cd12824da6db179c2b20fb40 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Wed, 10 Mar 2021 18:49:30 +0800 Subject: [PATCH 09/22] pre-commit-fix --- pandas/tests/frame/methods/test_to_csv.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index 06d09a1d5e6f4..6088aeeadf059 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -24,6 +24,9 @@ from pandas.io.common import get_handle +from zipfile import ZipFile +from pathlib import Path + MIXED_FLOAT_DTYPES = ["float16", "float32", "float64"] MIXED_INT_DTYPES = [ "uint8", @@ -1343,14 +1346,10 @@ def test_to_csv_numpy_16_bug(self): ] ) def test_to_csv_content_name_in_zipped_file(self, df, csv_name): - from zipfile import ZipFile - from pathlib import Path suffix_zip_name = csv_name + '.zip' with tm.ensure_clean(suffix_zip_name) as pth: - ''' - ensure_clean will add random str before suffix_zip_name, - need Path.stem to get real file name - ''' + #ensure_clean will add random str before suffix_zip_name, + #need Path.stem to get real file name df.to_csv(pth) zf = ZipFile(pth) pp = Path(pth) From 9dea00adb0ad92ddf1bebf1092bc0627741668b1 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Wed, 10 Mar 2021 19:05:07 +0800 Subject: [PATCH 10/22] pre-commit-fix2 --- pandas/tests/frame/methods/test_to_csv.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index 6088aeeadf059..5e9dbf380c277 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -26,7 +26,6 @@ from zipfile import ZipFile from pathlib import Path - MIXED_FLOAT_DTYPES = ["float16", "float32", "float64"] MIXED_INT_DTYPES = [ "uint8", @@ -1338,18 +1337,19 @@ def test_to_csv_numpy_16_bug(self): assert "2000-01-01" in result @pytest.mark.parametrize( - "df,csv_name", [ + "df,csv_name", + [ ( - DataFrame({'a': [1, 2, 3, 4, 5], 'b': [4, 5, 6, 7, 8]}), - "test_to_csv_zipped_content_name.csv" + DataFrame({"a": [1, 2, 3, 4, 5], "b": [4, 5, 6, 7, 8]}), + "test_to_csv_zipped_content_name.csv", ) - ] + ], ) def test_to_csv_content_name_in_zipped_file(self, df, csv_name): - suffix_zip_name = csv_name + '.zip' + suffix_zip_name = csv_name + ".zip" with tm.ensure_clean(suffix_zip_name) as pth: - #ensure_clean will add random str before suffix_zip_name, - #need Path.stem to get real file name + # ensure_clean will add random str before suffix_zip_name, + # need Path.stem to get real file name df.to_csv(pth) zf = ZipFile(pth) pp = Path(pth) From e20c663eb689b32e1d093b85f9b55e32b45ad9bc Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Wed, 10 Mar 2021 19:41:26 +0800 Subject: [PATCH 11/22] pre-commit-fix3 --- pandas/tests/frame/methods/test_to_csv.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index 5e9dbf380c277..2129ee46835b2 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -1,6 +1,8 @@ import csv from io import StringIO import os +from zipfile import ZipFile +from pathlib import Path import numpy as np import pytest @@ -24,8 +26,6 @@ from pandas.io.common import get_handle -from zipfile import ZipFile -from pathlib import Path MIXED_FLOAT_DTYPES = ["float16", "float32", "float64"] MIXED_INT_DTYPES = [ "uint8", From ebb298d3f114c07cb82b755b10b2fd31b81814b9 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Mon, 8 Feb 2021 09:48:48 +0800 Subject: [PATCH 12/22] enhancement to dataframe to_csv zip method,add this step to perform like gz or bz2 --- pandas/io/common.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/io/common.py b/pandas/io/common.py index b87e8fcae1064..b8bbd4fc08c51 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -776,6 +776,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) + if archive_name.endswith('.zip'): + self.archive_name = archive_name[:-4] + kwargs_zip: Dict[str, Any] = {"compression": zipfile.ZIP_DEFLATED} kwargs_zip.update(kwargs) From 437510c55e71fc5d487c347152da97bfff000d62 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Mon, 8 Feb 2021 15:00:25 +0800 Subject: [PATCH 13/22] fix test test_to_csv_zip_arguments --- pandas/tests/io/formats/test_to_csv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index 5e599818308b8..8836120e37c46 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -549,7 +549,7 @@ def test_to_csv_zip_arguments(self, compression, archive_name): path, compression={"method": compression, "archive_name": archive_name} ) with ZipFile(path) as zp: - expected_arcname = path if archive_name is None else archive_name + expected_arcname = path[:-4] if archive_name is None else archive_name expected_arcname = os.path.basename(expected_arcname) assert len(zp.filelist) == 1 archived_file = os.path.basename(zp.filelist[0].filename) From 5ce76506dcc85747aff21902e55142bb0443b0f6 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Tue, 9 Feb 2021 09:00:44 +0800 Subject: [PATCH 14/22] test_to_csv_compression_encoding_gcs --- pandas/io/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index b8bbd4fc08c51..7871a05d7906c 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -778,7 +778,7 @@ def __init__( # 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) + archive_name = stringify_path(file) if archive_name.endswith('.zip'): self.archive_name = archive_name[:-4] From 4cfd964dc7cbaa492e87b05d28347ae8f9012247 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Tue, 9 Feb 2021 09:14:59 +0800 Subject: [PATCH 15/22] fix pre-commit test failed --- pandas/io/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index 7871a05d7906c..aac883d163a36 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -779,7 +779,7 @@ def __init__( # add csv file name like gz or bz2 if archive_name is None and isinstance(file, (os.PathLike, str)): archive_name = stringify_path(file) - if archive_name.endswith('.zip'): + if archive_name.endswith(".zip"): self.archive_name = archive_name[:-4] kwargs_zip: Dict[str, Any] = {"compression": zipfile.ZIP_DEFLATED} From 7c1bad8489e26f36c1f0bd2937c03178f3584e73 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Tue, 9 Feb 2021 09:57:47 +0800 Subject: [PATCH 16/22] fix pre-commit test failed --- pandas/io/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index aac883d163a36..06a0d9a928234 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -778,7 +778,7 @@ def __init__( # add csv file name like gz or bz2 if archive_name is None and isinstance(file, (os.PathLike, str)): - archive_name = stringify_path(file) + archive_name = os.path.basename(file) if archive_name.endswith(".zip"): self.archive_name = archive_name[:-4] From def02897781087b7b60078d4d0e9d2b566b2615e Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Tue, 9 Feb 2021 12:27:01 +0800 Subject: [PATCH 17/22] try fix ci errors --- pandas/io/common.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index 06a0d9a928234..68d75a26dbda3 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -775,13 +775,10 @@ def __init__( mode = mode.replace("b", "") 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) if archive_name.endswith(".zip"): self.archive_name = archive_name[:-4] - kwargs_zip: Dict[str, Any] = {"compression": zipfile.ZIP_DEFLATED} kwargs_zip.update(kwargs) From 799a2d5e6d0c718d7e89178c0c8e9da5f27df11c Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Wed, 10 Mar 2021 18:34:48 +0800 Subject: [PATCH 18/22] add test for to_csv with filename=='xxxx.csv.zip',filename in zipfile expected to be 'xxxx.cvs' --- pandas/tests/frame/methods/test_to_csv.py | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index aed784a6e4c3c..06d09a1d5e6f4 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -1333,3 +1333,28 @@ def test_to_csv_numpy_16_bug(self): result = buf.getvalue() assert "2000-01-01" in result + + @pytest.mark.parametrize( + "df,csv_name", [ + ( + DataFrame({'a': [1, 2, 3, 4, 5], 'b': [4, 5, 6, 7, 8]}), + "test_to_csv_zipped_content_name.csv" + ) + ] + ) + def test_to_csv_content_name_in_zipped_file(self, df, csv_name): + from zipfile import ZipFile + from pathlib import Path + suffix_zip_name = csv_name + '.zip' + with tm.ensure_clean(suffix_zip_name) as pth: + ''' + ensure_clean will add random str before suffix_zip_name, + need Path.stem to get real file name + ''' + df.to_csv(pth) + zf = ZipFile(pth) + pp = Path(pth) + result = zf.filelist[0].filename + expected = pp.stem + zf.close() + assert result == expected From 86ad72f9d15e9f97a3d1a8059cfb285e858ce304 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Wed, 10 Mar 2021 18:49:30 +0800 Subject: [PATCH 19/22] pre-commit-fix --- pandas/tests/frame/methods/test_to_csv.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index 06d09a1d5e6f4..6088aeeadf059 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -24,6 +24,9 @@ from pandas.io.common import get_handle +from zipfile import ZipFile +from pathlib import Path + MIXED_FLOAT_DTYPES = ["float16", "float32", "float64"] MIXED_INT_DTYPES = [ "uint8", @@ -1343,14 +1346,10 @@ def test_to_csv_numpy_16_bug(self): ] ) def test_to_csv_content_name_in_zipped_file(self, df, csv_name): - from zipfile import ZipFile - from pathlib import Path suffix_zip_name = csv_name + '.zip' with tm.ensure_clean(suffix_zip_name) as pth: - ''' - ensure_clean will add random str before suffix_zip_name, - need Path.stem to get real file name - ''' + #ensure_clean will add random str before suffix_zip_name, + #need Path.stem to get real file name df.to_csv(pth) zf = ZipFile(pth) pp = Path(pth) From 0644612256991aa3b7e7f045d8a5e1ec4448694d Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Wed, 10 Mar 2021 19:05:07 +0800 Subject: [PATCH 20/22] pre-commit-fix2 --- pandas/tests/frame/methods/test_to_csv.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index 6088aeeadf059..5e9dbf380c277 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -26,7 +26,6 @@ from zipfile import ZipFile from pathlib import Path - MIXED_FLOAT_DTYPES = ["float16", "float32", "float64"] MIXED_INT_DTYPES = [ "uint8", @@ -1338,18 +1337,19 @@ def test_to_csv_numpy_16_bug(self): assert "2000-01-01" in result @pytest.mark.parametrize( - "df,csv_name", [ + "df,csv_name", + [ ( - DataFrame({'a': [1, 2, 3, 4, 5], 'b': [4, 5, 6, 7, 8]}), - "test_to_csv_zipped_content_name.csv" + DataFrame({"a": [1, 2, 3, 4, 5], "b": [4, 5, 6, 7, 8]}), + "test_to_csv_zipped_content_name.csv", ) - ] + ], ) def test_to_csv_content_name_in_zipped_file(self, df, csv_name): - suffix_zip_name = csv_name + '.zip' + suffix_zip_name = csv_name + ".zip" with tm.ensure_clean(suffix_zip_name) as pth: - #ensure_clean will add random str before suffix_zip_name, - #need Path.stem to get real file name + # ensure_clean will add random str before suffix_zip_name, + # need Path.stem to get real file name df.to_csv(pth) zf = ZipFile(pth) pp = Path(pth) From 2b138b6fe5cfa3ef3912f0130ddd0ec23b27e608 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Wed, 10 Mar 2021 19:41:26 +0800 Subject: [PATCH 21/22] pre-commit-fix3 --- pandas/tests/frame/methods/test_to_csv.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index 5e9dbf380c277..2129ee46835b2 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -1,6 +1,8 @@ import csv from io import StringIO import os +from zipfile import ZipFile +from pathlib import Path import numpy as np import pytest @@ -24,8 +26,6 @@ from pandas.io.common import get_handle -from zipfile import ZipFile -from pathlib import Path MIXED_FLOAT_DTYPES = ["float16", "float32", "float64"] MIXED_INT_DTYPES = [ "uint8", From 958f088c7ab48c5cf7814c87a4df3e43decaebe5 Mon Sep 17 00:00:00 2001 From: Hu Qin Date: Thu, 11 Mar 2021 09:23:12 +0800 Subject: [PATCH 22/22] pre-commit-fix5 --- pandas/tests/frame/methods/test_to_csv.py | 23 ---------------------- pandas/tests/io/formats/test_to_csv.py | 24 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index 2129ee46835b2..1eb9f0bea23dd 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -1,8 +1,6 @@ import csv from io import StringIO import os -from zipfile import ZipFile -from pathlib import Path import numpy as np import pytest @@ -1336,24 +1334,3 @@ def test_to_csv_numpy_16_bug(self): result = buf.getvalue() assert "2000-01-01" in result - @pytest.mark.parametrize( - "df,csv_name", - [ - ( - DataFrame({"a": [1, 2, 3, 4, 5], "b": [4, 5, 6, 7, 8]}), - "test_to_csv_zipped_content_name.csv", - ) - ], - ) - def test_to_csv_content_name_in_zipped_file(self, df, csv_name): - suffix_zip_name = csv_name + ".zip" - with tm.ensure_clean(suffix_zip_name) as pth: - # ensure_clean will add random str before suffix_zip_name, - # need Path.stem to get real file name - df.to_csv(pth) - zf = ZipFile(pth) - pp = Path(pth) - result = zf.filelist[0].filename - expected = pp.stem - zf.close() - assert result == expected diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index 8836120e37c46..b8c7f8dac0d02 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -644,6 +644,30 @@ def test_to_csv_encoding_binary_handle(self, mode): handle.seek(0) assert handle.read().startswith(b'\xef\xbb\xbf""') + @pytest.mark.parametrize( + "df,csv_name", + [ + ( + DataFrame({"a": [1, 2, 3, 4, 5], "b": [4, 5, 6, 7, 8]}), + "test_to_csv_zipped_content_name.csv", + ) + ], + ) + def test_to_csv_content_name_in_zipped_file(self, df, csv_name): + from zipfile import ZipFile + from pathlib import Path + + suffix_zip_name = csv_name + ".zip" + with tm.ensure_clean(suffix_zip_name) as pth: + # ensure_clean will add random str before suffix_zip_name, + # need Path.stem to get real file name + df.to_csv(pth) + zf = ZipFile(pth) + pp = Path(pth) + result = zf.filelist[0].filename + expected = pp.stem + zf.close() + assert result == expected def test_to_csv_iterative_compression_name(compression): # GH 38714