Skip to content

Commit 90132de

Browse files
SaturnFromTitangfyoung
authored andcommitted
Fixed test util imports in pandas/tests/io (#29299)
xref: #29272
1 parent 291f3c5 commit 90132de

19 files changed

+260
-280
lines changed

pandas/tests/io/excel/test_openpyxl.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from pandas import DataFrame
4-
from pandas.util.testing import ensure_clean
4+
import pandas.util.testing as tm
55

66
from pandas.io.excel import ExcelWriter, _OpenpyxlWriter
77

@@ -65,7 +65,7 @@ def test_write_cells_merge_styled(ext):
6565
)
6666
]
6767

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

86-
with ensure_clean(ext) as f:
86+
with tm.ensure_clean(ext) as f:
8787
wb = openpyxl.Workbook()
8888
wb.worksheets[0].title = "foo"
8989
wb.worksheets[0]["A1"].value = "foo"

pandas/tests/io/excel/test_style.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
from pandas import DataFrame
5-
from pandas.util.testing import ensure_clean
5+
import pandas.util.testing as tm
66

77
from pandas.io.excel import ExcelWriter
88
from pandas.io.formats.excel import ExcelFormatter
@@ -70,7 +70,7 @@ def custom_converter(css):
7070
# Prepare spreadsheets
7171

7272
df = DataFrame(np.random.randn(11, 3))
73-
with ensure_clean(".xlsx" if engine != "xlwt" else ".xls") as path:
73+
with tm.ensure_clean(".xlsx" if engine != "xlwt" else ".xls") as path:
7474
writer = ExcelWriter(path, engine=engine)
7575
df.to_excel(writer, sheet_name="frame")
7676
df.style.to_excel(writer, sheet_name="unstyled")

pandas/tests/io/excel/test_writers.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import pandas as pd
1313
from pandas import DataFrame, Index, MultiIndex, get_option, set_option
1414
import pandas.util.testing as tm
15-
from pandas.util.testing import ensure_clean, makeCustomDataframe as mkdf
1615

