Skip to content

Commit

Permalink
Fixed test util imports in pandas/tests/io (pandas-dev#29299)
Browse files Browse the repository at this point in the history
  • Loading branch information
SaturnFromTitan authored and proost committed Dec 19, 2019
1 parent 33344bd commit def514d
Show file tree
Hide file tree
Showing 19 changed files with 260 additions and 280 deletions.
6 changes: 3 additions & 3 deletions pandas/tests/io/excel/test_openpyxl.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from pandas import DataFrame
from pandas.util.testing import ensure_clean
import pandas.util.testing as tm

from pandas.io.excel import ExcelWriter, _OpenpyxlWriter

Expand Down Expand Up @@ -65,7 +65,7 @@ def test_write_cells_merge_styled(ext):
)
]

with ensure_clean(ext) as path:
with tm.ensure_clean(ext) as path:
writer = _OpenpyxlWriter(path)
writer.write_cells(initial_cells, sheet_name=sheet_name)
writer.write_cells(merge_cells, sheet_name=sheet_name)
Expand All @@ -83,7 +83,7 @@ def test_write_cells_merge_styled(ext):
def test_write_append_mode(ext, mode, expected):
df = DataFrame([1], columns=["baz"])

with ensure_clean(ext) as f:
with tm.ensure_clean(ext) as f:
wb = openpyxl.Workbook()
wb.worksheets[0].title = "foo"
wb.worksheets[0]["A1"].value = "foo"
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/excel/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest

from pandas import DataFrame
from pandas.util.testing import ensure_clean
import pandas.util.testing as tm

from pandas.io.excel import ExcelWriter
from pandas.io.formats.excel import ExcelFormatter
Expand Down Expand Up @@ -70,7 +70,7 @@ def custom_converter(css):
# Prepare spreadsheets

df = DataFrame(np.random.randn(11, 3))
with ensure_clean(".xlsx" if engine != "xlwt" else ".xls") as path:
with tm.ensure_clean(".xlsx" if engine != "xlwt" else ".xls") as path:
writer = ExcelWriter(path, engine=engine)
df.to_excel(writer, sheet_name="frame")
df.style.to_excel(writer, sheet_name="unstyled")
Expand Down
33 changes: 16 additions & 17 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import pandas as pd
from pandas import DataFrame, Index, MultiIndex, get_option, set_option
import pandas.util.testing as tm
from pandas.util.testing import ensure_clean, makeCustomDataframe as mkdf

