Skip to content

ENH: move SpecificationError to error/__init__.py #47089

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 4 commits into from
May 26, 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 @@ -41,6 +41,7 @@ Exceptions and warnings
errors.ParserError
errors.ParserWarning
errors.PerformanceWarning
errors.SpecificationError
errors.UnsortedIndexError
errors.UnsupportedFunctionCall

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 @@ -151,7 +151,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` now exposed in ``pandas.errors`` (:issue:`27656`)
- :class:`DataError` and :class:`SpecificationError` are now exposed in ``pandas.errors`` (:issue:`27656`)

.. ---------------------------------------------------------------------------
.. _whatsnew_150.notable_bug_fixes:
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
Axis,
NDFrameT,
)
from pandas.errors import DataError
from pandas.errors import (
DataError,
SpecificationError,
)
from pandas.util._decorators import cache_readonly
from pandas.util._exceptions import find_stack_level

Expand All @@ -51,10 +54,7 @@
)

from pandas.core.algorithms import safe_sort
from pandas.core.base import (
SelectionMixin,
SpecificationError,
)
from pandas.core.base import SelectionMixin
import pandas.core.common as com
from pandas.core.construction import (
create_series_with_explicit_dtype,
Expand Down
4 changes: 0 additions & 4 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,6 @@ def __setattr__(self, key: str, value):
object.__setattr__(self, key, value)


class SpecificationError(Exception):
pass


class SelectionMixin(Generic[NDFrameT]):
"""
mixin implementing the selection & aggregation interface on a group-like
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
Manager2D,
SingleManager,
)
from pandas.errors import SpecificationError
from pandas.util._decorators import (
Appender,
Substitution,
Expand Down Expand Up @@ -68,7 +69,6 @@
reconstruct_func,
validate_func_kwargs,
)
from pandas.core.base import SpecificationError
import pandas.core.common as com
from pandas.core.construction import create_series_with_explicit_dtype
from pandas.core.frame import DataFrame
Expand Down
23 changes: 23 additions & 0 deletions pandas/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,26 @@ class DataError(Exception):
Or, it can be raised when trying to apply a function to a non-numerical
column on a rolling window.
"""


class SpecificationError(Exception):
"""
Exception raised in two scenarios. The first way is calling agg on a
Dataframe or Series using a nested renamer (dict-of-dict).
The second way is calling agg on a Dataframe with duplicated functions
names without assigning column name.

Examples
--------
>>> df = pd.DataFrame({'A': [1, 1, 1, 2, 2],
... 'B': range(5),
... 'C': range(5)})
>>> df.groupby('A').B.agg({'foo': 'count'}) # doctest: +SKIP
... # SpecificationError: nested renamer is not supported

>>> df.groupby('A').agg({'B': {'foo': ['sum', 'max']}}) # doctest: +SKIP
... # SpecificationError: nested renamer is not supported

>>> df.groupby('A').agg(['min', 'min']) # doctest: +SKIP
... # SpecificationError: nested renamer is not supported
"""
3 changes: 2 additions & 1 deletion pandas/tests/apply/test_invalid_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import numpy as np
import pytest

from pandas.errors import SpecificationError

from pandas import (
Categorical,
DataFrame,
Expand All @@ -20,7 +22,6 @@
notna,
)
import pandas._testing as tm
from pandas.core.base import SpecificationError


@pytest.mark.parametrize("result_type", ["foo", 1])
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import numpy as np
import pytest

from pandas.errors import SpecificationError

from pandas.core.dtypes.common import is_integer_dtype

import pandas as pd
Expand All @@ -21,7 +23,6 @@
to_datetime,
)
import pandas._testing as tm
from pandas.core.base import SpecificationError
from pandas.core.groupby.grouper import Grouping


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

from pandas.errors import SpecificationError

import pandas as pd
from pandas import (
DataFrame,
Expand All @@ -19,7 +21,6 @@
period_range,
)
import pandas._testing as tm
from pandas.core.base import SpecificationError

from pandas.io.formats.printing import pprint_thing

Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

from pandas._libs import lib
from pandas.compat import IS64
from pandas.errors import PerformanceWarning
from pandas.errors import (
PerformanceWarning,
SpecificationError,
)

import pandas as pd
from pandas import (
Expand All @@ -24,7 +27,6 @@
)
import pandas._testing as tm
from pandas.core.arrays import BooleanArray
from pandas.core.base import SpecificationError
import pandas.core.common as com
from pandas.core.groupby.base import maybe_normalize_deprecated_kernels

Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,15 @@ def test_agg():

msg = "nested renamer is not supported"
for t in cases:
with pytest.raises(pd.core.base.SpecificationError, match=msg):
with pytest.raises(pd.errors.SpecificationError, match=msg):
t.aggregate({"A": {"mean": "mean", "sum": "sum"}})

expected = pd.concat([a_mean, a_sum, b_mean, b_sum], axis=1)
expected.columns = pd.MultiIndex.from_tuples(
[("A", "mean"), ("A", "sum"), ("B", "mean2"), ("B", "sum2")]
)
for t in cases:
with pytest.raises(pd.core.base.SpecificationError, match=msg):
with pytest.raises(pd.errors.SpecificationError, match=msg):
t.aggregate(
{
"A": {"mean": "mean", "sum": "sum"},
Expand Down Expand Up @@ -539,10 +539,10 @@ def test_agg_misc():

# series like aggs
for t in cases:
with pytest.raises(pd.core.base.SpecificationError, match=msg):
with pytest.raises(pd.errors.SpecificationError, match=msg):
t["A"].agg({"A": ["sum", "std"]})

with pytest.raises(pd.core.base.SpecificationError, match=msg):
with pytest.raises(pd.errors.SpecificationError, match=msg):
t["A"].agg({"A": ["sum", "std"], "B": ["mean", "std"]})

# errors
Expand Down Expand Up @@ -588,17 +588,17 @@ def test_agg_nested_dicts():

msg = "nested renamer is not supported"
for t in cases:
with pytest.raises(pd.core.base.SpecificationError, match=msg):
with pytest.raises(pd.errors.SpecificationError, match=msg):
t.aggregate({"r1": {"A": ["mean", "sum"]}, "r2": {"B": ["mean", "sum"]}})

for t in cases:

with pytest.raises(pd.core.base.SpecificationError, match=msg):
with pytest.raises(pd.errors.SpecificationError, match=msg):
t[["A", "B"]].agg(
{"A": {"ra": ["mean", "std"]}, "B": {"rb": ["mean", "std"]}}
)

with pytest.raises(pd.core.base.SpecificationError, match=msg):
with pytest.raises(pd.errors.SpecificationError, match=msg):
t.agg({"A": {"ra": ["mean", "std"]}, "B": {"rb": ["mean", "std"]}})


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 @@ -20,6 +20,7 @@
"OptionError",
"NumbaUtilError",
"DataError",
"SpecificationError",
],
)
def test_exception_importable(exc):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/window/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import pytest

from pandas.errors import SpecificationError

from pandas import (
DataFrame,
Index,
Expand All @@ -13,7 +15,6 @@
timedelta_range,
)
import pandas._testing as tm
from pandas.core.base import SpecificationError


def test_getitem(step):
Expand Down