Skip to content

Commit afc277b

Browse files
committed
API: expose pandas.errors
move all exception / warning impls (and references) from pandas.core.common and pandas.io.common closes pandas-dev#14800
1 parent f60f04a commit afc277b

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

+287
-209
lines changed

doc/source/whatsnew/v0.20.0.txt

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

58+
59+
.. _whatsnew_0200.enhancements.errors:
60+
61+
pandas errors
62+
^^^^^^^^^^^^^
63+
64+
We are adding a standard public location for all pandas exceptions & warnings ``pandas.errors``. (:issue:`14800`). Previously
65+
these exceptions & warnings could be imported from ``pandas.core.common`` or ``pandas.io.common``. These exceptions and warnings
66+
will be removed from the ``*.common`` locations in a future release. (:issue:`15541`)
67+
68+
The following are now part of this API:
69+
70+
.. ipython:: python
71+
72+
import pprint
73+
from pandas import errors
74+
excs = [ e for e in dir(errors) if not e.startswith('_') ]
75+
pprint.pprint(excs)
76+
5877
.. _whatsnew_0200.enhancements.groupby_access:
5978

6079
Groupby Enhancements

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

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

23+
# compat
24+
from pandas.errors import ( # noqa
25+
PandasError, PerformanceWarning,
26+
AmbiguousIndexError, UnsupportedFunctionCall, UnsortedIndexError)
27+
2328
# back-compat of public API
2429
# deprecate these functions
2530
m = sys.modules['pandas.core.common']
@@ -73,14 +78,6 @@ def array_equivalent(*args, **kwargs):
7378
return missing.array_equivalent(*args, **kwargs)
7479

7580

76-
class PandasError(Exception):
77-
pass
78-
79-
80-
class PerformanceWarning(Warning):
81-
pass
82-
83-
8481
class SettingWithCopyError(ValueError):
8582
pass
8683

@@ -89,24 +86,6 @@ class SettingWithCopyWarning(Warning):
8986
pass
9087

9188

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

pandas/core/frame.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
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.errors import PandasError
60+
from pandas.core.common import (_try_sort,
6061
_default_index,
6162
_values_from_object,
6263
_maybe_box_datetimelike,

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
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.errors import PandasError
25+
from pandas.core.common import _try_sort, _default_index
2526
from pandas.core.frame import DataFrame
2627
from pandas.core.generic import NDFrame, _shared_docs
2728
from pandas.core.index import (Index, MultiIndex, _ensure_index,

pandas/errors/__init__.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# flake8: noqa
2+
3+
""" expose public exceptions & warnings """
4+
5+
from pandas._libs.tslib import OutOfBoundsDatetime
6+
7+
8+
class PandasError(Exception):
9+
pass
10+
11+
12+
class PerformanceWarning(Warning):
13+
pass
14+
15+
class AmbiguousIndexError(PandasError, KeyError):
16+
pass
17+
18+
19+
class UnsupportedFunctionCall(ValueError):
20+
pass
21+
22+
23+
class UnsortedIndexError(KeyError):
24+
""" Error raised when attempting to get a slice of a MultiIndex
25+
and the index has not been lexsorted. Subclass of `KeyError`.
26+
27+
.. versionadded:: 0.20.0
28+
29+
"""
30+
pass
31+
32+
33+
class ParserError(ValueError):
34+
"""
35+
Exception that is thrown by an error is encountered in `pd.read_csv`
36+
"""
37+
pass
38+
39+
40+
class DtypeWarning(Warning):
41+
"""
42+
Warning that is raised whenever `pd.read_csv` encounters non-
43+
uniform dtypes in a column(s) of a given CSV file
44+
"""
45+
pass
46+
47+
48+
class EmptyDataError(ValueError):
49+
"""
50+
Exception that is thrown in `pd.read_csv` (by both the C and
51+
Python engines) when empty data or header is encountered
52+
"""
53+
pass
54+
55+
56+
class ParserWarning(Warning):
57+
"""
58+
Warning that is raised in `pd.read_csv` whenever it is necessary
59+
to change parsers (generally from 'c' to 'python') contrary to the
60+
one specified by the user due to lack of support or functionality for
61+
parsing particular attributes of a CSV file with the requsted engine
62+
"""
63+
pass

pandas/indexes/multi.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
is_list_like,
2020
is_scalar)
2121
from pandas.types.missing import isnull, array_equivalent
22+
from pandas.errors import PerformanceWarning, UnsortedIndexError
2223
from pandas.core.common import (_values_from_object,
2324
is_bool_indexer,
24-
is_null_slice,
25-
PerformanceWarning,
26-
UnsortedIndexError)
27-
25+
is_null_slice)
2826

