Skip to content

Commit ef5ffe2

Browse files
committed
DEPR: relocate exceptions from pandas.core.common pandas-dev#14800
Move exceptions/warnings from pandas.core.common to pandas.api.exceptions The exceptions/warnings can still be imported from pandas.core.common however a DeprecationWarning will be issued when they are raised.
1 parent 1661c08 commit ef5ffe2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+249
-124
lines changed

doc/source/whatsnew/v0.20.0.txt

+18-1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,23 @@ Deprecations
265265
- ``DataFrame.astype()`` has deprecated the ``raise_on_error`` parameter in favor of ``errors`` (:issue:`14878`)
266266
- ``Series.sortlevel`` and ``DataFrame.sortlevel`` have been deprecated in favor of ``Series.sort_index`` and ``DataFrame.sort_index`` (:issue:`15099`)
267267

268+
.. _whatsnew_0200.moved
269+
270+
Moved
271+
^^^^^
272+
273+
The following exceptions have been relocated from ``pandas.core.common`` to ``pandas.api.exceptions``:
274+
275+
- ``AbstractMethodError``
276+
- ``AmbiguousIndexError``
277+
- ``PandasError``
278+
- ``PerformanceWarning``
279+
- ``SettingWithCopyError``
280+
- ``SettingWithCopyWarning``
281+
- ``UnsupportedFunctionCall``
282+
- ``UnsortedIndexError``
283+
284+
Raising any of the above exceptions, imported from ``pandas.core.common``, will show a ``DeprecationWarning`` (:issue:`14800`)
268285

269286
.. _whatsnew_0200.prior_deprecations:
270287

@@ -369,4 +386,4 @@ Bug Fixes
369386
- Bug in ``Series`` constructor when both ``copy=True`` and ``dtype`` arguments are provided (:issue:`15125`)
370387
- Bug in ``pd.read_csv()`` for the C engine where ``usecols`` were being indexed incorrectly with ``parse_dates`` (:issue:`14792`)
371388

372-
- Bug in ``Series.dt.round`` inconsistent behaviour on NAT's with different arguments (:issue:`14940`)
389+
- Bug in ``Series.dt.round`` inconsistent behaviour on NAT's with different arguments (:issue:`14940`)

pandas/api/exceptions.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
class PandasError(Exception):
3+
pass
4+
5+
6+
class PerformanceWarning(Warning):
7+
pass
8+
9+
10+
class SettingWithCopyError(ValueError):
11+
pass
12+
13+
14+
class SettingWithCopyWarning(Warning):
15+
pass
16+
17+
18+
class AmbiguousIndexError(PandasError, KeyError):
19+
pass
20+
21+
22+
class UnsupportedFunctionCall(ValueError):
23+
pass
24+
25+
26+
class UnsortedIndexError(KeyError):
27+
""" Error raised when attempting to get a slice of a MultiIndex
28+
and the index has not been lexsorted. Subclass of `KeyError`.
29+
30+
.. versionadded:: 0.20.0
31+
32+
"""
33+
pass
34+
35+
36+
class AbstractMethodError(NotImplementedError):
37+
"""Raise this error instead of NotImplementedError for abstract methods
38+
while keeping compatibility with Python 2 and Python 3.
39+
"""
40+
41+
def __init__(self, class_instance):
42+
self.class_instance = class_instance
43+
44+
def __str__(self):
45+
return ("This method must be defined in the concrete class of %s" %
46+
self.class_instance.__class__.__name__)

pandas/api/tests/test_api.py

+48-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import numpy as np
44

55
import pandas as pd
6+
import pandas.core.common
7+
from pandas.api import exceptions
68
from pandas.core import common as com
79
from pandas import api
810
from pandas.api import types
@@ -135,7 +137,7 @@ def test_api(self):
135137

136138
class TestApi(Base, tm.TestCase):
137139

138-
allowed = ['tests', 'types']
140+
allowed = ['exceptions', 'tests', 'types']
139141

140142
def test_api(self):
141143