1716
from pandas.io.excel import (
1817
ExcelFile,
@@ -29,7 +28,7 @@ def path(ext):
2928
"""
3029
Fixture to open file for use in each test case.
3130
"""
32-
with ensure_clean(ext) as file_path:
31+
with tm.ensure_clean(ext) as file_path:
3332
yield file_path
3433

3534

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

67-
with ensure_clean(ext) as path:
66+
with tm.ensure_clean(ext) as path:
6867
df.to_excel(path, filename, index=False, header=False)
6968
result = pd.read_excel(path, filename, usecols=[0], header=header)
7069

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

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

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

96-
with ensure_clean(ext) as pth:
95+
with tm.ensure_clean(ext) as pth:
9796
with ExcelWriter(pth) as writer:
9897
refdf.to_excel(writer, "Data_no_head", header=False, index=False)
9998
refdf.to_excel(writer, "Data_with_head", index=False)
@@ -127,7 +126,7 @@ def tdf(col_sheet_name):
127126
dfs = [tdf(s) for s in sheets]
128127
dfs = dict(zip(sheets, dfs))
129128

130-
with ensure_clean(ext) as pth:
129+
with tm.ensure_clean(ext) as pth:
131130
with ExcelWriter(pth) as ew:
132131
for sheetname, df in dfs.items():
133132
df.to_excel(ew, sheetname)
@@ -140,7 +139,7 @@ def tdf(col_sheet_name):
140139
@td.skip_if_no("xlsxwriter")
141140
def test_read_excel_multiindex_empty_level(self, ext):
142141
# see gh-12453
143-
with ensure_clean(ext) as path:
142+
with tm.ensure_clean(ext) as path:
144143
df = DataFrame(
145144
{
146145
("One", "x"): {0: 1},
@@ -194,7 +193,7 @@ def test_excel_multindex_roundtrip(
194193
self, ext, c_idx_names, r_idx_names, c_idx_levels, r_idx_levels
195194
):
196195
# see gh-4679
197-
with ensure_clean(ext) as pth:
196+
with tm.ensure_clean(ext) as pth:
198197
if c_idx_levels == 1 and c_idx_names:
199198
pytest.skip(
200199
"Column index name cannot be serialized unless it's a MultiIndex"
@@ -204,7 +203,9 @@ def test_excel_multindex_roundtrip(
204203
# unnamed levels, not Nones.
205204
check_names = r_idx_names or r_idx_levels <= 1
206205

207-
df = mkdf(5, 5, c_idx_names, r_idx_names, c_idx_levels, r_idx_levels)
206+
df = tm.makeCustomDataframe(
207+
5, 5, c_idx_names, r_idx_names, c_idx_levels, r_idx_levels
208+
)
208209
df.to_excel(pth)
209210

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

246-
with ensure_clean(ext) as pth:
247+
with tm.ensure_clean(ext) as pth:
247248
df2.to_excel(pth)
248249

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

584-
with ensure_clean(ext) as filename2:
585+
with tm.ensure_clean(ext) as filename2:
585586
writer1 = ExcelWriter(path)
586587
writer2 = ExcelWriter(
587588
filename2,
@@ -778,13 +779,13 @@ def test_to_excel_output_encoding(self, ext):
778779
columns=["X\u0193", "Y", "Z"],
779780
)
780781

781-
with ensure_clean("__tmp_to_excel_float_format__." + ext) as filename:
782+
with tm.ensure_clean("__tmp_to_excel_float_format__." + ext) as filename:
782783
df.to_excel(filename, sheet_name="TestSheet", encoding="utf8")
783784
result = pd.read_excel(filename, "TestSheet", encoding="utf8", index_col=0)
784785
tm.assert_frame_equal(result, df)
785786

786787
def test_to_excel_unicode_filename(self, ext, path):
787-
with ensure_clean("\u0192u." + ext) as filename:
788+
with tm.ensure_clean("\u0192u." + ext) as filename:
788789
try:
789790
f = open(filename, "wb")
790791
except UnicodeEncodeError:
@@ -932,12 +933,10 @@ def roundtrip(data, header=True, parser_hdr=0, index=True):
932933
nrows = 5
933934
ncols = 3
934935

935-
from pandas.util.testing import makeCustomDataframe as mkdf
936-
937936
# ensure limited functionality in 0.10
938937
# override of gh-2370 until sorted out in 0.11
939938

940-
df = mkdf(
939+
df = tm.makeCustomDataframe(
941940
nrows, ncols, r_idx_nlevels=r_idx_nlevels, c_idx_nlevels=c_idx_nlevels
942941
)
943942

@@ -1216,7 +1215,7 @@ class TestExcelWriterEngineTests:
12161215
],
12171216
)
12181217
def test_ExcelWriter_dispatch(self, klass, ext):
1219-
with ensure_clean(ext) as path:
1218+
with tm.ensure_clean(ext) as path:
12201219
writer = ExcelWriter(path)
12211220
if ext == ".xlsx" and td.safe_import("xlsxwriter"):
12221221
# xlsxwriter has preference over openpyxl if both installed

pandas/tests/io/excel/test_xlrd.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import pandas as pd
44
import pandas.util.testing as tm
5-
from pandas.util.testing import ensure_clean
65

76
from pandas.io.excel import ExcelFile
87

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

25-
with ensure_clean(read_ext) as pth:
24+
with tm.ensure_clean(read_ext) as pth:
2625
df.to_excel(pth, sheet_name)
2726
book = xlrd.open_workbook(pth)
2827

pandas/tests/io/excel/test_xlsxwriter.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
from pandas import DataFrame
6-
from pandas.util.testing import ensure_clean
6+
import pandas.util.testing as tm
77

88
from pandas.io.excel import ExcelWriter
99

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

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

2626
writer = ExcelWriter(path)
@@ -59,6 +59,6 @@ def test_column_format(ext):
5959
def test_write_append_mode_raises(ext):
6060
msg = "Append mode is not supported with xlsxwriter!"
6161

62-
with ensure_clean(ext) as f:
62+
with tm.ensure_clean(ext) as f:
6363
with pytest.raises(ValueError, match=msg):
6464
ExcelWriter(f, engine="xlsxwriter", mode="a")

pandas/tests/io/excel/test_xlwt.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pandas as pd
55
from pandas import DataFrame, MultiIndex
6-
from pandas.util.testing import ensure_clean
6+
import pandas.util.testing as tm
77

88
from pandas.io.excel import ExcelWriter, _XlwtWriter
99

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

2525

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

3434

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

4444

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

65-
with ensure_clean(ext) as f:
65+
with tm.ensure_clean(ext) as f:
6666
with pytest.raises(ValueError, match=msg):
6767
ExcelWriter(f, engine="xlwt", mode="a")

pandas/tests/io/json/test_compression.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import pandas as pd
66
import pandas.util.testing as tm
7-
from pandas.util.testing import assert_frame_equal
87

98

109
def test_compression_roundtrip(compression):
@@ -16,12 +15,12 @@ def test_compression_roundtrip(compression):
1615

1716
with tm.ensure_clean() as path:
1817
df.to_json(path, compression=compression)
19-
assert_frame_equal(df, pd.read_json(path, compression=compression))
18+
tm.assert_frame_equal(df, pd.read_json(path, compression=compression))
2019

2120
# explicitly ensure file was compressed.
2221
with tm.decompress_file(path, compression) as fh:
2322
result = fh.read().decode("utf8")
24-
assert_frame_equal(df, pd.read_json(result))
23+
tm.assert_frame_equal(df, pd.read_json(result))
2524

2625

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

34-
assert_frame_equal(uncompressed_df, compressed_df)
33+
tm.assert_frame_equal(uncompressed_df, compressed_df)
3534

3635

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

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

5150

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

6059

6160
def test_chunksize_with_compression(compression):
@@ -66,7 +65,7 @@ def test_chunksize_with_compression(compression):
6665

6766
res = pd.read_json(path, lines=True, chunksize=1, compression=compression)
6867
roundtripped_df = pd.concat(res)
69-
assert_frame_equal(df, roundtripped_df)
68+
tm.assert_frame_equal(df, roundtripped_df)
7069

7170

7271
def test_write_unsupported_compression_type():

0 commit comments

Comments
 (0)