Skip to content

dataframe.astype() exception message to include column name #48321

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 12 commits into from
Oct 14, 2022
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Other enhancements
- Added ``index`` parameter to :meth:`DataFrame.to_dict` (:issue:`46398`)
- Added metadata propagation for binary operators on :class:`DataFrame` (:issue:`28283`)
- :class:`.CategoricalConversionWarning`, :class:`.InvalidComparison`, :class:`.InvalidVersion`, :class:`.LossySetitemError`, and :class:`.NoBufferPresent` are now exposed in ``pandas.errors`` (:issue:`27656`)
-
- :func:`DataFrame.astype` exception message thrown improved to include column name when type conversion is not possible. (:issue:`47571`)

.. ---------------------------------------------------------------------------
.. _whatsnew_200.notable_bug_fixes:
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6264,7 +6264,13 @@ def astype(
if isna(cdt):
res_col = col.copy() if copy else col
else:
res_col = col.astype(dtype=cdt, copy=copy, errors=errors)
try:
res_col = col.astype(dtype=cdt, copy=copy, errors=errors)
except ValueError as ex:
ex.args = (
f"{ex}: Error while type casting for column '{col_name}'",
)
raise
results.append(res_col)

elif is_extension_array_dtype(dtype) and self.ndim > 1:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,18 @@ def test_astype_arg_for_errors(self):

df.astype(np.int8, errors="ignore")

def test_astype_invalid_conversion(self):
# GH#47571
df = DataFrame({"a": [1, 2, "text"], "b": [1, 2, 3]})

msg = (
"invalid literal for int() with base 10: 'text': "
"Error while type casting for column 'a'"
)

with pytest.raises(ValueError, match=re.escape(msg)):
df.astype({"a": int})

def test_astype_arg_for_errors_dictlist(self):
# GH#25905
df = DataFrame(
Expand Down