Skip to content

STYLE: prohibit parsing exeption messages #50408

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

Closed
8 changes: 8 additions & 0 deletions doc/source/reference/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,26 @@ Exceptions and warnings
errors.CSSWarning
errors.DatabaseError
errors.DataError
errors.DataOneDimensionalError
errors.DiffArrayLengthError
errors.DotMismatchShapeError
errors.DtypeWarning
errors.DuplicateLabelError
errors.EmptyDataError
errors.IncompatibilityWarning
errors.IndexingError
errors.IndexSpecifiedError
errors.InvalidColumnName
errors.InvalidComparison
errors.InvalidIndexError
errors.InvalidVersion
errors.IntCastingNaNError
errors.LossySetitemError
errors.MergeError
errors.NATypeError
errors.NoBufferPresent
errors.NoObjectConcatenateError
errors.NotObjectError
errors.NullFrequencyError
errors.NumbaUtilError
errors.NumExprClobberingError
Expand All @@ -61,6 +68,7 @@ Exceptions and warnings
errors.SettingWithCopyError
errors.SettingWithCopyWarning
errors.SpecificationError
errors.SupplyTzDetypeError
errors.UndefinedVariableError
errors.UnsortedIndexError
errors.UnsupportedFunctionCall
Expand Down
4 changes: 3 additions & 1 deletion pandas/_libs/missing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from sys import maxsize

cimport cython
from cython cimport Py_ssize_t

import numpy as np

cimport numpy as cnp
Expand All @@ -30,6 +31,7 @@ from pandas._libs.tslibs.np_datetime cimport (
)

from pandas._libs.ops_dispatch import maybe_dispatch_ufunc_to_dunder_op
from pandas.errors import NATypeError

cdef:
float64_t INF = <float64_t>np.inf
Expand Down Expand Up @@ -410,7 +412,7 @@ class NAType(C_NAType):
return self.__repr__()

def __bool__(self):
raise TypeError("boolean value of NA is ambiguous")
raise NATypeError("boolean value of NA is ambiguous")

def __hash__(self):
# GH 30013: Ensure hash is large enough to avoid hash collisions with integers
Expand Down
5 changes: 3 additions & 2 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ cnp.import_array()

from pandas._libs.util cimport is_array

from pandas.errors import NotObjectError


cdef cnp.dtype _dtype_obj = np.dtype("object")

Expand All @@ -18,8 +20,7 @@ cpdef check_result_array(object obj, object dtype):
if dtype != _dtype_obj:
# If it is object dtype, the function can be a reduction/aggregation
# and still return an ndarray e.g. test_agg_over_numpy_arrays
raise ValueError("Must produce aggregated value")

raise NotObjectError("Must produce aggregated value")

