Skip to content

Commit da0523a

Browse files
committed
API: expose pandas.errors
closes pandas-dev#14800 Author: Jeff Reback <[email protected]> Closes pandas-dev#15541 from jreback/exceptions and squashes the following commits: e5fbdc8 [Jeff Reback] give nicer deprecation / message on infer_dtype moving ab4525b [Jeff Reback] typo on pandas.errors in whatsnew d636ef7 [Jeff Reback] document removed exceptions 3dc4b9a [Jeff Reback] more docs for exceptions 2bb1fbd [Jeff Reback] remove AmbiguousIndexError, completely unused 5754630 [Jeff Reback] fix doc-string 35d225f [Jeff Reback] more examples e91901d [Jeff Reback] DOC: better docs on infer_type 7e8432d [Jeff Reback] remove need for PandasError sub-class 92b2fdc [Jeff Reback] corrections 991fbb4 [Jeff Reback] API: expose pandas.errors eec40cd [Jeff Reback] add pandas.api.lib add infer_dtype to pandas.api.lib
1 parent eedcc8f commit da0523a

Some content is hidden

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

47 files changed

+420
-236
lines changed

doc/source/whatsnew/v0.20.0.txt

+26
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ Commonly called 'unix epoch' or POSIX time.
7676

7777
pd.to_datetime([1, 2, 3], unit='D')
7878

79+
.. _whatsnew_0200.enhancements.errors:
80+
81+
pandas errors
82+
^^^^^^^^^^^^^
83+
84+
We are adding a standard public location for all pandas exceptions & warnings ``pandas.errors``. (:issue:`14800`). Previously
85+
these exceptions & warnings could be imported from ``pandas.core.common`` or ``pandas.io.common``. These exceptions and warnings
86+
will be removed from the ``*.common`` locations in a future release. (:issue:`15541`)
87+
88+
The following are now part of this API:
89+
90+
.. code-block:: python
91+
92+
['DtypeWarning',
93+
'EmptyDataError',
94+
'OutOfBoundsDatetime',
95+
'ParserError',
96+
'ParserWarning',
97+
'PerformanceWarning',
98+
'UnsortedIndexError',
99+
'UnsupportedFunctionCall']
100+
79101
.. _whatsnew_0200.enhancements.groupby_access:
80102

81103
Groupby Enhancements
@@ -858,6 +880,10 @@ Other API Changes
858880
- ``NaT`` will now correctly return ``np.nan`` for ``Timedelta`` and ``Period`` accessors such as ``days`` and ``quarter`` (:issue:`15782`)
859881
- ``NaT`` will now returns ``NaT`` for ``tz_localize`` and ``tz_convert``
860882
methods (:issue:`15830`)
883+
- ``DataFrame`` and ``Panel`` constructors with invalid input will now raise ``ValueError`` rather than ``PandasError``, if called with scalar inputs and not axes (:issue:`15541`)
884+
885+
- ``DataFrame`` and ``Panel`` constructors with invalid input will now raise ``ValueError`` rather than ``pandas.core.common.PandasError``, if called with scalar inputs and not axes; The exception ``PandasError`` is removed as well. (:issue:`15541`)
886+
- The exception ``pandas.core.common.AmbiguousIndexError`` is removed as it is not referenced (:issue:`15541`)
861887

862888
.. _whatsnew_0200.develop:
863889

pandas/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262

6363
json = _DeprecatedModule(deprmod='pandas.json', deprmodto='pandas.io.json.libjson')
6464
parser = _DeprecatedModule(deprmod='pandas.parser', deprmodto='pandas.io.libparsers')
65-
lib = _DeprecatedModule(deprmod='pandas.lib', deprmodto='pandas._libs.lib')
65+
lib = _DeprecatedModule(deprmod='pandas.lib', deprmodto='pandas._libs.lib',
66+
moved={'infer_dtype': 'pandas.api.lib.infer_dtype'})
6667
tslib = _DeprecatedModule(deprmod='pandas.tslib', deprmodto='pandas._libs.tslib')
6768

6869
# use the closest tagged version if possible

pandas/_libs/src/inference.pyx

+96-14
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,91 @@ cdef _try_infer_map(v):
218218
return None
219219

220220

