Skip to content

Commit 321769a

Browse files
dataxerikyehoshuadimarsky
authored andcommitted
ENH: Move IndexingError to error/__init__.py per GH27656 (pandas-dev#47357)
1 parent dacf0cd commit 321769a

File tree

11 files changed

+37
-24
lines changed

11 files changed

+37
-24
lines changed

doc/source/reference/testing.rst

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Exceptions and warnings
3030
errors.DtypeWarning
3131
errors.DuplicateLabelError
3232
errors.EmptyDataError
33+
errors.IndexingError
3334
errors.InvalidIndexError
3435
errors.IntCastingNaNError
3536
errors.MergeError

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Other enhancements
174174
- A :class:`errors.PerformanceWarning` is now thrown when using ``string[pyarrow]`` dtype with methods that don't dispatch to ``pyarrow.compute`` methods (:issue:`42613`)
175175
- Added ``numeric_only`` argument to :meth:`Resampler.sum`, :meth:`Resampler.prod`, :meth:`Resampler.min`, :meth:`Resampler.max`, :meth:`Resampler.first`, and :meth:`Resampler.last` (:issue:`46442`)
176176
- ``times`` argument in :class:`.ExponentialMovingWindow` now accepts ``np.timedelta64`` (:issue:`47003`)
177-
- :class:`DataError`, :class:`SpecificationError`, :class:`SettingWithCopyError`, :class:`SettingWithCopyWarning`, :class:`NumExprClobberingError`, :class:`UndefinedVariableError` are now exposed in ``pandas.errors`` (:issue:`27656`)
177+
- :class:`DataError`, :class:`SpecificationError`, :class:`SettingWithCopyError`, :class:`SettingWithCopyWarning`, :class:`NumExprClobberingError`, :class:`UndefinedVariableError`, and :class:`IndexingError` are now exposed in ``pandas.errors`` (:issue:`27656`)
178178
- Added ``check_like`` argument to :func:`testing.assert_series_equal` (:issue:`47247`)
179179

180180
.. ---------------------------------------------------------------------------

pandas/core/indexing.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pandas._libs.lib import item_from_zerodim
1717
from pandas.errors import (
1818
AbstractMethodError,
19+
IndexingError,
1920
InvalidIndexError,
2021
)
2122
from pandas.util._decorators import doc
@@ -121,10 +122,6 @@ def __getitem__(self, arg):
121122
IndexSlice = _IndexSlice()
122123

123124

124-
class IndexingError(Exception):
125-
pass
126-
127-
128125
class IndexingMixin:
129126
"""
130127
Mixin for adding .loc/.iloc/.at/.iat to Dataframes and Series.

pandas/errors/__init__.py

+21
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,24 @@ def __init__(self, name: str, is_local: bool | None = None) -> None:
353353
else:
354354
msg = f"name {base_msg}"
355355
super().__init__(msg)
356+
357+
358+
class IndexingError(Exception):
359+
"""
360+
Exception is raised when trying to index and there is a mismatch in dimensions.
361+
362+
Examples
363+
--------
364+
>>> df = pd.DataFrame({'A': [1, 1, 1]})
365+
>>> df.loc[..., ..., 'A'] # doctest: +SKIP
366+
... # IndexingError: indexer may only contain one '...' entry
367+
>>> df = pd.DataFrame({'A': [1, 1, 1]})
368+
>>> df.loc[1, ..., ...] # doctest: +SKIP
369+
... # IndexingError: Too many indexers
370+
>>> df[pd.Series([True], dtype=bool)] # doctest: +SKIP
371+
... # IndexingError: Unalignable boolean Series provided as indexer...
372+
>>> s = pd.Series(range(2),
373+
... index = pd.MultiIndex.from_product([["a", "b"], ["c"]]))
374+
>>> s.loc["a", "c", "d"] # doctest: +SKIP
375+
... # IndexingError: Too many indexers
376+
"""

pandas/tests/frame/test_query_eval.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import pytest
55

6+
from pandas.errors import UndefinedVariableError
67
import pandas.util._test_decorators as td
78

89
import pandas as pd
@@ -495,8 +496,6 @@ def test_query_syntax_error(self):
495496
df.query("i - +", engine=engine, parser=parser)
496497

497498
def test_query_scope(self):
498-
from pandas.errors import UndefinedVariableError
499-
500499
engine, parser = self.engine, self.parser
501500
skip_if_no_pandas_parser(parser)
502501

@@ -522,8 +521,6 @@ def test_query_scope(self):
522521
df.query("@a > b > c", engine=engine, parser=parser)
523522

524523
def test_query_doesnt_pickup_local(self):
525-
from pandas.errors import UndefinedVariableError
526-
527524
engine, parser = self.engine, self.parser
528525
n = m = 10
529526
df = DataFrame(np.random.randint(m, size=(n, 3)), columns=list("abc"))
@@ -618,8 +615,6 @@ def test_nested_scope(self):
618615
tm.assert_frame_equal(result, expected)
619616

620617
def test_nested_raises_on_local_self_reference(self):
621-
from pandas.errors import UndefinedVariableError
622-
623618
df = DataFrame(np.random.randn(5, 3))
624619

625620
# can't reference ourself b/c we're a local so @ is necessary
@@ -678,8 +673,6 @@ def test_at_inside_string(self):
678673
tm.assert_frame_equal(result, expected)
679674

680675
def test_query_undefined_local(self):
681-
from pandas.errors import UndefinedVariableError
682-
683676
engine, parser = self.engine, self.parser
684677
skip_if_no_pandas_parser(parser)
685678

@@ -838,8 +831,6 @@ def test_date_index_query_with_NaT_duplicates(self):
838831
df.query("index < 20130101 < dates3", engine=engine, parser=parser)
839832

840833
def test_nested_scope(self):
841-
from pandas.errors import UndefinedVariableError
842-
843834
engine = self.engine
844835
parser = self.parser
845836
# smoke test

pandas/tests/indexing/multiindex/test_loc.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import numpy as np
22
import pytest
33

4-
from pandas.errors import PerformanceWarning
4+
from pandas.errors import (
5+
IndexingError,
6+
PerformanceWarning,
7+
)
58

69
import pandas as pd
710
from pandas import (
@@ -11,7 +14,6 @@
1114
Series,
1215
)
1316
import pandas._testing as tm
14-
from pandas.core.indexing import IndexingError
1517

1618

1719
@pytest.fixture

pandas/tests/indexing/test_iloc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pytest
1212

1313
from pandas.compat.numpy import is_numpy_min
14+
from pandas.errors import IndexingError
1415
import pandas.util._test_decorators as td
1516

1617
from pandas import (
@@ -32,7 +33,6 @@
3233
)
3334
import pandas._testing as tm
3435
from pandas.api.types import is_scalar
35-
from pandas.core.indexing import IndexingError
3636
from pandas.tests.indexing.common import Base
3737

3838
# We pass through the error message from numpy

pandas/tests/indexing/test_indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import numpy as np
88
import pytest
99

10+
from pandas.errors import IndexingError
11+
1012
from pandas.core.dtypes.common import (
1113
is_float_dtype,
1214
is_integer_dtype,
@@ -24,7 +26,6 @@
2426
)
2527
import pandas._testing as tm
2628
from pandas.core.api import Float64Index
27-
from pandas.core.indexing import IndexingError
2829
from pandas.tests.indexing.common import _mklbl
2930
from pandas.tests.indexing.test_floats import gen_obj
3031

pandas/tests/indexing/test_loc.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import numpy as np
1313
import pytest
1414

15+
from pandas.errors import IndexingError
1516
import pandas.util._test_decorators as td
1617

1718
import pandas as pd
@@ -36,10 +37,7 @@
3637
import pandas._testing as tm
3738
from pandas.api.types import is_scalar
3839
from pandas.core.api import Float64Index
39-
from pandas.core.indexing import (
40-
IndexingError,
41-
_one_ellipsis_message,
42-
)
40+
from pandas.core.indexing import _one_ellipsis_message
4341
from pandas.tests.indexing.common import Base
4442

4543

pandas/tests/series/indexing/test_setitem.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import numpy as np
77
import pytest
88

9+
from pandas.errors import IndexingError
10+
911
from pandas.core.dtypes.common import is_list_like
1012

1113
from pandas import (
@@ -30,7 +32,6 @@
3032
timedelta_range,
3133
)
3234
import pandas._testing as tm
33-
from pandas.core.indexing import IndexingError
3435

3536
from pandas.tseries.offsets import BDay
3637

pandas/tests/test_errors.py

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"SettingWithCopyError",
2828
"SettingWithCopyWarning",
2929
"NumExprClobberingError",
30+
"IndexingError",
3031
],
3132
)
3233
def test_exception_importable(exc):

0 commit comments

Comments
 (0)