diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index a7c9c86b3d9a5..36e8e9d2264f5 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -19,6 +19,9 @@ from pandas.compat import ( IS64, + PY310, + PY311, + is_numpy_dev, is_platform_windows, ) import pandas.util._test_decorators as td @@ -3384,10 +3387,23 @@ def test_filepath_or_buffer_arg( ): getattr(df, method)(buf=filepath_or_buffer, encoding=encoding) elif encoding == "foo": - expected_warning = FutureWarning if method == "to_latex" else None - with tm.assert_produces_warning(expected_warning): - with pytest.raises(LookupError, match="unknown encoding"): - getattr(df, method)(buf=filepath_or_buffer, encoding=encoding) + if ( + method == "to_string" + and data == "abc" + and filepath_or_buffer_id == "string" + and PY310 + and not PY311 + and not is_numpy_dev + ): + # raises importwarning _SixMetaPathImporter.find_spec + with tm.assert_produces_warning(ImportWarning, check_stacklevel=False): + with pytest.raises(LookupError, match="unknown encoding"): + getattr(df, method)(buf=filepath_or_buffer, encoding=encoding) + else: + expected_warning = FutureWarning if method == "to_latex" else None + with tm.assert_produces_warning(expected_warning): + with pytest.raises(LookupError, match="unknown encoding"): + getattr(df, method)(buf=filepath_or_buffer, encoding=encoding) else: expected = getattr(df, method)() getattr(df, method)(buf=filepath_or_buffer, encoding=encoding)