Skip to content

Commit 744b846

Browse files
authored
ENH: move an exception and add a prehook to check for exception place… (#48088)
* ENH: move an exception and add a prehook to check for exception placement * ENH: fix import * ENH: revert moving error * ENH: add docstring and fix import for test * ENH: re-design approach based on feedback * ENH: update whatsnew rst * ENH: apply feedback changes * ENH: refactor to remove exception_warning_list and ignore _version.py * ENH: remove NotThisMethod from tests and all
1 parent 3937fbe commit 744b846

File tree

12 files changed

+233
-45
lines changed

12 files changed

+233
-45
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ repos:
236236
entry: python scripts/validate_min_versions_in_sync.py
237237
language: python
238238
files: ^(ci/deps/actions-.*-minimum_versions\.yaml|pandas/compat/_optional\.py)$
239+
- id: validate-errors-locations
240+
name: Validate errors locations
241+
description: Validate errors are in approriate locations.
242+
entry: python scripts/validate_exception_location.py
243+
language: python
244+
files: ^pandas/
245+
exclude: ^(pandas/_libs/|pandas/tests/|pandas/errors/__init__.py$|pandas/_version.py)
246+
types: [python]
239247
- id: flake8-pyi
240248
name: flake8-pyi
241249
entry: flake8 --extend-ignore=E301,E302,E305,E701,E704

doc/source/reference/testing.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ Exceptions and warnings
3838
errors.IncompatibilityWarning
3939
errors.IndexingError
4040
errors.InvalidColumnName
41+
errors.InvalidComparison
4142
errors.InvalidIndexError
43+
errors.InvalidVersion
4244
errors.IntCastingNaNError
45+
errors.LossySetitemError
4346
errors.MergeError
47+
errors.NoBufferPresent
4448
errors.NullFrequencyError
4549
errors.NumbaUtilError
4650
errors.NumExprClobberingError

doc/source/whatsnew/v1.6.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Other enhancements
3434
- :func:`assert_frame_equal` now shows the first element where the DataFrames differ, analogously to ``pytest``'s output (:issue:`47910`)
3535
- Added ``index`` parameter to :meth:`DataFrame.to_dict` (:issue:`46398`)
3636
- Added metadata propagation for binary operators on :class:`DataFrame` (:issue:`28283`)
37+
- :class:`.CategoricalConversionWarning`, :class:`.InvalidComparison`, :class:`.InvalidVersion`, :class:`.LossySetitemError`, and :class:`.NoBufferPresent` are now exposed in ``pandas.errors`` (:issue:`27656`)
3738
-
3839

3940
.. ---------------------------------------------------------------------------

pandas/core/arrays/datetimelike.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
from pandas.compat.numpy import function as nv
6767
from pandas.errors import (
6868
AbstractMethodError,
69+
InvalidComparison,
6970
NullFrequencyError,
7071
PerformanceWarning,
7172
)
@@ -154,15 +155,6 @@
154155
DatetimeLikeArrayT = TypeVar("DatetimeLikeArrayT", bound="DatetimeLikeArrayMixin")
155156

156157

157-
class InvalidComparison(Exception):
158-
"""
159-
Raised by _validate_comparison_value to indicate to caller it should
160-
return invalid_comparison.
161-
"""
162-
163-
pass
164-
165-
166158
class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray):
167159
"""
168160
Shared Base/Mixin class for DatetimeArray, TimedeltaArray, PeriodArray

pandas/core/dtypes/cast.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040
DtypeObj,
4141
Scalar,
4242
)
43-
from pandas.errors import IntCastingNaNError
43+
from pandas.errors import (
44+
IntCastingNaNError,
45+
LossySetitemError,
46+
)
4447
from pandas.util._exceptions import find_stack_level
4548
from pandas.util._validators import validate_bool_kwarg
4649

@@ -2103,11 +2106,3 @@ def _dtype_can_hold_range(rng: range, dtype: np.dtype) -> bool:
21032106
if not len(rng):
21042107
return True
21052108
return np.can_cast(rng[0], dtype) and np.can_cast(rng[-1], dtype)
2106-
2107-
2108-
class LossySetitemError(Exception):
2109-
"""
2110-
Raised when trying to do a __setitem__ on an np.ndarray that is not lossless.
2111-
"""
2112-
2113-
pass

pandas/core/interchange/column.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from pandas._libs.lib import infer_dtype
88
from pandas._libs.tslibs import iNaT
9+
from pandas.errors import NoBufferPresent
910
from pandas.util._decorators import cache_readonly
1011

1112
import pandas as pd
@@ -23,7 +24,6 @@
2324
from pandas.core.interchange.utils import (
2425
ArrowCTypes,
2526
Endianness,
26-
NoBufferPresent,
2727
dtype_to_arrow_c_fmt,
2828
)
2929

pandas/core/interchange/utils.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,3 @@ def dtype_to_arrow_c_fmt(dtype: DtypeObj) -> str:
8989
raise NotImplementedError(
9090
f"Conversion of {dtype} to Arrow C format string is not implemented."
9191
)
92-
93-
94-
class NoBufferPresent(Exception):
95-
"""Exception to signal that there is no requested buffer."""

pandas/errors/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
OutOfBoundsTimedelta,
1313
)
1414

15+
from pandas.util.version import InvalidVersion
16+
1517

1618
class IntCastingNaNError(ValueError):
1719
"""
@@ -535,6 +537,24 @@ class CategoricalConversionWarning(Warning):
535537
"""
536538

537539

540+
class LossySetitemError(Exception):
541+
"""
542+
Raised when trying to do a __setitem__ on an np.ndarray that is not lossless.
543+
"""
544+
545+
546+
class NoBufferPresent(Exception):
547+
"""
548+
Exception is raised in _get_data_buffer to signal that there is no requested buffer.
549+
"""
550+
551+
552+
class InvalidComparison(Exception):
553+
"""
554+
Exception is raised by _validate_comparison_value to indicate an invalid comparison.
555+
"""
556+
557+
538558
__all__ = [
539559
"AbstractMethodError",
540560
"AccessorRegistrationWarning",
@@ -550,9 +570,13 @@ class CategoricalConversionWarning(Warning):
550570
"IncompatibilityWarning",
551571
"IntCastingNaNError",
552572
"InvalidColumnName",
573+
"InvalidComparison",
553574
"InvalidIndexError",
575+
"InvalidVersion",
554576
"IndexingError",
577+
"LossySetitemError",
555578
"MergeError",
579+
"NoBufferPresent",
556580
"NullFrequencyError",
557581
"NumbaUtilError",
558582
"NumExprClobberingError",

pandas/tests/test_errors.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,37 @@
1111
@pytest.mark.parametrize(
1212
"exc",
1313
[
14-
"UnsupportedFunctionCall",
15-
"UnsortedIndexError",
16-
"OutOfBoundsDatetime",
17-
"ParserError",
18-
"PerformanceWarning",
14+
"AttributeConflictWarning",
15+
"CSSWarning",
16+
"CategoricalConversionWarning",
17+
"ClosedFileError",
18+
"DataError",
19+
"DatabaseError",
1920
"DtypeWarning",
2021
"EmptyDataError",
21-
"ParserWarning",
22+
"IncompatibilityWarning",
23+
"IndexingError",
24+
"InvalidColumnName",
25+
"InvalidComparison",
26+
"InvalidVersion",
27+
"LossySetitemError",
2228
"MergeError",
23-
"OptionError",
24-
"NumbaUtilError",
25-
"DataError",
26-
"SpecificationError",
27-
"SettingWithCopyError",
28-
"SettingWithCopyWarning",
29+
"NoBufferPresent",
2930
"NumExprClobberingError",
30-
"IndexingError",
31-
"PyperclipException",
32-
"CSSWarning",
33-
"ClosedFileError",
31+
"NumbaUtilError",
32+
"OptionError",
33+
"OutOfBoundsDatetime",
34+
"ParserError",
35+
"ParserWarning",
36+
"PerformanceWarning",
3437
"PossibleDataLossError",
35-
"IncompatibilityWarning",
36-
"AttributeConflictWarning",
37-
"DatabaseError",
3838
"PossiblePrecisionLoss",
39-
"CategoricalConversionWarning",
40-
"InvalidColumnName",
39+
"PyperclipException",
40+
"SettingWithCopyError",
41+
"SettingWithCopyWarning",
42+
"SpecificationError",
43+
"UnsortedIndexError",
44+
"UnsupportedFunctionCall",
4145
"ValueLabelTypeMismatch",
4246
],
4347
)

scripts/pandas_errors_documented.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Check that doc/source/reference/general_utility_functions.rst documents
2+
Check that doc/source/reference/testing.rst documents
33
all exceptions and warnings in pandas/errors/__init__.py.
44
55
This is meant to be run as a pre-commit hook - to run it manually, you can do:

0 commit comments

Comments
 (0)