Skip to content

Excel Tests Continued Fixture Cleanup #26662

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

Merged
merged 7 commits into from
Jun 8, 2019
44 changes: 19 additions & 25 deletions pandas/tests/io/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ def frame(float_frame):
return float_frame[:10]


@pytest.fixture
def frame2(float_frame):
float_frame = float_frame.copy()
float_frame.columns = ['D', 'C', 'B', 'A']
return float_frame[:10]


@pytest.fixture
def tsframe():
return tm.makeTimeDataFrame()[:5]
Expand All @@ -58,11 +51,16 @@ def ignore_xlrd_time_clock_warning():
yield


@td.skip_if_no('xlrd', '1.0.0')
class ReadingTestsBase:
# This is based on ExcelWriterBase

@pytest.fixture(autouse=True, params=['xlrd', None])
@pytest.mark.parametrize("ext", ['.xls', '.xlsx', '.xlsm'])
class TestReaders:

@pytest.fixture(autouse=True, params=[
# Add any engines to test here
pytest.param('xlrd', marks=pytest.mark.skipif(
not td.safe_import("xlrd"), reason="no xlrd")),
pytest.param(None, marks=pytest.mark.skipif(
not td.safe_import("xlrd"), reason="no xlrd")),
])
def cd_and_set_engine(self, request, datapath, monkeypatch):
"""
Change directory and set engine for read_excel calls.
Expand All @@ -80,7 +78,6 @@ def df_ref(self):
parse_dates=True, engine='python')
return df_ref

@td.skip_if_no("xlrd", "1.0.1") # see gh-22682
Copy link
Member Author

Choose a reason for hiding this comment

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

These didn't really solve the issue they were supposed to so removed to make things cleaner. Again I think the real issue occurs when defusedxml is installed in the right environment, but will have to delve further into which environments that is exactly (orthogonal to this PR)

def test_usecols_int(self, ext, df_ref):
df_ref = df_ref.reindex(columns=["A", "B", "C"])

Expand All @@ -102,7 +99,6 @@ def test_usecols_int(self, ext, df_ref):
tm.assert_frame_equal(df1, df_ref, check_names=False)
tm.assert_frame_equal(df2, df_ref, check_names=False)

@td.skip_if_no('xlrd', '1.0.1') # GH-22682
def test_usecols_list(self, ext, df_ref):

df_ref = df_ref.reindex(columns=['B', 'C'])
Expand All @@ -115,7 +111,6 @@ def test_usecols_list(self, ext, df_ref):
tm.assert_frame_equal(df1, df_ref, check_names=False)
tm.assert_frame_equal(df2, df_ref, check_names=False)

@td.skip_if_no('xlrd', '1.0.1') # GH-22682
def test_usecols_str(self, ext, df_ref):

df1 = df_ref.reindex(columns=['A', 'B', 'C'])
Expand Down Expand Up @@ -270,7 +265,6 @@ def test_excel_passes_na(self, ext):
columns=['Test'])
tm.assert_frame_equal(parsed, expected)

@td.skip_if_no('xlrd', '1.0.1') # GH-22682
@pytest.mark.parametrize('arg', ['sheet', 'sheetname', 'parse_cols'])
def test_unexpected_kwargs_raises(self, ext, arg):
# gh-17964
Expand All @@ -281,7 +275,6 @@ def test_unexpected_kwargs_raises(self, ext, arg):
with pytest.raises(TypeError, match=msg):
pd.read_excel(excel, **kwarg)

@td.skip_if_no('xlrd', '1.0.1') # GH-22682
def test_excel_table_sheet_by_index(self, ext, df_ref):

excel = ExcelFile('test1' + ext)
Expand Down Expand Up @@ -499,7 +492,6 @@ def test_date_conversion_overflow(self, ext):
result = pd.read_excel('testdateoverflow' + ext)
tm.assert_frame_equal(result, expected)

@td.skip_if_no("xlrd", "1.0.1") # see gh-22682
def test_sheet_name_and_sheetname(self, ext, df_ref):
# gh-10559: Minor improvement: Change "sheet_name" to "sheetname"
# gh-10969: DOC: Consistent var names (sheetname vs sheet_name)
Expand Down Expand Up @@ -844,7 +836,7 @@ def test_read_excel_squeeze(self, ext):
tm.assert_series_equal(actual, expected)


@td.skip_if_no('xlrd', '1.0.0')
@td.skip_if_no('xlrd')
Copy link
Member Author

Choose a reason for hiding this comment

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

Comment might have been lost on a prior PR but the min xlrd version for pandas is 1.0.0 anyway, so this was duplicative

@pytest.mark.parametrize("ext", ['.xls', '.xlsx', '.xlsm'])
class TestRoundTrip:

Expand Down Expand Up @@ -1045,9 +1037,9 @@ def test_read_excel_parse_dates(self, ext):
tm.assert_frame_equal(df, res)


@td.skip_if_no('xlrd', '1.0.0')
@td.skip_if_no('xlrd')
@pytest.mark.parametrize("ext", ['.xls', '.xlsx', '.xlsm'])
class TestXlrdReader(ReadingTestsBase):
class TestXlrdReader:
"""
This is the base class for the xlrd tests, and 3 different file formats
are supported: xls, xlsx, xlsm
Expand Down Expand Up @@ -1149,9 +1141,11 @@ def test_excel_sheet_by_name_raise(self, *_):
with pytest.raises(xlrd.XLRDError):
pd.read_excel(xl, "0")

def test_excel_writer_context_manager(self, frame, frame2, *_):
def test_excel_writer_context_manager(self, frame, *_):
with ExcelWriter(self.path) as writer:
frame.to_excel(writer, "Data1")
frame2 = frame.copy()
frame2.columns = frame.columns[::-1]
frame2.to_excel(writer, "Data2")

with ExcelFile(self.path) as reader:
Expand Down Expand Up @@ -1318,7 +1312,7 @@ def test_sheets(self, merge_cells, engine, ext, frame, tsframe):
assert 'test1' == reader.sheet_names[0]
assert 'test2' == reader.sheet_names[1]

def test_colaliases(self, merge_cells, engine, ext, frame, frame2):
def test_colaliases(self, merge_cells, engine, ext, frame):
frame = frame.copy()
frame['A'][:5] = nan

Expand All @@ -1329,10 +1323,10 @@ def test_colaliases(self, merge_cells, engine, ext, frame, frame2):

# column aliases
col_aliases = Index(['AA', 'X', 'Y', 'Z'])
frame2.to_excel(self.path, 'test1', header=col_aliases)
frame.to_excel(self.path, 'test1', header=col_aliases)
reader = ExcelFile(self.path)
rs = pd.read_excel(reader, 'test1', index_col=0)
xp = frame2.copy()
xp = frame.copy()
xp.columns = col_aliases
tm.assert_frame_equal(xp, rs)

Expand Down