Skip to content

ENH: Move IndexingError to error/__init__.py per GH27656 #47357

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 1 commit into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/reference/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Exceptions and warnings
errors.DtypeWarning
errors.DuplicateLabelError
errors.EmptyDataError
errors.IndexingError
errors.InvalidIndexError
errors.IntCastingNaNError
errors.MergeError
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Other enhancements
- A :class:`errors.PerformanceWarning` is now thrown when using ``string[pyarrow]`` dtype with methods that don't dispatch to ``pyarrow.compute`` methods (:issue:`42613`)
- 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`)
- ``times`` argument in :class:`.ExponentialMovingWindow` now accepts ``np.timedelta64`` (:issue:`47003`)
- :class:`DataError`, :class:`SpecificationError`, :class:`SettingWithCopyError`, :class:`SettingWithCopyWarning`, :class:`NumExprClobberingError`, :class:`UndefinedVariableError` are now exposed in ``pandas.errors`` (:issue:`27656`)
- :class:`DataError`, :class:`SpecificationError`, :class:`SettingWithCopyError`, :class:`SettingWithCopyWarning`, :class:`NumExprClobberingError`, :class:`UndefinedVariableError`, and :class:`IndexingError` are now exposed in ``pandas.errors`` (:issue:`27656`)
- Added ``check_like`` argument to :func:`testing.assert_series_equal` (:issue:`47247`)

.. ---------------------------------------------------------------------------
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pandas._libs.lib import item_from_zerodim
from pandas.errors import (
AbstractMethodError,
IndexingError,
InvalidIndexError,
)
from pandas.util._decorators import doc
Expand Down Expand Up @@ -121,10 +122,6 @@ def __getitem__(self, arg):
IndexSlice = _IndexSlice()


class IndexingError(Exception):
pass


class IndexingMixin:
"""
Mixin for adding .loc/.iloc/.at/.iat to Dataframes and Series.
Expand Down
21 changes: 21 additions & 0 deletions pandas/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,24 @@ def __init__(self, name: str, is_local: bool | None = None) -> None:
else:
msg = f"name {base_msg}"
super().__init__(msg)


class IndexingError(Exception):
"""
Exception is raised when trying to index and there is a mismatch in dimensions.

Examples
--------
>>> df = pd.DataFrame({'A': [1, 1, 1]})
>>> df.loc[..., ..., 'A'] # doctest: +SKIP
... # IndexingError: indexer may only contain one '...' entry
>>> df = pd.DataFrame({'A': [1, 1, 1]})
>>> df.loc[1, ..., ...] # doctest: +SKIP
... # IndexingError: Too many indexers
>>> df[pd.Series([True], dtype=bool)] # doctest: +SKIP
... # IndexingError: Unalignable boolean Series provided as indexer...
>>> s = pd.Series(range(2),
... index = pd.MultiIndex.from_product([["a", "b"], ["c"]]))
>>> s.loc["a", "c", "d"] # doctest: +SKIP
... # IndexingError: Too many indexers
"""
11 changes: 1 addition & 10 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import pytest

from pandas.errors import UndefinedVariableError
import pandas.util._test_decorators as td

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

def test_query_scope(self):
from pandas.errors import UndefinedVariableError

engine, parser = self.engine, self.parser
skip_if_no_pandas_parser(parser)

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

def test_query_doesnt_pickup_local(self):
from pandas.errors import UndefinedVariableError

engine, parser = self.engine, self.parser
n = m = 10
df = DataFrame(np.random.randint(m, size=(n, 3)), columns=list("abc"))
Expand Down Expand Up @@ -618,8 +615,6 @@ def test_nested_scope(self):
tm.assert_frame_equal(result, expected)

def test_nested_raises_on_local_self_reference(self):
from pandas.errors import UndefinedVariableError

df = DataFrame(np.random.randn(5, 3))

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

def test_query_undefined_local(self):
from pandas.errors import UndefinedVariableError

engine, parser = self.engine, self.parser
skip_if_no_pandas_parser(parser)

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

def test_nested_scope(self):
from pandas.errors import UndefinedVariableError

engine = self.engine
parser = self.parser
# smoke test
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import numpy as np
import pytest

from pandas.errors import PerformanceWarning
from pandas.errors import (
IndexingError,
PerformanceWarning,
)

import pandas as pd
from pandas import (
Expand All @@ -11,7 +14,6 @@
Series,
)
import pandas._testing as tm
from pandas.core.indexing import IndexingError


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pytest

from pandas.compat.numpy import is_numpy_min
from pandas.errors import IndexingError
import pandas.util._test_decorators as td

from pandas import (
Expand All @@ -32,7 +33,6 @@
)
import pandas._testing as tm
from pandas.api.types import is_scalar
from pandas.core.indexing import IndexingError
from pandas.tests.indexing.common import Base

# We pass through the error message from numpy
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import numpy as np
import pytest

from pandas.errors import IndexingError

from pandas.core.dtypes.common import (
is_float_dtype,
is_integer_dtype,
Expand All @@ -24,7 +26,6 @@
)
import pandas._testing as tm
from pandas.core.api import Float64Index
from pandas.core.indexing import IndexingError
from pandas.tests.indexing.common import _mklbl
from pandas.tests.indexing.test_floats import gen_obj

Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import numpy as np
import pytest

from pandas.errors import IndexingError
import pandas.util._test_decorators as td

import pandas as pd
Expand All @@ -36,10 +37,7 @@
import pandas._testing as tm
from pandas.api.types import is_scalar
from pandas.core.api import Float64Index
from pandas.core.indexing import (
IndexingError,
_one_ellipsis_message,
)
from pandas.core.indexing import _one_ellipsis_message
from pandas.tests.indexing.common import Base


Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import numpy as np
import pytest

from pandas.errors import IndexingError

from pandas.core.dtypes.common import is_list_like

from pandas import (
Expand All @@ -30,7 +32,6 @@
timedelta_range,
)
import pandas._testing as tm
from pandas.core.indexing import IndexingError

from pandas.tseries.offsets import BDay

Expand Down
1 change: 1 addition & 0 deletions pandas/tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"SettingWithCopyError",
"SettingWithCopyWarning",
"NumExprClobberingError",
"IndexingError",
],
)
def test_exception_importable(exc):
Expand Down