@@ -215,6 +217,50 @@ def test_removed_from_core_common(self):
215217
'ensure_float']:
216218
self.assertRaises(AttributeError, lambda: getattr(com, t))
217219

220+
def test_exceptions_deprecated_in_commom_core(self):
221+
# see issue #14800. Exceptions deprecated & moved from
222+
# pandas.common.core to pandas.api.exceptions
223+
224+
class _ConcreteClass:
225+
pass
226+
227+
moved_exceptions = ('AmbiguousIndexError', 'PandasError',
228+
'PerformanceWarning', 'SettingWithCopyError',
229+
'SettingWithCopyWarning',
230+
'UnsupportedFunctionCall',
231+
'UnsortedIndexError')
232+
233+
for moved_exception in moved_exceptions:
234+
with tm.assert_produces_warning(DeprecationWarning):
235+
getattr(pandas.core.common, moved_exception)()
236+
237+
with tm.assert_produces_warning(DeprecationWarning):
238+
pandas.core.common.AbstractMethodError(_ConcreteClass())
239+
240+
with self.assertRaises(exceptions.AbstractMethodError):
241+
raise exceptions.AbstractMethodError(_ConcreteClass())
242+
243+
with self.assertRaises(exceptions.AmbiguousIndexError):
244+
raise exceptions.AmbiguousIndexError()
245+
246+
with self.assertRaises(exceptions.PandasError):
247+
raise exceptions.PandasError()
248+
249+
with self.assertRaises(exceptions.PerformanceWarning):
250+
raise exceptions.PerformanceWarning()
251+
252+
with self.assertRaises(exceptions.SettingWithCopyError):
253+
raise exceptions.SettingWithCopyError()
254+
255+
with self.assertRaises(exceptions.SettingWithCopyWarning):
256+
raise exceptions.SettingWithCopyWarning()
257+
258+
with self.assertRaises(exceptions.UnsupportedFunctionCall):
259+
raise exceptions.UnsupportedFunctionCall()
260+
261+
with self.assertRaises(exceptions.UnsortedIndexError):
262+
raise exceptions.UnsortedIndexError()
263+
218264

219265
class TestDatetools(tm.TestCase):
220266

@@ -228,6 +274,7 @@ def test_deprecation_access_obj(self):
228274
check_stacklevel=False):
229275
pd.datetools.monthEnd
230276

277+
231278
if __name__ == '__main__':
232279
import nose
233280
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

pandas/compat/numpy/function.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from numpy import ndarray
2222
from pandas.util.validators import (validate_args, validate_kwargs,
2323
validate_args_and_kwargs)
24-
from pandas.core.common import UnsupportedFunctionCall
24+
from pandas.api.exceptions import UnsupportedFunctionCall
2525
from pandas.types.common import is_integer, is_bool
2626
from pandas.compat import OrderedDict
2727

pandas/core/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pandas.compat.numpy import function as nv
1717
from pandas.util.decorators import (Appender, cache_readonly,
1818
deprecate_kwarg, Substitution)
19-
from pandas.core.common import AbstractMethodError
19+
from pandas.api.exceptions import AbstractMethodError
2020
from pandas.formats.printing import pprint_thing
2121

2222
_shared_docs = dict()

pandas/core/common.py

+16-47
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from pandas.api import types
2121
from pandas.types import common
2222

23+
from pandas.util.depr_module import _add_proxies
24+
2325
# back-compat of public API
2426
# deprecate these functions
2527
m = sys.modules['pandas.core.common']
@@ -63,6 +65,20 @@ def wrapper(*args, **kwargs):
6365

6466
setattr(m, t, outer(t))
6567

68+
# Relocate exceptions, see #14800
69+
_moved_exceptions = ('AbstractMethodError',
70+
'AmbiguousIndexError',
71+
'PandasError',
72+
'PerformanceWarning',
73+
'SettingWithCopyError',
74+
'SettingWithCopyWarning',
75+
'UnsupportedFunctionCall',
76+
'UnsortedIndexError')
77+
78+
_add_proxies(old_mod_name='pandas.core.common',
79+
new_mod_name='pandas.api.exceptions',
80+
entities=_moved_exceptions)
81+
6682

