Skip to content

Commit 14a8d98

Browse files
committed
API: expose pandas.api.exceptions
xref pandas-dev#14800
1 parent 76e5185 commit 14a8d98

File tree

8 files changed

+153
-82
lines changed

8 files changed

+153
-82
lines changed

doc/source/whatsnew/v0.20.0.txt

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

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

5578
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._libs.tslib import OutOfBoundsDatetime
9+
from pandas.io.common import (ParserError, DtypeWarning,
10+
EmptyDataError, ParserWarning)

pandas/lib.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import warnings
44
warnings.warn("The pandas.lib module is deprecated and will be "
5-
"removed in a future version. Please import from "
6-
"the pandas._libs.lib instead", FutureWarning, stacklevel=2)
5+
"removed in a future version. These are private functions "
6+
"and can be accessed from pandas._libs.lib instead",
7+
FutureWarning, stacklevel=2)
78
from pandas._libs.lib import *

pandas/tests/api/test_api.py

+2-79
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
# -*- coding: utf-8 -*-
22

33
from warnings import catch_warnings
4-
import numpy as np
54

65
import pandas as pd
7-
from pandas.core import common as com
86
from pandas import api
9-
from pandas.api import types
107
from pandas.util import testing as tm
118

129

@@ -129,80 +126,6 @@ def test_api(self):
129126
self.check(api, self.allowed)
130127

131128

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

208131
def test_deprecation_access_func(self):
@@ -267,7 +190,7 @@ class TestLib(tm.TestCase):
267190
def test_deprecation_access_func(self):
268191
with tm.assert_produces_warning(FutureWarning,
269192
check_stacklevel=False):
270-
pd.lib.infer_dtype
193+
pd.lib.infer_dtype(['foo'])
271194

272195

273196
class TestTSLib(tm.TestCase):
@@ -276,4 +199,4 @@ def test_deprecation_access_func(self):
276199
# some libraries may be imported before we
277200
# test and could show the warning
278201
with catch_warnings(record=True):
279-
pd.tslib.Timestamp
202+
pd.tslib.Timestamp('20160101')

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))

pandas/tslib.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import warnings
44
warnings.warn("The pandas.tslib module is deprecated and will be "
55
"removed in a future version. Please import from "
6-
"the pandas._libs.tslib instead", FutureWarning, stacklevel=2)
6+
"the pandas or pandas.api.exceptions instead", FutureWarning, stacklevel=2)
77
from pandas._libs.tslib import (Timestamp, Timedelta,
88
NaT, OutOfBoundsDatetime)

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ def pxd(name):
626626
packages=['pandas',
627627
'pandas.api',
628628
'pandas.api.types',
629+
'pandas.api.exceptions',
629630
'pandas.compat',
630631
'pandas.compat.numpy',
631632
'pandas.computation',

0 commit comments

Comments
 (0)