Skip to content

Commit 4386409

Browse files
committed
API: expose pandas.api.exceptions
xref pandas-dev#14800
1 parent 2340fb8 commit 4386409

File tree

6 files changed

+147
-78
lines changed

6 files changed

+147
-78
lines changed

doc/source/whatsnew/v0.20.0.txt

+23
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,29 @@ fixed-width text files, and :func:`read_excel` for parsing Excel files.
5353
pd.read_fwf(StringIO(data)).dtypes
5454
pd.read_fwf(StringIO(data), dtype={'a':'float64', 'b':'object'}).dtypes
5555

56+
57+
.. _whatsnew_0200.enhancements.dev_api_exceptions:
58+
59+
pandas development API
60+
^^^^^^^^^^^^^^^^^^^^^^
61+
62+
In 0.19.0, we introduced standard sub-package to present a public API, see :ref:`here <whatsnew_0190.dev_api>`.
63+
We are adding ``pandas.api.exceptions`` so to expose supported exceptions and warnings. (:issue:`14800`)
64+
65+
The following are now part of this API:
66+
67+
.. ipython:: python
68+
69+
import pprint
70+
from pandas.api import exceptions
71+
excs = [ e for e in dir(exceptions) if not e.startswith('_') ]
72+
pprint.pprint(excs)
73+
74+
.. note::
75+
76+
These existing definitions for these are deprecated and will be removed in a future version.
77+
78+
5679
.. _whatsnew_0200.enhancements.groupby_access:
5780

5881
Groupby Enhancements

pandas/api/exceptions/__init__.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# flake8: noqa
2+
3+
""" public toolkit API """
4+
5+
from pandas.core.common import (PandasError, PerformanceWarning,
6+
AmbiguousIndexError, UnsupportedFunctionCall,
7+
UnsortedIndexError)
8+
from pandas.tslib import OutOfBoundsDatetime
9+
from pandas.io.common import (ParserError, DtypeWarning,
10+
EmptyDataError, ParserWarning)

pandas/tests/api/test_api.py

-78
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3-
import numpy as np
4-
53
import pandas as pd
6-
from pandas.core import common as com
74
from pandas import api
8-
from pandas.api import types
95
from pandas.util import testing as tm
106

117

@@ -140,80 +136,6 @@ def test_api(self):
140136
self.check(api, self.allowed)
141137

142138

143-
class TestTypes(Base, tm.TestCase):
144-
145-
allowed = ['is_any_int_dtype', 'is_bool', 'is_bool_dtype',
146-
'is_categorical', 'is_categorical_dtype', 'is_complex',
147-
'is_complex_dtype', 'is_datetime64_any_dtype',
148-
'is_datetime64_dtype', 'is_datetime64_ns_dtype',
149-
'is_datetime64tz_dtype', 'is_datetimetz', 'is_dtype_equal',
150-
'is_extension_type', 'is_float', 'is_float_dtype',
151-
'is_floating_dtype', 'is_int64_dtype', 'is_integer',
152-
'is_integer_dtype', 'is_number', 'is_numeric_dtype',
153-
'is_object_dtype', 'is_scalar', 'is_sparse',
154-
'is_string_dtype', 'is_signed_integer_dtype',
155-
'is_timedelta64_dtype', 'is_timedelta64_ns_dtype',
156-
'is_unsigned_integer_dtype', 'is_period',
157-
'is_period_dtype', 'is_re', 'is_re_compilable',
158-
'is_dict_like', 'is_iterator',
159-
'is_list_like', 'is_hashable',
160-
'is_named_tuple', 'is_sequence',
161-
'pandas_dtype']
162-
163-
def test_types(self):
164-
165-
self.check(types, self.allowed)
166-
167-
def check_deprecation(self, fold, fnew):
168-
with tm.assert_produces_warning(DeprecationWarning):
169-
try:
170-
result = fold('foo')
171-
expected = fnew('foo')
172-
self.assertEqual(result, expected)
173-
except TypeError:
174-
self.assertRaises(TypeError,
175-
lambda: fnew('foo'))
176-
except AttributeError:
177-
self.assertRaises(AttributeError,
178-
lambda: fnew('foo'))
179-
180-
def test_deprecation_core_common(self):
181-
182-
# test that we are in fact deprecating
183-
# the pandas.core.common introspectors
184-
for t in self.allowed:
185-
self.check_deprecation(getattr(com, t), getattr(types, t))
186-
187-
def test_deprecation_core_common_array_equivalent(self):
188-
189-
with tm.assert_produces_warning(DeprecationWarning):
190-
com.array_equivalent(np.array([1, 2]), np.array([1, 2]))
191-
192-
def test_deprecation_core_common_moved(self):
193-
194-
# these are in pandas.types.common
195-
l = ['is_datetime_arraylike',
196-
'is_datetime_or_timedelta_dtype',
197-
'is_datetimelike',
198-
'is_datetimelike_v_numeric',
199-
'is_datetimelike_v_object',
200-
'is_datetimetz',
201-
'is_int_or_datetime_dtype',
202-
'is_period_arraylike',
203-
'is_string_like',
204-
'is_string_like_dtype']
205-
206-
from pandas.types import common as c
207-
for t in l:
208-
self.check_deprecation(getattr(com, t), getattr(c, t))
209-
210-
def test_removed_from_core_common(self):
211-
212-
for t in ['is_null_datelike_scalar',
213-
'ensure_float']:
214-
self.assertRaises(AttributeError, lambda: getattr(com, t))
215-
216-
217139
class TestDatetools(tm.TestCase):
218140

