Skip to content

Commit 2b82159

Browse files
gfyoungjreback
authored andcommitted
MAINT: Use context manager in test_c_parser_only (#24512)
Specifically, use context manager for pytest.raises. xref gh-24332.
1 parent 52e9b11 commit 2b82159

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

pandas/tests/io/parser/test_c_parser_only.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,33 @@ def test_dtype_and_names_error(c_parser_only):
111111
names=["a", "b"], dtype={"a": np.int32})
112112

113113

114-
def test_unsupported_dtype(c_parser_only):
114+
@pytest.mark.parametrize("match,kwargs", [
115+
# For each of these cases, all of the dtypes are valid, just unsupported.
116+
(("the dtype datetime64 is not supported for parsing, "
117+
"pass this column using parse_dates instead"),
118+
dict(dtype={"A": "datetime64", "B": "float64"})),
119+
120+
(("the dtype datetime64 is not supported for parsing, "
121+
"pass this column using parse_dates instead"),
122+
dict(dtype={"A": "datetime64", "B": "float64"},
123+
parse_dates=["B"])),
124+
125+
("the dtype timedelta64 is not supported for parsing",
126+
dict(dtype={"A": "timedelta64", "B": "float64"})),
127+
128+
("the dtype <U8 is not supported for parsing",
129+
dict(dtype={"A": "U8"}))
130+
], ids=["dt64-0", "dt64-1", "td64", "<U8"])
131+
def test_unsupported_dtype(c_parser_only, match, kwargs):
115132
parser = c_parser_only
116133
df = DataFrame(np.random.rand(5, 2), columns=list(
117134
"AB"), index=["1A", "1B", "1C", "1D", "1E"])
118135

119136
with tm.ensure_clean("__unsupported_dtype__.csv") as path:
120137
df.to_csv(path)
121138

122-
# valid but we don"t support it (date)
123-
pytest.raises(TypeError, parser.read_csv, path,
124-
dtype={"A": "datetime64", "B": "float64"},
125-
index_col=0)
126-
pytest.raises(TypeError, parser.read_csv, path,
127-
dtype={"A": "datetime64", "B": "float64"},
128-
index_col=0, parse_dates=["B"])
129-
130-
# valid but we don"t support it
131-
pytest.raises(TypeError, parser.read_csv, path,
132-
dtype={"A": "timedelta64", "B": "float64"},
133-
index_col=0)
134-
135-
# valid but unsupported - fixed width unicode string
136-
pytest.raises(TypeError, parser.read_csv, path,
137-
dtype={"A": "U8"},
138-
index_col=0)
139+
with pytest.raises(TypeError, match=match):
140+
parser.read_csv(path, index_col=0, **kwargs)
139141

140142

141143
@td.skip_if_32bit

0 commit comments

Comments
 (0)