221-
def infer_dtype(object _values):
221+
def infer_dtype(object value):
222222
"""
223-
we are coercing to an ndarray here
223+
Effeciently infer the type of a passed val, or list-like
224+
array of values. Return a string describing the type.
225+
226+
Parameters
227+
----------
228+
value : scalar, list, ndarray, or pandas type
229+
230+
Returns
231+
-------
232+
string describing the common type of the input data.
233+
Results can include:
234+
235+
- string
236+
- unicode
237+
- bytes
238+
- floating
239+
- integer
240+
- mixed-integer
241+
- mixed-integer-float
242+
- complex
243+
- categorical
244+
- boolean
245+
- datetime64
246+
- datetime
247+
- date
248+
- timedelta64
249+
- timedelta
250+
- time
251+
- period
252+
- mixed
253+
254+
Raises
255+
------
256+
TypeError if ndarray-like but cannot infer the dtype
257+
258+
Notes
259+
-----
260+
- 'mixed' is the catchall for anything that is not otherwise
261+
specialized
262+
- 'mixed-integer-float' are floats and integers
263+
- 'mixed-integer' are integers mixed with non-integers
264+
265+
Examples
266+
--------
267+
>>> infer_dtype(['foo', 'bar'])
268+
'string'
269+
270+
>>> infer_dtype([b'foo', b'bar'])
271+
'bytes'
272+
273+
>>> infer_dtype([1, 2, 3])
274+
'integer'
275+
276+
>>> infer_dtype([1, 2, 3.5])
277+
'mixed-integer-float'
278+
279+
>>> infer_dtype([1.0, 2.0, 3.5])
280+
'floating'
281+
282+
>>> infer_dtype(['a', 1])
283+
'mixed-integer'
284+
285+
>>> infer_dtype([True, False])
286+
'boolean'
287+
288+
>>> infer_dtype([True, False, np.nan])
289+
'mixed'
290+
291+
>>> infer_dtype([pd.Timestamp('20130101')])
292+
'datetime'
293+
294+
>>> infer_dtype([datetime.date(2013, 1, 1)])
295+
'date'
296+
297+
>>> infer_dtype([np.datetime64('2013-01-01')])
298+
'datetime64'
299+
300+
>>> infer_dtype([datetime.timedelta(0, 1, 1)])
301+
'timedelta'
302+
303+
>>> infer_dtype(pd.Series(list('aabc')).astype('category'))
304+
'categorical'
305+
224306
"""
225307

226308
cdef:
@@ -229,27 +311,27 @@ def infer_dtype(object _values):
229311
ndarray values
230312
bint seen_pdnat = False, seen_val = False
231313

232-
if isinstance(_values, np.ndarray):
233-
values = _values
234-
elif hasattr(_values, 'dtype'):
314+
if isinstance(value, np.ndarray):
315+
values = value
316+
elif hasattr(value, 'dtype'):
235317

236318
# this will handle ndarray-like
237319
# e.g. categoricals
238320
try:
239-
values = getattr(_values, '_values', getattr(
240-
_values, 'values', _values))
321+
values = getattr(value, '_values', getattr(
322+
value, 'values', value))
241323
except:
242-
val = _try_infer_map(_values)
243-
if val is not None:
244-
return val
324+
value = _try_infer_map(value)
325+
if value is not None:
326+
return value
245327

246328
# its ndarray like but we can't handle
247-
raise ValueError("cannot infer type for {0}".format(type(_values)))
329+
raise ValueError("cannot infer type for {0}".format(type(value)))
248330

249331
else:
250-
if not isinstance(_values, list):
251-
_values = list(_values)
252-
values = list_to_object_array(_values)
332+
if not isinstance(value, list):
333+
value = list(value)
334+
values = list_to_object_array(value)
253335

254336
values = getattr(values, 'values', values)
255337
val = _try_infer_map(values)

pandas/api/lib/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# flake8: noqa
2+
3+
""" public toolkit API """
4+
5+
from pandas._libs.lib import infer_dtype

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.errors import UnsupportedFunctionCall
2525
from pandas.types.common import is_integer, is_bool
2626
from pandas.compat import OrderedDict
2727

pandas/computation/align.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
import pandas as pd
1111
from pandas import compat
12-
import pandas.core.common as com
12+
from pandas.errors import PerformanceWarning
13+
from pandas.core.common import flatten
1314
from pandas.computation.common import _result_type_many
1415

1516

@@ -101,7 +102,7 @@ def _align_core(terms):
101102
'than an order of magnitude on term {1!r}, '
102103
'by more than {2:.4g}; performance may '
103104
'suffer'.format(axis, terms[i].name, ordm),
104-
category=pd.core.common.PerformanceWarning,
105+
category=PerformanceWarning,
105106
stacklevel=6)
106107