cpdef inline extract_result(object res):
""" extract the result object, it might be a 0-dim ndarray
Expand Down
22 changes: 12 additions & 10 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
NDFrameT,
npt,
)
from pandas.errors import SpecificationError
from pandas.errors import (
DiffArrayLengthError,
SpecificationError,
)
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.cast import is_nested_object
Expand Down Expand Up @@ -864,15 +867,14 @@ def wrap_results_for_axis(

try:
result = self.obj._constructor(data=results)
except ValueError as err:
if "All arrays must be of the same length" in str(err):
# e.g. result = [[2, 3], [1.5], ['foo', 'bar']]
# see test_agg_listlike_result GH#29587
res = self.obj._constructor_sliced(results)
res.index = res_index
return res
else:
raise
except DiffArrayLengthError:
# e.g. result = [[2, 3], [1.5], ['foo', 'bar']]
# see test_agg_listlike_result GH#29587
res = self.obj._constructor_sliced(results)
res.index = res_index
return res
except ValueError:
raise

if not isinstance(results[0], ABCSeries):
if len(result.index) == len(self.res_columns):
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@
TimeNonexistent,
npt,
)
from pandas.errors import PerformanceWarning
from pandas.errors import (
PerformanceWarning,
SupplyTzDetypeError,
)
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import validate_inclusive

Expand Down Expand Up @@ -2367,9 +2370,8 @@ def _validate_tz_from_dtype(
# We also need to check for the case where the user passed a
# tz-naive dtype (i.e. datetime64[ns])
if tz is not None and not timezones.tz_compare(tz, dtz):
raise ValueError(
"cannot supply both a tz and a "
"timezone-naive dtype (i.e. datetime64[ns])"
raise SupplyTzDetypeError(
"cannot supply both a tz and a timezone-naive dtype"
)

return tz
Expand Down
10 changes: 8 additions & 2 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
DtypeObj,
T,
)
from pandas.errors import (
DataOneDimensionalError,
IndexSpecifiedError,
)

from pandas.core.dtypes.base import (
ExtensionDtype,
Expand Down Expand Up @@ -540,7 +544,9 @@ def sanitize_array(

if not is_list_like(data):
if index is None:
raise ValueError("index must be specified when data is not list-like")
raise IndexSpecifiedError(
"index must be specified when data is not list-like"
)
data = construct_1d_arraylike_from_scalar(data, len(index), dtype)
return data

Expand Down Expand Up @@ -666,7 +672,7 @@ def _sanitize_ndim(
if isinstance(data, np.ndarray):
if allow_2d:
return result
raise ValueError("Data must be 1-dimensional")
raise DataOneDimensionalError("Data must be 1-dimensional")
if is_object_dtype(dtype) and isinstance(dtype, ExtensionDtype):
# i.e. PandasDtype("O")

Expand Down
18 changes: 10 additions & 8 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from pandas.errors import (
IntCastingNaNError,
LossySetitemError,
SupplyTzDetypeError,
)

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -1177,14 +1178,15 @@ def maybe_cast_to_datetime(
else:
try:
dta = DatetimeArray._from_sequence(value, dtype=dtype)
except ValueError as err:
# We can give a Series-specific exception message.
if "cannot supply both a tz and a timezone-naive dtype" in str(err):
raise ValueError(
"Cannot convert timezone-aware data to "
"timezone-naive dtype. Use "
"pd.Series(values).dt.tz_localize(None) instead."
) from err

except SupplyTzDetypeError as err:
raise ValueError(
"Cannot convert timezone-aware data to "
"timezone-naive dtype. Use "
"pd.Series(values).dt.tz_localize(None) instead."
) from err

except ValueError:
raise

return dta
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
NaT,
iNaT,
)
from pandas.errors import NATypeError

from pandas.core.dtypes.common import (
DT64NS_DTYPE,
Expand Down Expand Up @@ -580,9 +581,9 @@ def _array_equivalent_object(left: np.ndarray, right: np.ndarray, strict_nan: bo
try:
if np.any(np.asarray(left_value != right_value)):
return False
except TypeError as err:
if "boolean value of NA is ambiguous" in str(err):
return False
except NATypeError:
return False
except TypeError:
raise
return True

Expand Down
13 changes: 8 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@
function as nv,
np_percentile_argname,
)
from pandas.errors import InvalidIndexError
from pandas.errors import (
DotMismatchShapeError,
InvalidIndexError,
)
from pandas.util._decorators import (
Appender,
Substitution,
Expand Down Expand Up @@ -1570,7 +1573,7 @@ def dot(self, other: AnyArrayLike | DataFrame) -> DataFrame | Series:
lvals = self.values
rvals = np.asarray(other)
if lvals.shape[1] != rvals.shape[0]:
raise ValueError(
raise DotMismatchShapeError(
f"Dot product shape mismatch, {lvals.shape} vs {rvals.shape}"
)

Expand Down Expand Up @@ -1609,12 +1612,12 @@ def __rmatmul__(self, other) -> DataFrame:
"""
try:
return self.T.dot(np.transpose(other)).T
except ValueError as err:
if "shape mismatch" not in str(err):
raise
except DotMismatchShapeError as err:
# GH#21581 give exception message for original shapes
msg = f"shapes {np.shape(other)} and {self.shape} not aligned"
raise ValueError(msg) from err
except ValueError:
raise