2927
import pandas.core.base as base
3028
from pandas.util.decorators import (Appender, cache_readonly,

pandas/io/common.py

+8-37
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
from pandas.core.common import AbstractMethodError
1313
from pandas.types.common import is_number
1414

15+
# compat
16+
from pandas.errors import (ParserError, DtypeWarning, # noqa
17+
EmptyDataError, ParserWarning)
18+
19+
# gh-12665: Alias for now and remove later.
20+
CParserError = ParserError
21+
22+
1523
try:
1624
from s3fs import S3File
1725
need_text_wrapping = (BytesIO, S3File)
@@ -69,43 +77,6 @@ def urlopen(*args, **kwargs):
6977
_VALID_URLS.discard('')
7078

7179

72-
class ParserError(ValueError):
73-
"""
74-
Exception that is thrown by an error is encountered in `pd.read_csv`
75-
"""
76-
pass
77-
78-
79-
# gh-12665: Alias for now and remove later.
80-
CParserError = ParserError
81-
82-
83-
class DtypeWarning(Warning):
84-
"""
85-
Warning that is raised whenever `pd.read_csv` encounters non-
86-
uniform dtypes in a column(s) of a given CSV file
87-
"""
88-
pass
89-
90-
91-
class EmptyDataError(ValueError):
92-
"""
93-
Exception that is thrown in `pd.read_csv` (by both the C and
94-
Python engines) when empty data or header is encountered
95-
"""
96-
pass
97-
98-
99-
class ParserWarning(Warning):
100-
"""
101-
Warning that is raised in `pd.read_csv` whenever it is necessary
102-
to change parsers (generally from 'c' to 'python') contrary to the
103-
one specified by the user due to lack of support or functionality for
104-
parsing particular attributes of a CSV file with the requsted engine
105-
"""
106-
pass
107-
108-
10980
class BaseIterator(object):
11081
"""Subclass this and provide a "__next__()" method to obtain an iterator.
11182
Useful only when the object being iterated is non-reusable (e.g. OK for a

pandas/io/excel.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
from pandas.core.frame import DataFrame
1717
from pandas.io.parsers import TextParser
18+
from pandas.errors import EmptyDataError
1819
from pandas.io.common import (_is_url, _urlopen, _validate_header_arg,
19-
EmptyDataError, get_filepath_or_buffer,
20-
_NA_VALUES)
20+
get_filepath_or_buffer, _NA_VALUES)
2121
from pandas.tseries.period import Period
2222
from pandas.io.json import libjson
2323
from pandas.compat import (map, zip, reduce, range, lrange, u, add_metaclass,

pandas/io/html.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import numpy as np
1414

1515
from pandas.types.common import is_list_like
16-
from pandas.io.common import (EmptyDataError, _is_url, urlopen,
16+
from pandas.errors import EmptyDataError
17+
from pandas.io.common import (_is_url, urlopen,
1718
parse_url, _validate_header_arg)
1819
from pandas.io.parsers import TextParser
1920
from pandas.compat import (lrange, lmap, u, string_types, iteritems,

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.errors 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

pandas/io/parsers.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
from pandas.core import algorithms
3030
from pandas.core.common import AbstractMethodError
3131
from pandas.io.date_converters import generic_parser
32+
from pandas.errors import ParserWarning, ParserError, EmptyDataError
3233
from pandas.io.common import (get_filepath_or_buffer, _validate_header_arg,
3334
_get_handle, UnicodeReader, UTF8Recoder,
34-
BaseIterator, ParserError, EmptyDataError,
35-
ParserWarning, _NA_VALUES, _infer_compression)
35+
BaseIterator,
36+
_NA_VALUES, _infer_compression)
3637
from pandas.tseries import tools
3738

3839
from pandas.util.decorators import Appender

pandas/io/parsers.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ from cpython cimport (PyObject, PyBytes_FromString,
1313
PyUnicode_Check, PyUnicode_AsUTF8String,
1414
PyErr_Occurred, PyErr_Fetch)
1515
from cpython.ref cimport PyObject, Py_XDECREF
16-
from pandas.io.common import (ParserError, DtypeWarning,
17-
EmptyDataError, ParserWarning)
16+
from pandas.errors import (ParserError, DtypeWarning,
17+
EmptyDataError, ParserWarning)
1818

1919
# Import CParserError as alias of ParserError for backwards compatibility.
2020
# Ultimately, we want to remove this import. See gh-12665 and gh-14479.
21-
from pandas.io.common import CParserError
21+
CParserError = ParserError
2222

2323
cdef extern from "Python.h":
2424
object PyUnicode_FromString(char *v)

pandas/io/pytables.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
from pandas.sparse.array import BlockIndex, IntIndex
3333
from pandas.core.base import StringMixin
3434
from pandas.formats.printing import adjoin, pprint_thing
35-
from pandas.core.common import _asarray_tuplesafe, PerformanceWarning
35+
from pandas.errors import PerformanceWarning
36+
from pandas.core.common import _asarray_tuplesafe
3637
from pandas.core.algorithms import match, unique
3738
from pandas.core.categorical import Categorical, _factorize_from_iterables
3839
from pandas.core.internals import (BlockManager, make_block,

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 *

0 commit comments

Comments
 (0)