107108
if transpose:
@@ -121,7 +122,7 @@ def _align(terms):
121122
"""Align a set of terms"""
122123
try:
123124
# flatten the parse tree (a nested list, really)
124-
terms = list(com.flatten(terms))
125+
terms = list(flatten(terms))
125126
except TypeError:
126127
# can't iterate so it must just be a constant or single variable
127128
if isinstance(terms.value, pd.core.generic.NDFrame):

pandas/core/common.py

+4-26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
from pandas.api import types
2121
from pandas.types import common
2222

23+
# compat
24+
from pandas.errors import ( # noqa
25+
PerformanceWarning, UnsupportedFunctionCall, UnsortedIndexError)
26+
2327
# back-compat of public API
2428
# deprecate these functions
2529
m = sys.modules['pandas.core.common']
@@ -73,14 +77,6 @@ def array_equivalent(*args, **kwargs):
7377
return missing.array_equivalent(*args, **kwargs)
7478

7579

76-
class PandasError(Exception):
77-
pass
78-
79-
80-
class PerformanceWarning(Warning):
81-
pass
82-
83-
8480
class SettingWithCopyError(ValueError):
8581
pass
8682

@@ -89,24 +85,6 @@ class SettingWithCopyWarning(Warning):
8985
pass
9086

9187

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-
11088
class AbstractMethodError(NotImplementedError):
11189
"""Raise this error instead of NotImplementedError for abstract methods
11290
while keeping compatibility with Python 2 and Python 3.

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
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.core.common import (_try_sort,
6060
_default_index,
6161
_values_from_object,
6262
_maybe_box_datetimelike,
@@ -347,7 +347,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
347347
mgr = self._init_ndarray(values, index, columns, dtype=dtype,
348348
copy=False)
349349
else:
350-
raise PandasError('DataFrame constructor not properly called!')
350+
raise ValueError('DataFrame constructor not properly called!')
351351

352352
NDFrame.__init__(self, mgr, fastpath=True)
353353

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ def _convert_to_indexer(self, obj, axis=0, is_setter=False):
11401140
ix[['foo', 'bar', 'baz']] -> [i, j, k] (indices of foo, bar, baz)
11411141
11421142
Going by Zen of Python?
1143-
"In the face of ambiguity, refuse the temptation to guess."
1143+
'In the face of ambiguity, refuse the temptation to guess.'
11441144
raise AmbiguousIndexError with integer labels?
11451145
- No, prefer label-based indexing
11461146
"""

pandas/core/ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
from pandas.compat import bind_method
2222
import pandas.core.missing as missing
2323

24-
from pandas.core.common import (_values_from_object, _maybe_match_name,
25-
PerformanceWarning)
24+
from pandas.errors import PerformanceWarning
25+
from pandas.core.common import _values_from_object, _maybe_match_name
2626
from pandas.types.missing import notnull, isnull
2727
from pandas.types.common import (needs_i8_conversion,
2828
is_datetimelike_v_numeric,

pandas/core/panel.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from pandas import compat
2222
from pandas.compat import (map, zip, range, u, OrderedDict, OrderedDefaultdict)
2323
from pandas.compat.numpy import function as nv
24-
from pandas.core.common import PandasError, _try_sort, _default_index
24+
from pandas.core.common import _try_sort, _default_index
2525
from pandas.core.frame import DataFrame
2626
from pandas.core.generic import NDFrame, _shared_docs
2727
from pandas.core.index import (Index, MultiIndex, _ensure_index,
@@ -174,7 +174,7 @@ def _init_data(self, data, copy, dtype, **kwargs):
174174
copy=False)
175175
copy = False
176176
else: # pragma: no cover
177-
raise PandasError('Panel constructor not properly called!')
177+
raise ValueError('Panel constructor not properly called!')
178178

179179
NDFrame.__init__(self, mgr, axes=axes, copy=copy, dtype=dtype)
180180

@@ -1150,8 +1150,8 @@ def _construct_return_type(self, result, axes=None):
11501150
return self._constructor_sliced(
11511151
result, **self._extract_axes_for_slice(self, axes))
11521152

1153-
raise PandasError('invalid _construct_return_type [self->%s] '
1154-
'[result->%s]' % (self, result))
1153+
raise ValueError('invalid _construct_return_type [self->%s] '
1154+
'[result->%s]' % (self, result))
11551155

11561156
def _wrap_result(self, result, axis):
11571157
axis = self._get_axis_name(axis)

0 commit comments

Comments
 (0)