6783
# deprecate array_equivalent
6884

@@ -73,53 +89,6 @@ def array_equivalent(*args, **kwargs):
7389
return missing.array_equivalent(*args, **kwargs)
7490

7591

76-
class PandasError(Exception):
77-
pass
78-
79-
80-
class PerformanceWarning(Warning):
81-
pass
82-
83-
84-
class SettingWithCopyError(ValueError):
85-
pass
86-
87-
88-
class SettingWithCopyWarning(Warning):
89-
pass
90-
91-
92-
class AmbiguousIndexError(PandasError, KeyError):
93-
pass
94-
95-
96-
class UnsupportedFunctionCall(ValueError):
97-
pass
98-
99-
100-
class UnsortedIndexError(KeyError):
101-
""" Error raised when attempting to get a slice of a MultiIndex
102-
and the index has not been lexsorted. Subclass of `KeyError`.
103-
104-
.. versionadded:: 0.20.0
105-
106-
"""
107-
pass
108-
109-
110-
class AbstractMethodError(NotImplementedError):
111-
"""Raise this error instead of NotImplementedError for abstract methods
112-
while keeping compatibility with Python 2 and Python 3.
113-
"""
114-
115-
def __init__(self, class_instance):
116-
self.class_instance = class_instance
117-
118-
def __str__(self):
119-
return ("This method must be defined in the concrete class of %s" %
120-
self.class_instance.__class__.__name__)
121-
122-
12392
def flatten(l):
12493
"""Flatten an arbitrarily nested sequence.
12594

pandas/core/frame.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
is_named_tuple)
5757
from pandas.types.missing import isnull, notnull
5858

59-
from pandas.core.common import (PandasError, _try_sort,
59+
from pandas.api.exceptions import PandasError
60+
61+
from pandas.core.common import (_try_sort,
6062
_default_index,
6163
_values_from_object,
6264
_maybe_box_datetimelike,

pandas/core/generic.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
from pandas.types.cast import _maybe_promote, _maybe_upcast_putmask
2828
from pandas.types.missing import isnull, notnull
2929
from pandas.types.generic import ABCSeries, ABCPanel
30-
30+
from pandas.api.exceptions import SettingWithCopyError, AbstractMethodError
3131
from pandas.core.common import (_values_from_object,
32-
_maybe_box_datetimelike,
33-
SettingWithCopyError, SettingWithCopyWarning,
34-
AbstractMethodError)
32+
_maybe_box_datetimelike)
33+
34+
from pandas.api.exceptions import SettingWithCopyWarning
3535

3636
from pandas.core.base import PandasObject
3737
from pandas.core.index import (Index, MultiIndex, _ensure_index,

pandas/core/groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
_ensure_float)
3333
from pandas.types.cast import _possibly_downcast_to_dtype
3434
from pandas.types.missing import isnull, notnull, _maybe_fill
35-
36-
from pandas.core.common import _values_from_object, AbstractMethodError
35+
from pandas.api.exceptions import AbstractMethodError
36+
from pandas.core.common import _values_from_object
3737
from pandas.core.base import (PandasObject, SelectionMixin, GroupByError,
3838
DataError, SpecificationError)
3939
from pandas.core.categorical import Categorical

pandas/core/ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
from pandas.compat import bind_method
2020
import pandas.core.missing as missing
2121
import pandas.algos as _algos
22-
from pandas.core.common import (_values_from_object, _maybe_match_name,
23-
PerformanceWarning)
22+
from pandas.api.exceptions import PerformanceWarning
23+
from pandas.core.common import _values_from_object, _maybe_match_name
2424
from pandas.types.missing import notnull, isnull
2525
from pandas.types.common import (needs_i8_conversion,
2626
is_datetimelike_v_numeric,

pandas/core/panel.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
is_string_like, is_scalar)
1515
from pandas.types.missing import notnull
1616

17+
from pandas.api.exceptions import PandasError
18+
1719
import pandas.computation.expressions as expressions
1820
import pandas.core.common as com
1921
import pandas.core.ops as ops
2022
import pandas.core.missing as missing
2123
from pandas import compat
2224
from pandas.compat import (map, zip, range, u, OrderedDict, OrderedDefaultdict)
2325
from pandas.compat.numpy import function as nv
24-
from pandas.core.common import PandasError, _try_sort, _default_index
26+
from pandas.core.common import _try_sort, _default_index
2527
from pandas.core.frame import DataFrame
2628
from pandas.core.generic import NDFrame, _shared_docs
2729
from pandas.core.index import (Index, MultiIndex, _ensure_index,

pandas/core/series.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@
3232
_possibly_convert_platform,
3333
_possibly_cast_to_datetime, _possibly_castable)
3434
from pandas.types.missing import isnull, notnull
35-
35+
from pandas.api.exceptions import SettingWithCopyError
3636
from pandas.core.common import (is_bool_indexer,
3737
_default_index,
3838
_asarray_tuplesafe,
3939
_values_from_object,
4040
_try_sort,
4141
_maybe_match_name,
42-
SettingWithCopyError,
4342
_maybe_box_datetimelike,
4443
_dict_compat)
4544
from pandas.core.index import (Index, MultiIndex, InvalidIndexError,

pandas/indexes/multi.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222
is_list_like,
2323
is_scalar)
2424
from pandas.types.missing import isnull, array_equivalent
25+
from pandas.api.exceptions import PerformanceWarning, UnsortedIndexError
2526
from pandas.core.common import (_values_from_object,
2627
is_bool_indexer,
27-
is_null_slice,
28-
PerformanceWarning,
29-
UnsortedIndexError)
28+
is_null_slice)
3029

3130

3231
from pandas.core.base import FrozenList

pandas/io/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pandas.compat import StringIO, BytesIO, string_types, text_type
1010
from pandas import compat
1111
from pandas.formats.printing import pprint_thing
12-
from pandas.core.common import AbstractMethodError
12+
from pandas.api.exceptions import AbstractMethodError
1313
from pandas.types.common import is_number
1414

1515
try:

pandas/io/gbq.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pandas import compat
1414
from pandas.core.api import DataFrame
1515
from pandas.tools.merge import concat
16-
from pandas.core.common import PandasError
16+
from pandas.api.exceptions import PandasError
1717
from pandas.compat import lzip, bytes_to_str
1818

1919

pandas/io/html.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from pandas.compat import (lrange, lmap, u, string_types, iteritems,
2020
raise_with_traceback, binary_type)
2121
from pandas import Series
22-
from pandas.core.common import AbstractMethodError
22+
from pandas.api.exceptions import AbstractMethodError
2323
from pandas.formats.printing import pprint_thing
2424

2525
_IMPORTS = False

pandas/io/json.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from pandas.compat import StringIO, long, u
1111
from pandas import compat, isnull
1212
from pandas import Series, DataFrame, to_datetime
13+
from pandas.api.exceptions import AbstractMethodError
1314
from pandas.io.common import get_filepath_or_buffer, _get_handle
14-
from pandas.core.common import AbstractMethodError
1515
from pandas.formats.printing import pprint_thing
1616

1717
loads = _json.loads

pandas/io/packers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
from pandas.sparse.api import SparseSeries, SparseDataFrame
6060
from pandas.sparse.array import BlockIndex, IntIndex
6161
from pandas.core.generic import NDFrame
62-
from pandas.core.common import PerformanceWarning
62+
from pandas.api.exceptions import PerformanceWarning
6363
from pandas.io.common import get_filepath_or_buffer
6464
from pandas.core.internals import BlockManager, make_block, _safe_reshape
6565
import pandas.core.internals as internals

0 commit comments

Comments
 (0)