Skip to content

Commit 330bede

Browse files
jbrockmendelWillAyd
authored andcommitted
CLN: Exception in core.dtypes (#28387)
1 parent 9dc6de3 commit 330bede

File tree

7 files changed

+9
-11
lines changed

7 files changed

+9
-11
lines changed

pandas/core/arrays/sparse.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ def construct_from_string(cls, string):
245245
if string.startswith("Sparse"):
246246
try:
247247
sub_type, has_fill_value = cls._parse_subtype(string)
248-
result = SparseDtype(sub_type)
249-
except Exception:
248+
except ValueError:
250249
raise TypeError(msg)
251250
else:
251+
result = SparseDtype(sub_type)
252252
msg = (
253253
"Could not construct SparseDtype from '{}'.\n\nIt "
254254
"looks like the fill_value in the string is not "

pandas/core/dtypes/common.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2049,10 +2049,8 @@ def pandas_dtype(dtype):
20492049
# raise a consistent TypeError if failed
20502050
try:
20512051
npdtype = np.dtype(dtype)
2052-
except Exception:
2053-
# we don't want to force a repr of the non-string
2054-
if not isinstance(dtype, str):
2055-
raise TypeError("data type not understood")
2052+
except SyntaxError:
2053+
# np.dtype uses `eval` which can raise SyntaxError
20562054
raise TypeError("data type '{}' not understood".format(dtype))
20572055

20582056
# Any invalid dtype (such as pd.Timestamp) should raise an error.

pandas/io/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,7 @@ def convert(self, values, nan_rep, encoding, errors, start=None, stop=None):
17911791
# making an Index instance could throw a number of different errors
17921792
try:
17931793
self.values = Index(values, **kwargs)
1794-
except Exception: # noqa: E722
1794+
except Exception:
17951795

17961796
# if the output freq is different that what we recorded,
17971797
# it should be None (see also 'doc example part 2')

pandas/tests/dtypes/test_common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def test__get_dtype_sparse():
678678
(None, "Cannot deduce dtype from null object"),
679679
(1, "data type not understood"),
680680
(1.2, "data type not understood"),
681-
("random string", "data type 'random string' not understood"),
681+
("random string", 'data type "random string" not understood'),
682682
(pd.DataFrame([1, 2]), "data type not understood"),
683683
],
684684
)

pandas/tests/indexes/interval/test_astype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_astype_cannot_cast(self, index, dtype):
6767
index.astype(dtype)
6868

6969
def test_astype_invalid_dtype(self, index):
70-
msg = "data type 'fake_dtype' not understood"
70+
msg = 'data type "fake_dtype" not understood'
7171
with pytest.raises(TypeError, match=msg):
7272
index.astype("fake_dtype")
7373

pandas/tests/indexes/interval/test_construction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_generic_errors(self, constructor):
164164
constructor(dtype="int64", **filler)
165165

166166
# invalid dtype
167-
msg = "data type 'invalid' not understood"
167+
msg = 'data type "invalid" not understood'
168168
with pytest.raises(TypeError, match=msg):
169169
constructor(dtype="invalid", **filler)
170170

pandas/tests/io/parser/test_dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_invalid_dtype_per_column(all_parsers):
7979
3,4.5
8080
4,5.5"""
8181

82-
with pytest.raises(TypeError, match="data type 'foo' not understood"):
82+
with pytest.raises(TypeError, match='data type "foo" not understood'):
8383
parser.read_csv(StringIO(data), dtype={"one": "foo", 1: "int"})
8484

8585

0 commit comments

Comments
 (0)