# ----------------------------------------------------------------------
# IO methods (to / from other formats)
Expand Down
13 changes: 7 additions & 6 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
SingleManager,
TakeIndexer,
)
from pandas.errors import SpecificationError
from pandas.errors import (
NoObjectConcatenateError,
SpecificationError,
)
from pandas.util._decorators import (
Appender,
Substitution,
Expand Down Expand Up @@ -1177,12 +1180,10 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
gba = GroupByApply(self, [func], args=(), kwargs={})
try:
result = gba.agg()

except ValueError as err:
if "No objects to concatenate" not in str(err):
raise
except NoObjectConcatenateError:
result = self._aggregate_frame(func)

except ValueError:
raise
else:
sobj = self._selected_obj

Expand Down
12 changes: 7 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
)
from pandas.compat.numpy import function as nv
from pandas.errors import (
DataOneDimensionalError,
DuplicateLabelError,
IndexSpecifiedError,
InvalidIndexError,
)
from pandas.util._decorators import (
Expand Down Expand Up @@ -500,11 +502,11 @@ def __new__(

try:
arr = sanitize_array(data, None, dtype=dtype, copy=copy)
except ValueError as err:
if "index must be specified when data is not list-like" in str(err):
raise cls._raise_scalar_data_error(data) from err
if "Data must be 1-dimensional" in str(err):
raise ValueError("Index data must be 1-dimensional") from err
except IndexSpecifiedError as err:
raise cls._raise_scalar_data_error(data) from err
except DataOneDimensionalError as err:
raise ValueError("Index data must be 1-dimensional") from err
except ValueError:
raise
arr = ensure_wrapped_if_datetimelike(arr)

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Manager,
npt,
)
from pandas.errors import DiffArrayLengthError

from pandas.core.dtypes.cast import (
construct_1d_arraylike_from_scalar,
Expand Down Expand Up @@ -623,7 +624,7 @@ def _extract_index(data) -> Index:
if have_raw_arrays:
lengths = list(set(raw_lengths))
if len(lengths) > 1:
raise ValueError("All arrays must be of the same length")
raise DiffArrayLengthError("All arrays must be of the same length")

if have_dicts:
raise ValueError(
Expand Down
17 changes: 7 additions & 10 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from pandas.errors import (
AbstractMethodError,
DataError,
NotObjectError,
)
from pandas.util._decorators import (
Appender,
Expand Down Expand Up @@ -459,18 +460,14 @@ def _groupby_and_aggregate(self, how, *args, **kwargs):
# on Series, raising AttributeError or KeyError
# (depending on whether the column lookup uses getattr/__getitem__)
result = grouped.apply(how, *args, **kwargs)
except NotObjectError:
# we have a non-reducing function; try to evaluate

except ValueError as err:
if "Must produce aggregated value" in str(err):
# raised in _aggregate_named
# see test_apply_without_aggregation, test_apply_with_mutated_index
pass
else:
raise

# we have a non-reducing function
# try to evaluate
# raised in _aggregate_named
# see test_apply_without_aggregation, test_apply_with_mutated_index
result = grouped.apply(how, *args, **kwargs)
except ValueError:
raise

return self._wrap_result(result)

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
AxisInt,
HashableT,
)
from pandas.errors import NoObjectConcatenateError
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.concat import concat_compat
Expand Down Expand Up @@ -420,7 +421,7 @@ def __init__(
objs = list(objs)

if len(objs) == 0:
raise ValueError("No objects to concatenate")
raise NoObjectConcatenateError("No objects to concatenate")

if keys is None:
objs = list(com.not_none(*objs))
Expand Down
Loading