from pandas.io.excel import (
ExcelFile,
Expand All @@ -29,7 +28,7 @@ def path(ext):
"""
Fixture to open file for use in each test case.
"""
with ensure_clean(ext) as file_path:
with tm.ensure_clean(ext) as file_path:
yield file_path


Expand Down Expand Up @@ -64,7 +63,7 @@ def test_read_one_empty_col_no_header(self, ext, header, expected):
filename = "no_header"
df = pd.DataFrame([["", 1, 100], ["", 2, 200], ["", 3, 300], ["", 4, 400]])

with ensure_clean(ext) as path:
with tm.ensure_clean(ext) as path:
df.to_excel(path, filename, index=False, header=False)
result = pd.read_excel(path, filename, usecols=[0], header=header)

Expand All @@ -80,7 +79,7 @@ def test_read_one_empty_col_with_header(self, ext, header, expected):
filename = "with_header"
df = pd.DataFrame([["", 1, 100], ["", 2, 200], ["", 3, 300], ["", 4, 400]])

with ensure_clean(ext) as path:
with tm.ensure_clean(ext) as path:
df.to_excel(path, "with_header", index=False, header=True)
result = pd.read_excel(path, filename, usecols=[0], header=header)

Expand All @@ -93,7 +92,7 @@ def test_set_column_names_in_parameter(self, ext):
# keyword argument names
refdf = pd.DataFrame([[1, "foo"], [2, "bar"], [3, "baz"]], columns=["a", "b"])

with ensure_clean(ext) as pth:
with tm.ensure_clean(ext) as pth:
with ExcelWriter(pth) as writer:
refdf.to_excel(writer, "Data_no_head", header=False, index=False)
refdf.to_excel(writer, "Data_with_head", index=False)
Expand Down Expand Up @@ -127,7 +126,7 @@ def tdf(col_sheet_name):
dfs = [tdf(s) for s in sheets]
dfs = dict(zip(sheets, dfs))

with ensure_clean(ext) as pth:
with tm.ensure_clean(ext) as pth:
with ExcelWriter(pth) as ew:
for sheetname, df in dfs.items():
df.to_excel(ew, sheetname)
Expand All @@ -140,7 +139,7 @@ def tdf(col_sheet_name):
@td.skip_if_no("xlsxwriter")
def test_read_excel_multiindex_empty_level(self, ext):
# see gh-12453
with ensure_clean(ext) as path:
with tm.ensure_clean(ext) as path:
df = DataFrame(
{
("One", "x"): {0: 1},
Expand Down Expand Up @@ -194,7 +193,7 @@ def test_excel_multindex_roundtrip(
self, ext, c_idx_names, r_idx_names, c_idx_levels, r_idx_levels
):
# see gh-4679
with ensure_clean(ext) as pth:
with tm.ensure_clean(ext) as pth:
if c_idx_levels == 1 and c_idx_names:
pytest.skip(
"Column index name cannot be serialized unless it's a MultiIndex"
Expand All @@ -204,7 +203,9 @@ def test_excel_multindex_roundtrip(
# unnamed levels, not Nones.
check_names = r_idx_names or r_idx_levels <= 1

df = mkdf(5, 5, c_idx_names, r_idx_names, c_idx_levels, r_idx_levels)
df = tm.makeCustomDataframe(
5, 5, c_idx_names, r_idx_names, c_idx_levels, r_idx_levels
)
df.to_excel(pth)

act = pd.read_excel(
Expand Down Expand Up @@ -243,7 +244,7 @@ def test_read_excel_parse_dates(self, ext):
df2 = df.copy()
df2["date_strings"] = df2["date_strings"].dt.strftime("%m/%d/%Y")

with ensure_clean(ext) as pth:
with tm.ensure_clean(ext) as pth:
df2.to_excel(pth)

res = pd.read_excel(pth, index_col=0)
Expand Down Expand Up @@ -581,7 +582,7 @@ def test_excel_date_datetime_format(self, engine, ext, path):
columns=["X", "Y"],
)

with ensure_clean(ext) as filename2:
with tm.ensure_clean(ext) as filename2:
writer1 = ExcelWriter(path)
writer2 = ExcelWriter(
filename2,
Expand Down Expand Up @@ -778,13 +779,13 @@ def test_to_excel_output_encoding(self, ext):
columns=["X\u0193", "Y", "Z"],
)

with ensure_clean("__tmp_to_excel_float_format__." + ext) as filename:
with tm.ensure_clean("__tmp_to_excel_float_format__." + ext) as filename:
df.to_excel(filename, sheet_name="TestSheet", encoding="utf8")
result = pd.read_excel(filename, "TestSheet", encoding="utf8", index_col=0)
tm.assert_frame_equal(result, df)

def test_to_excel_unicode_filename(self, ext, path):
with ensure_clean("\u0192u." + ext) as filename:
with tm.ensure_clean("\u0192u." + ext) as filename:
try:
f = open(filename, "wb")
except UnicodeEncodeError:
Expand Down Expand Up @@ -932,12 +933,10 @@ def roundtrip(data, header=True, parser_hdr=0, index=True):
nrows = 5
ncols = 3

from pandas.util.testing import makeCustomDataframe as mkdf

# ensure limited functionality in 0.10
# override of gh-2370 until sorted out in 0.11

df = mkdf(
df = tm.makeCustomDataframe(
nrows, ncols, r_idx_nlevels=r_idx_nlevels, c_idx_nlevels=c_idx_nlevels
)

Expand Down Expand Up @@ -1216,7 +1215,7 @@ class TestExcelWriterEngineTests:
],
)
def test_ExcelWriter_dispatch(self, klass, ext):
with ensure_clean(ext) as path:
with tm.ensure_clean(ext) as path:
writer = ExcelWriter(path)
if ext == ".xlsx" and td.safe_import("xlsxwriter"):
# xlsxwriter has preference over openpyxl if both installed
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/io/excel/test_xlrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import pandas as pd
import pandas.util.testing as tm
from pandas.util.testing import ensure_clean

from pandas.io.excel import ExcelFile

Expand All @@ -22,7 +21,7 @@ def test_read_xlrd_book(read_ext, frame):
engine = "xlrd"
sheet_name = "SheetA"

with ensure_clean(read_ext) as pth:
with tm.ensure_clean(read_ext) as pth:
df.to_excel(pth, sheet_name)
book = xlrd.open_workbook(pth)

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/io/excel/test_xlsxwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from pandas import DataFrame
from pandas.util.testing import ensure_clean
import pandas.util.testing as tm

from pandas.io.excel import ExcelWriter

Expand All @@ -20,7 +20,7 @@ def test_column_format(ext):
warnings.simplefilter("ignore")
openpyxl = pytest.importorskip("openpyxl")

with ensure_clean(ext) as path:
with tm.ensure_clean(ext) as path:
frame = DataFrame({"A": [123456, 123456], "B": [123456, 123456]})

writer = ExcelWriter(path)
Expand Down Expand Up @@ -59,6 +59,6 @@ def test_column_format(ext):
def test_write_append_mode_raises(ext):
msg = "Append mode is not supported with xlsxwriter!"

with ensure_clean(ext) as f:
with tm.ensure_clean(ext) as f:
with pytest.raises(ValueError, match=msg):
ExcelWriter(f, engine="xlsxwriter", mode="a")
10 changes: 5 additions & 5 deletions pandas/tests/io/excel/test_xlwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pandas as pd
from pandas import DataFrame, MultiIndex
from pandas.util.testing import ensure_clean
import pandas.util.testing as tm

from pandas.io.excel import ExcelWriter, _XlwtWriter

Expand All @@ -19,7 +19,7 @@ def test_excel_raise_error_on_multiindex_columns_and_no_index(ext):
)
df = DataFrame(np.random.randn(10, 3), columns=cols)
with pytest.raises(NotImplementedError):
with ensure_clean(ext) as path:
with tm.ensure_clean(ext) as path:
df.to_excel(path, index=False)


Expand All @@ -28,7 +28,7 @@ def test_excel_multiindex_columns_and_index_true(ext):
[("site", ""), ("2014", "height"), ("2014", "weight")]
)
df = pd.DataFrame(np.random.randn(10, 3), columns=cols)
with ensure_clean(ext) as path:
with tm.ensure_clean(ext) as path:
df.to_excel(path, index=True)


Expand All @@ -38,7 +38,7 @@ def test_excel_multiindex_index(ext):
[("site", ""), ("2014", "height"), ("2014", "weight")]
)
df = DataFrame(np.random.randn(3, 10), index=cols)
with ensure_clean(ext) as path:
with tm.ensure_clean(ext) as path:
df.to_excel(path, index=False)


Expand All @@ -62,6 +62,6 @@ def test_to_excel_styleconverter(ext):
def test_write_append_mode_raises(ext):
msg = "Append mode is not supported with xlwt!"

with ensure_clean(ext) as f:
with tm.ensure_clean(ext) as f:
with pytest.raises(ValueError, match=msg):
ExcelWriter(f, engine="xlwt", mode="a")
13 changes: 6 additions & 7 deletions pandas/tests/io/json/test_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import pandas as pd
import pandas.util.testing as tm
from pandas.util.testing import assert_frame_equal


def test_compression_roundtrip(compression):
Expand All @@ -16,12 +15,12 @@ def test_compression_roundtrip(compression):

with tm.ensure_clean() as path:
df.to_json(path, compression=compression)
assert_frame_equal(df, pd.read_json(path, compression=compression))
tm.assert_frame_equal(df, pd.read_json(path, compression=compression))

# explicitly ensure file was compressed.
with tm.decompress_file(path, compression) as fh:
result = fh.read().decode("utf8")
assert_frame_equal(df, pd.read_json(result))
tm.assert_frame_equal(df, pd.read_json(result))


def test_read_zipped_json(datapath):
Expand All @@ -31,7 +30,7 @@ def test_read_zipped_json(datapath):
compressed_path = datapath("io", "json", "data", "tsframe_v012.json.zip")
compressed_df = pd.read_json(compressed_path, compression="zip")

assert_frame_equal(uncompressed_df, compressed_df)
tm.assert_frame_equal(uncompressed_df, compressed_df)


@td.skip_if_not_us_locale
Expand All @@ -46,7 +45,7 @@ def test_with_s3_url(compression, s3_resource):
s3_resource.Bucket("pandas-test").put_object(Key="test-1", Body=f)

roundtripped_df = pd.read_json("s3://pandas-test/test-1", compression=compression)
assert_frame_equal(df, roundtripped_df)
tm.assert_frame_equal(df, roundtripped_df)


def test_lines_with_compression(compression):
Expand All @@ -55,7 +54,7 @@ def test_lines_with_compression(compression):
df = pd.read_json('{"a": [1, 2, 3], "b": [4, 5, 6]}')
df.to_json(path, orient="records", lines=True, compression=compression)
roundtripped_df = pd.read_json(path, lines=True, compression=compression)
assert_frame_equal(df, roundtripped_df)
tm.assert_frame_equal(df, roundtripped_df)


def test_chunksize_with_compression(compression):
Expand All @@ -66,7 +65,7 @@ def test_chunksize_with_compression(compression):

res = pd.read_json(path, lines=True, chunksize=1, compression=compression)
roundtripped_df = pd.concat(res)
assert_frame_equal(df, roundtripped_df)
tm.assert_frame_equal(df, roundtripped_df)


def test_write_unsupported_compression_type():
Expand Down
Loading

0 comments on commit def514d

Please sign in to comment.