Skip to content

CLN: Exception in core.dtypes #28387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 13, 2019
4 changes: 2 additions & 2 deletions pandas/core/arrays/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ def construct_from_string(cls, string):
if string.startswith("Sparse"):
try:
sub_type, has_fill_value = cls._parse_subtype(string)
result = SparseDtype(sub_type)
except Exception:
except ValueError:
raise TypeError(msg)
else:
result = SparseDtype(sub_type)
msg = (
"Could not construct SparseDtype from '{}'.\n\nIt "
"looks like the fill_value in the string is not "
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2049,10 +2049,8 @@ def pandas_dtype(dtype):
# raise a consistent TypeError if failed
try:
npdtype = np.dtype(dtype)
except Exception:
# we don't want to force a repr of the non-string
if not isinstance(dtype, str):
raise TypeError("data type not understood")
except SyntaxError:
# np.dtype uses `eval` which can raise SyntaxError
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can easily end up here with something that does not raise a syntaxError

But I assume the idea is that in that case the TypeError generated by numpy is fine to raise directly?

If so, can you add a comment for that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(based on your comment below, I suppose that is indeed the case)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorisvandenbossche I am not sure it matters; this will raise a TypeError if its invalid anyhow (catching the SyntaxError is just for hygiene of the error message)

raise TypeError("data type '{}' not understood".format(dtype))

# Any invalid dtype (such as pd.Timestamp) should raise an error.
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,7 @@ def convert(self, values, nan_rep, encoding, errors, start=None, stop=None):
# making an Index instance could throw a number of different errors
try:
self.values = Index(values, **kwargs)
except Exception: # noqa: E722
except Exception:

# if the output freq is different that what we recorded,
# it should be None (see also 'doc example part 2')
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ def test__get_dtype_sparse():
(None, "Cannot deduce dtype from null object"),
(1, "data type not understood"),
(1.2, "data type not understood"),
("random string", "data type 'random string' not understood"),
("random string", 'data type "random string" not understood'),
(pd.DataFrame([1, 2]), "data type not understood"),
],
)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/interval/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_astype_cannot_cast(self, index, dtype):
index.astype(dtype)

def test_astype_invalid_dtype(self, index):
msg = "data type 'fake_dtype' not understood"
msg = 'data type "fake_dtype" not understood'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to change the quoting here? I'm actually surprised this doesn't cause failures since the exception message shown in diff uses single quotes around the bad dtype

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now passing through an exception raised by numpy

with pytest.raises(TypeError, match=msg):
index.astype("fake_dtype")

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/interval/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_generic_errors(self, constructor):
constructor(dtype="int64", **filler)

# invalid dtype
msg = "data type 'invalid' not understood"
msg = 'data type "invalid" not understood'
with pytest.raises(TypeError, match=msg):
constructor(dtype="invalid", **filler)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/parser/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_invalid_dtype_per_column(all_parsers):
3,4.5
4,5.5"""

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


Expand Down