From ecf536afd25bd182fa6edb5f8c94b2f2ee7a7545 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 28 May 2019 21:54:51 -0700 Subject: [PATCH 1/4] Removed unnecessary writer subclassing --- pandas/tests/io/test_excel.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pandas/tests/io/test_excel.py b/pandas/tests/io/test_excel.py index 100de227aa97c..ae2f7b963827b 100644 --- a/pandas/tests/io/test_excel.py +++ b/pandas/tests/io/test_excel.py @@ -2065,11 +2065,10 @@ def test_path_local_path(self, merge_cells, engine, ext): @td.skip_if_no('openpyxl') -@pytest.mark.parametrize("merge_cells,ext,engine", [ - (None, '.xlsx', 'openpyxl')]) -class TestOpenpyxlTests(_WriterBase): +@pytest.mark.parametrize("ext,engine", [('.xlsx', 'openpyxl')]) +class TestOpenpyxlTests: - def test_to_excel_styleconverter(self, merge_cells, ext, engine): + def test_to_excel_styleconverter(self, ext, engine): from openpyxl import styles hstyle = { @@ -2123,7 +2122,7 @@ def test_to_excel_styleconverter(self, merge_cells, ext, engine): assert kw['number_format'] == number_format assert kw['protection'] == protection - def test_write_cells_merge_styled(self, merge_cells, ext, engine): + def test_write_cells_merge_styled(self, ext, engine): from pandas.io.formats.excel import ExcelCell sheet_name = 'merge_styled' @@ -2157,7 +2156,7 @@ def test_write_cells_merge_styled(self, merge_cells, ext, engine): @pytest.mark.parametrize("mode,expected", [ ('w', ['baz']), ('a', ['foo', 'bar', 'baz'])]) - def test_write_append_mode(self, merge_cells, ext, engine, mode, expected): + def test_write_append_mode(self, ext, engine, mode, expected): import openpyxl df = DataFrame([1], columns=['baz']) @@ -2184,7 +2183,7 @@ def test_write_append_mode(self, merge_cells, ext, engine, mode, expected): @td.skip_if_no('xlwt') @pytest.mark.parametrize("merge_cells,ext,engine", [ (None, '.xls', 'xlwt')]) -class TestXlwtTests(_WriterBase): +class TestXlwtTests: def test_excel_raise_error_on_multiindex_columns_and_no_index( self, merge_cells, ext, engine): @@ -2245,7 +2244,7 @@ def test_write_append_mode_raises(self, merge_cells, ext, engine): @td.skip_if_no('xlsxwriter') @pytest.mark.parametrize("merge_cells,ext,engine", [ (None, '.xlsx', 'xlsxwriter')]) -class TestXlsxWriterTests(_WriterBase): +class TestXlsxWriterTests: @td.skip_if_no('openpyxl') def test_column_format(self, merge_cells, ext, engine): From 2fcb2b1fbe40194522c40e12d478ec447fa05810 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 28 May 2019 21:55:03 -0700 Subject: [PATCH 2/4] Fixed issue in class creation of ExcelWriter --- pandas/io/excel/_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py index 3af6be7a371e7..24412b26b021b 100644 --- a/pandas/io/excel/_base.py +++ b/pandas/io/excel/_base.py @@ -591,7 +591,7 @@ class ExcelWriter(metaclass=abc.ABCMeta): def __new__(cls, path, engine=None, **kwargs): # only switch class if generic(ExcelWriter) - if issubclass(cls, ExcelWriter): + if cls is ExcelWriter: if engine is None or (isinstance(engine, str) and engine == 'auto'): if isinstance(path, str): From a26b6c03e8912a7766d5bfabbfabaaa0acc0a3b1 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 28 May 2019 21:57:56 -0700 Subject: [PATCH 3/4] Removed unnecessary merge_cells --- pandas/tests/io/test_excel.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/pandas/tests/io/test_excel.py b/pandas/tests/io/test_excel.py index ae2f7b963827b..9a2a77ec5d29a 100644 --- a/pandas/tests/io/test_excel.py +++ b/pandas/tests/io/test_excel.py @@ -2181,12 +2181,11 @@ def test_write_append_mode(self, ext, engine, mode, expected): @td.skip_if_no('xlwt') -@pytest.mark.parametrize("merge_cells,ext,engine", [ - (None, '.xls', 'xlwt')]) +@pytest.mark.parametrize("ext,engine", [('.xls', 'xlwt')]) class TestXlwtTests: def test_excel_raise_error_on_multiindex_columns_and_no_index( - self, merge_cells, ext, engine): + self, ext, engine): # MultiIndex as columns is not yet implemented 9794 cols = MultiIndex.from_tuples([('site', ''), ('2014', 'height'), @@ -2196,8 +2195,7 @@ def test_excel_raise_error_on_multiindex_columns_and_no_index( with ensure_clean(ext) as path: df.to_excel(path, index=False) - def test_excel_multiindex_columns_and_index_true(self, merge_cells, ext, - engine): + def test_excel_multiindex_columns_and_index_true(self, ext, engine): cols = MultiIndex.from_tuples([('site', ''), ('2014', 'height'), ('2014', 'weight')]) @@ -2205,7 +2203,7 @@ def test_excel_multiindex_columns_and_index_true(self, merge_cells, ext, with ensure_clean(ext) as path: df.to_excel(path, index=True) - def test_excel_multiindex_index(self, merge_cells, ext, engine): + def test_excel_multiindex_index(self, ext, engine): # MultiIndex as index works so assert no error #9794 cols = MultiIndex.from_tuples([('site', ''), ('2014', 'height'), @@ -2214,7 +2212,7 @@ def test_excel_multiindex_index(self, merge_cells, ext, engine): with ensure_clean(ext) as path: df.to_excel(path, index=False) - def test_to_excel_styleconverter(self, merge_cells, ext, engine): + def test_to_excel_styleconverter(self, ext, engine): import xlwt hstyle = {"font": {"bold": True}, @@ -2233,7 +2231,7 @@ def test_to_excel_styleconverter(self, merge_cells, ext, engine): assert xlwt.Alignment.HORZ_CENTER == xls_style.alignment.horz assert xlwt.Alignment.VERT_TOP == xls_style.alignment.vert - def test_write_append_mode_raises(self, merge_cells, ext, engine): + def test_write_append_mode_raises(self, ext, engine): msg = "Append mode is not supported with xlwt!" with ensure_clean(ext) as f: @@ -2242,12 +2240,11 @@ def test_write_append_mode_raises(self, merge_cells, ext, engine): @td.skip_if_no('xlsxwriter') -@pytest.mark.parametrize("merge_cells,ext,engine", [ - (None, '.xlsx', 'xlsxwriter')]) +@pytest.mark.parametrize("ext,engine", [('.xlsx', 'xlsxwriter')]) class TestXlsxWriterTests: @td.skip_if_no('openpyxl') - def test_column_format(self, merge_cells, ext, engine): + def test_column_format(self, ext, engine): # Test that column formats are applied to cells. Test for issue #9167. # Applicable to xlsxwriter only. with warnings.catch_warnings(): @@ -2291,7 +2288,7 @@ def test_column_format(self, merge_cells, ext, engine): assert read_num_format == num_format - def test_write_append_mode_raises(self, merge_cells, ext, engine): + def test_write_append_mode_raises(self, ext, engine): msg = "Append mode is not supported with xlsxwriter!" with ensure_clean(ext) as f: From 39e4d4e37dca818ce73ed06924a1a79c323bd797 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 28 May 2019 22:15:11 -0700 Subject: [PATCH 4/4] Removed sparsely used parametrization --- pandas/tests/io/test_excel.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/pandas/tests/io/test_excel.py b/pandas/tests/io/test_excel.py index 9a2a77ec5d29a..8b871caae58a6 100644 --- a/pandas/tests/io/test_excel.py +++ b/pandas/tests/io/test_excel.py @@ -2065,10 +2065,10 @@ def test_path_local_path(self, merge_cells, engine, ext): @td.skip_if_no('openpyxl') -@pytest.mark.parametrize("ext,engine", [('.xlsx', 'openpyxl')]) +@pytest.mark.parametrize("ext", ['.xlsx']) class TestOpenpyxlTests: - def test_to_excel_styleconverter(self, ext, engine): + def test_to_excel_styleconverter(self, ext): from openpyxl import styles hstyle = { @@ -2122,7 +2122,7 @@ def test_to_excel_styleconverter(self, ext, engine): assert kw['number_format'] == number_format assert kw['protection'] == protection - def test_write_cells_merge_styled(self, ext, engine): + def test_write_cells_merge_styled(self, ext): from pandas.io.formats.excel import ExcelCell sheet_name = 'merge_styled' @@ -2156,7 +2156,7 @@ def test_write_cells_merge_styled(self, ext, engine): @pytest.mark.parametrize("mode,expected", [ ('w', ['baz']), ('a', ['foo', 'bar', 'baz'])]) - def test_write_append_mode(self, ext, engine, mode, expected): + def test_write_append_mode(self, ext, mode, expected): import openpyxl df = DataFrame([1], columns=['baz']) @@ -2168,7 +2168,7 @@ def test_write_append_mode(self, ext, engine, mode, expected): wb.worksheets[1]['A1'].value = 'bar' wb.save(f) - writer = ExcelWriter(f, engine=engine, mode=mode) + writer = ExcelWriter(f, engine='openpyxl', mode=mode) df.to_excel(writer, sheet_name='baz', index=False) writer.save() @@ -2181,11 +2181,11 @@ def test_write_append_mode(self, ext, engine, mode, expected): @td.skip_if_no('xlwt') -@pytest.mark.parametrize("ext,engine", [('.xls', 'xlwt')]) +@pytest.mark.parametrize("ext,", ['.xls']) class TestXlwtTests: def test_excel_raise_error_on_multiindex_columns_and_no_index( - self, ext, engine): + self, ext): # MultiIndex as columns is not yet implemented 9794 cols = MultiIndex.from_tuples([('site', ''), ('2014', 'height'), @@ -2195,7 +2195,7 @@ def test_excel_raise_error_on_multiindex_columns_and_no_index( with ensure_clean(ext) as path: df.to_excel(path, index=False) - def test_excel_multiindex_columns_and_index_true(self, ext, engine): + def test_excel_multiindex_columns_and_index_true(self, ext): cols = MultiIndex.from_tuples([('site', ''), ('2014', 'height'), ('2014', 'weight')]) @@ -2203,7 +2203,7 @@ def test_excel_multiindex_columns_and_index_true(self, ext, engine): with ensure_clean(ext) as path: df.to_excel(path, index=True) - def test_excel_multiindex_index(self, ext, engine): + def test_excel_multiindex_index(self, ext): # MultiIndex as index works so assert no error #9794 cols = MultiIndex.from_tuples([('site', ''), ('2014', 'height'), @@ -2212,7 +2212,7 @@ def test_excel_multiindex_index(self, ext, engine): with ensure_clean(ext) as path: df.to_excel(path, index=False) - def test_to_excel_styleconverter(self, ext, engine): + def test_to_excel_styleconverter(self, ext): import xlwt hstyle = {"font": {"bold": True}, @@ -2231,20 +2231,20 @@ def test_to_excel_styleconverter(self, ext, engine): assert xlwt.Alignment.HORZ_CENTER == xls_style.alignment.horz assert xlwt.Alignment.VERT_TOP == xls_style.alignment.vert - def test_write_append_mode_raises(self, ext, engine): + def test_write_append_mode_raises(self, ext): msg = "Append mode is not supported with xlwt!" with ensure_clean(ext) as f: with pytest.raises(ValueError, match=msg): - ExcelWriter(f, engine=engine, mode='a') + ExcelWriter(f, engine='xlwt', mode='a') @td.skip_if_no('xlsxwriter') -@pytest.mark.parametrize("ext,engine", [('.xlsx', 'xlsxwriter')]) +@pytest.mark.parametrize("ext", ['.xlsx']) class TestXlsxWriterTests: @td.skip_if_no('openpyxl') - def test_column_format(self, ext, engine): + def test_column_format(self, ext): # Test that column formats are applied to cells. Test for issue #9167. # Applicable to xlsxwriter only. with warnings.catch_warnings(): @@ -2288,12 +2288,12 @@ def test_column_format(self, ext, engine): assert read_num_format == num_format - def test_write_append_mode_raises(self, ext, engine): + def test_write_append_mode_raises(self, ext): msg = "Append mode is not supported with xlsxwriter!" with ensure_clean(ext) as f: with pytest.raises(ValueError, match=msg): - ExcelWriter(f, engine=engine, mode='a') + ExcelWriter(f, engine='xlsxwriter', mode='a') class TestExcelWriterEngineTests: