Skip to content

Commit 07dc9a2

Browse files
committed
TST (string dtype): clean up construction of expected string arrays (pandas-dev#59481)
1 parent ee701c2 commit 07dc9a2

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

pandas/tests/io/excel/test_readers.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -659,31 +659,34 @@ def test_dtype_backend_and_dtype(self, read_ext):
659659
@pytest.mark.xfail(
660660
using_string_dtype(), reason="infer_string takes precedence", strict=False
661661
)
662-
def test_dtype_backend_string(self, read_ext, string_storage, tmp_excel):
662+
def test_dtype_backend_string(self, read_ext, string_storage):
663663
# GH#36712
664664
if read_ext in (".xlsb", ".xls"):
665665
pytest.skip(f"No engine for filetype: '{read_ext}'")
666666

667-
df = DataFrame(
668-
{
669-
"a": np.array(["a", "b"], dtype=np.object_),
670-
"b": np.array(["x", pd.NA], dtype=np.object_),
671-
}
672-
)
673-
df.to_excel(tmp_excel, sheet_name="test", index=False)
674-
675667
with pd.option_context("mode.string_storage", string_storage):
676-
result = pd.read_excel(
677-
tmp_excel, sheet_name="test", dtype_backend="numpy_nullable"
668+
df = DataFrame(
669+
{
670+
"a": np.array(["a", "b"], dtype=np.object_),
671+
"b": np.array(["x", pd.NA], dtype=np.object_),
672+
}
678673
)
679674

680-
expected = DataFrame(
681-
{
682-
"a": Series(["a", "b"], dtype=pd.StringDtype(string_storage)),
683-
"b": Series(["x", None], dtype=pd.StringDtype(string_storage)),
684-
}
685-
)
686-
tm.assert_frame_equal(result, expected)
675+
with tm.ensure_clean(read_ext) as file_path:
676+
df.to_excel(file_path, sheet_name="test", index=False)
677+
result = pd.read_excel(
678+
file_path, sheet_name="test", dtype_backend="numpy_nullable"
679+
)
680+
681+
expected = DataFrame(
682+
{
683+
"a": Series(["a", "b"], dtype=pd.StringDtype(string_storage)),
684+
"b": Series(["x", None], dtype=pd.StringDtype(string_storage)),
685+
}
686+
)
687+
# the storage of the str columns' Index is also affected by the
688+
# string_storage setting -> ignore that for checking the result
689+
tm.assert_frame_equal(result, expected, check_column_type=False)
687690

688691
@pytest.mark.parametrize("dtypes, exp_value", [({}, 1), ({"a.1": "int64"}, 1)])
689692
def test_dtype_mangle_dup_cols(self, read_ext, dtypes, exp_value):

0 commit comments

Comments
 (0)