219141
def test_deprecation_access_func(self):

pandas/tests/api/test_exceptions.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import pytest
4+
import pandas # noqa
5+
import pandas as pd
6+
7+
8+
@pytest.mark.parametrize(
9+
"exc", ['PandasError', 'AmbiguousIndexError',
10+
'UnsupportedFunctionCall', 'UnsortedIndexError',
11+
'OutOfBoundsDatetime',
12+
'ParserError', 'PerformanceWarning', 'DtypeWarning',
13+
'EmptyDataError', 'ParserWarning'])
14+
def test_exception_importable(exc):
15+
from pandas.api import exceptions
16+
e = getattr(exceptions, exc)
17+
assert e is not None
18+
19+
# check that we can raise on them
20+
with pytest.raises(e):
21+
raise e()
22+
23+
24+
def test_catch_oob():
25+
from pandas.api import exceptions
26+
27+
try:
28+
pd.Timestamp('15000101')
29+
except exceptions.OutOfBoundsDatetime:
30+
pass

pandas/tests/api/test_types.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
5+
from pandas.core import common as com
6+
from pandas.api import types
7+
from pandas.util import testing as tm
8+
9+
from .test_api import Base
10+
11+
12+
class TestTypes(Base, tm.TestCase):
13+
14+
allowed = ['is_any_int_dtype', 'is_bool', 'is_bool_dtype',
15+
'is_categorical', 'is_categorical_dtype', 'is_complex',
16+
'is_complex_dtype', 'is_datetime64_any_dtype',
17+
'is_datetime64_dtype', 'is_datetime64_ns_dtype',
18+
'is_datetime64tz_dtype', 'is_datetimetz', 'is_dtype_equal',
19+
'is_extension_type', 'is_float', 'is_float_dtype',
20+
'is_floating_dtype', 'is_int64_dtype', 'is_integer',
21+
'is_integer_dtype', 'is_number', 'is_numeric_dtype',
22+
'is_object_dtype', 'is_scalar', 'is_sparse',
23+
'is_string_dtype', 'is_signed_integer_dtype',
24+
'is_timedelta64_dtype', 'is_timedelta64_ns_dtype',
25+
'is_unsigned_integer_dtype', 'is_period',
26+
'is_period_dtype', 'is_re', 'is_re_compilable',
27+
'is_dict_like', 'is_iterator',
28+
'is_list_like', 'is_hashable',
29+
'is_named_tuple', 'is_sequence',
30+
'pandas_dtype']
31+
32+
def test_types(self):
33+
34+
self.check(types, self.allowed)
35+
36+
def check_deprecation(self, fold, fnew):
37+
with tm.assert_produces_warning(DeprecationWarning):
38+
try:
39+
result = fold('foo')
40+
expected = fnew('foo')
41+
self.assertEqual(result, expected)
42+
except TypeError:
43+
self.assertRaises(TypeError,
44+
lambda: fnew('foo'))
45+
except AttributeError:
46+
self.assertRaises(AttributeError,
47+
lambda: fnew('foo'))
48+
49+
def test_deprecation_core_common(self):
50+
51+
# test that we are in fact deprecating
52+
# the pandas.core.common introspectors
53+
for t in self.allowed:
54+
self.check_deprecation(getattr(com, t), getattr(types, t))
55+
56+
def test_deprecation_core_common_array_equivalent(self):
57+
58+
with tm.assert_produces_warning(DeprecationWarning):
59+
com.array_equivalent(np.array([1, 2]), np.array([1, 2]))
60+
61+
def test_deprecation_core_common_moved(self):
62+
63+
# these are in pandas.types.common
64+
l = ['is_datetime_arraylike',
65+
'is_datetime_or_timedelta_dtype',
66+
'is_datetimelike',
67+
'is_datetimelike_v_numeric',
68+
'is_datetimelike_v_object',
69+
'is_datetimetz',
70+
'is_int_or_datetime_dtype',
71+
'is_period_arraylike',
72+
'is_string_like',
73+
'is_string_like_dtype']
74+
75+
from pandas.types import common as c
76+
for t in l:
77+
self.check_deprecation(getattr(com, t), getattr(c, t))
78+
79+
def test_removed_from_core_common(self):
80+
81+
for t in ['is_null_datelike_scalar',
82+
'ensure_float']:
83+
self.assertRaises(AttributeError, lambda: getattr(com, t))

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ def pxd(name):
623623
packages=['pandas',
624624
'pandas.api',
625625
'pandas.api.types',
626+
'pandas.api.exceptions',
626627
'pandas.compat',
627628
'pandas.compat.numpy',
628629
'pandas.computation',

0 commit comments

Comments
 (0)