Skip to content

CLN: Remove collections.abc classes from pandas.compat #25957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ fi
if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then

# Check for imports from pandas.core.common instead of `import pandas.core.common as com`
# Check for imports from collections.abc instead of `from collections import abc`
MSG='Check for non-standard imports' ; echo $MSG
invgrep -R --include="*.py*" -E "from pandas.core.common import " pandas
invgrep -R --include="*.py*" -E "from collections.abc import " pandas
# invgrep -R --include="*.py*" -E "from numpy import nan " pandas # GH#24822 not yet implemented since the offending imports have not all been removed
RET=$(($RET + $?)) ; echo $MSG "DONE"

Expand Down
20 changes: 0 additions & 20 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import struct
import inspect
from collections import namedtuple
import collections

PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] >= 3
Expand Down Expand Up @@ -106,16 +105,6 @@ def lmap(*args, **kwargs):

def lfilter(*args, **kwargs):
return list(filter(*args, **kwargs))

Hashable = collections.abc.Hashable
Iterable = collections.abc.Iterable
Iterator = collections.abc.Iterator
Mapping = collections.abc.Mapping
MutableMapping = collections.abc.MutableMapping
Sequence = collections.abc.Sequence
Sized = collections.abc.Sized
Set = collections.abc.Set

else:
# Python 2
_name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
Expand All @@ -138,15 +127,6 @@ def signature(f):
lmap = builtins.map
lfilter = builtins.filter

Hashable = collections.Hashable
Iterable = collections.Iterable
Iterator = collections.Iterator
Mapping = collections.Mapping
MutableMapping = collections.MutableMapping
Sequence = collections.Sequence
Sized = collections.Sized
Set = collections.Set

if PY2:
def iteritems(obj, **kw):
return obj.iteritems(**kw)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/sparse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
SparseArray data structure
"""
from collections import abc
import numbers
import operator
import re
Expand Down Expand Up @@ -1377,7 +1378,7 @@ def map(self, mapper):
if isinstance(mapper, ABCSeries):
mapper = mapper.to_dict()

if isinstance(mapper, compat.Mapping):
if isinstance(mapper, abc.Mapping):
fill_value = mapper.get(self.fill_value, self.fill_value)
sp_values = [mapper.get(x, None) for x in self.sp_values]
else:
Expand Down
13 changes: 6 additions & 7 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

import collections
from collections import OrderedDict
from collections import OrderedDict, abc
from datetime import datetime, timedelta
from functools import partial
import inspect
Expand All @@ -14,7 +14,6 @@
import numpy as np

from pandas._libs import lib, tslibs
import pandas.compat as compat
from pandas.compat import PY36, iteritems

from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
Expand Down Expand Up @@ -374,13 +373,13 @@ def standardize_mapping(into):

Parameters
----------
into : instance or subclass of collections.Mapping
into : instance or subclass of collections.abc.Mapping
Must be a class, an initialized collections.defaultdict,
or an instance of a collections.Mapping subclass.
or an instance of a collections.abc.Mapping subclass.

Returns
-------
mapping : a collections.Mapping subclass or other constructor
mapping : a collections.abc.Mapping subclass or other constructor
a callable object that can accept an iterator to create
the desired Mapping.

Expand All @@ -394,7 +393,7 @@ def standardize_mapping(into):
return partial(
collections.defaultdict, into.default_factory)
into = type(into)
if not issubclass(into, compat.Mapping):
if not issubclass(into, abc.Mapping):
raise TypeError('unsupported type: {into}'.format(into=into))
elif into == collections.defaultdict:
raise TypeError(
Expand Down Expand Up @@ -471,7 +470,7 @@ def _get_rename_function(mapper):
Returns a function that will map names/labels, dependent if mapper
is a dict, Series or just a function.
"""
if isinstance(mapper, (compat.Mapping, ABCSeries)):
if isinstance(mapper, (abc.Mapping, ABCSeries)):

def f(x):
if x in mapper:
Expand Down
23 changes: 11 additions & 12 deletions pandas/core/dtypes/inference.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
""" basic inference routines """

from collections import abc
from numbers import Number
import re

import numpy as np

from pandas._libs import lib
from pandas.compat import PY2, Set, re_type

from pandas import compat
from pandas.compat import PY2, re_type

is_bool = lib.is_bool

Expand Down Expand Up @@ -113,7 +112,7 @@ def _iterable_not_string(obj):
False
"""

return isinstance(obj, compat.Iterable) and not isinstance(obj, str)
return isinstance(obj, abc.Iterable) and not isinstance(obj, str)


def is_iterator(obj):
Expand Down Expand Up @@ -288,15 +287,15 @@ def is_list_like(obj, allow_sets=True):
False
"""

return (isinstance(obj, compat.Iterable) and
return (isinstance(obj, abc.Iterable) and
# we do not count strings/unicode/bytes as list-like
not isinstance(obj, (str, bytes)) and

# exclude zero-dimensional numpy arrays, effectively scalars
not (isinstance(obj, np.ndarray) and obj.ndim == 0) and

# exclude sets if allow_sets is False
not (allow_sets is False and isinstance(obj, Set)))
not (allow_sets is False and isinstance(obj, abc.Set)))


def is_array_like(obj):
Expand Down Expand Up @@ -436,23 +435,23 @@ def is_named_tuple(obj):
def is_hashable(obj):
"""Return True if hash(obj) will succeed, False otherwise.

Some types will pass a test against collections.Hashable but fail when they
are actually hashed with hash().
Some types will pass a test against collections.abc.Hashable but fail when
they are actually hashed with hash().

Distinguish between these and other types by trying the call to hash() and
seeing if they raise TypeError.

Examples
--------
>>> a = ([],)
>>> isinstance(a, collections.Hashable)
>>> isinstance(a, collections.abc.Hashable)
True
>>> is_hashable(a)
False
"""
# Unfortunately, we can't use isinstance(obj, collections.Hashable), which
# can be faster than calling hash. That is because numpy scalars on Python
# 3 fail this test.
# Unfortunately, we can't use isinstance(obj, collections.abc.Hashable),
# which can be faster than calling hash. That is because numpy scalars
# fail this test.

# Reconsider this decision once this numpy bug is fixed:
# https://github.com/numpy/numpy/issues/5562
Expand Down
21 changes: 10 additions & 11 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
labeling information
"""
import collections
from collections import OrderedDict
from collections import OrderedDict, abc
import functools
import itertools
import sys
Expand All @@ -33,8 +33,7 @@
validate_axis_style_args)

from pandas import compat
from pandas.compat import (
PY36, Iterator, StringIO, lmap, lzip, raise_with_traceback)
from pandas.compat import PY36, StringIO, lmap, lzip, raise_with_traceback
from pandas.compat.numpy import function as nv
from pandas.core.dtypes.cast import (
maybe_upcast,
Expand Down Expand Up @@ -426,9 +425,9 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
copy=copy)

# For data is list-like, or Iterable (will consume into list)
elif (isinstance(data, compat.Iterable) and
elif (isinstance(data, abc.Iterable) and
not isinstance(data, (str, bytes))):
if not isinstance(data, compat.Sequence):
if not isinstance(data, abc.Sequence):
data = list(data)
if len(data) > 0:
if is_list_like(data[0]) and getattr(data[0], 'ndim', 1) == 1:
Expand Down Expand Up @@ -1203,7 +1202,7 @@ def to_dict(self, orient='dict', into=dict):
indicates `split`.

into : class, default dict
The collections.Mapping subclass used for all Mappings
The collections.abc.Mapping subclass used for all Mappings
in the return value. Can be the actual class or an empty
instance of the mapping type you want. If you want a
collections.defaultdict, you must pass it initialized.
Expand All @@ -1212,8 +1211,8 @@ def to_dict(self, orient='dict', into=dict):

Returns
-------
dict, list or collections.Mapping
Return a collections.Mapping object representing the DataFrame.
dict, list or collections.abc.Mapping
Return a collections.abc.Mapping object representing the DataFrame.
The resulting transformation depends on the `orient` parameter.

See Also
Expand Down Expand Up @@ -4080,7 +4079,7 @@ def set_index(self, keys, drop=True, append=False, inplace=False,
the same length as the calling DataFrame, or a list containing an
arbitrary combination of column keys and arrays. Here, "array"
encompasses :class:`Series`, :class:`Index`, ``np.ndarray``, and
instances of :class:`abc.Iterator`.
instances of :class:`~collections.abc.Iterator`.
drop : bool, default True
Delete columns to be used as the new index.
append : bool, default False
Expand Down Expand Up @@ -4166,7 +4165,7 @@ def set_index(self, keys, drop=True, append=False, inplace=False,
missing = []
for col in keys:
if isinstance(col, (ABCIndexClass, ABCSeries, np.ndarray,
list, Iterator)):
list, abc.Iterator)):
# arrays are fine as long as they are one-dimensional
# iterators get converted to list below
if getattr(col, 'ndim', 1) != 1:
Expand Down Expand Up @@ -4213,7 +4212,7 @@ def set_index(self, keys, drop=True, append=False, inplace=False,
elif isinstance(col, (list, np.ndarray)):
arrays.append(col)
names.append(None)
elif isinstance(col, Iterator):
elif isinstance(col, abc.Iterator):
arrays.append(list(col))
names.append(None)
# from here, col can only be a column label
Expand Down
14 changes: 7 additions & 7 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
which here returns a DataFrameGroupBy object.
"""

import collections
from collections import OrderedDict, abc
import copy
from functools import partial
from textwrap import dedent
Expand Down Expand Up @@ -226,7 +226,7 @@ def _aggregate_generic(self, func, *args, **kwargs):
axis = self.axis
obj = self._obj_with_exclusions

result = collections.OrderedDict()
result = OrderedDict()
if axis != obj._info_axis_number:
try:
for name, data in self:
Expand All @@ -253,7 +253,7 @@ def _aggregate_item_by_item(self, func, *args, **kwargs):
# only for axis==0

obj = self._obj_with_exclusions
result = collections.OrderedDict()
result = OrderedDict()
cannot_agg = []
errors = None
for item in obj:
Expand Down Expand Up @@ -770,7 +770,7 @@ def aggregate(self, func_or_funcs, *args, **kwargs):
if isinstance(func_or_funcs, str):
return getattr(self, func_or_funcs)(*args, **kwargs)

if isinstance(func_or_funcs, compat.Iterable):
if isinstance(func_or_funcs, abc.Iterable):
# Catch instances of lists / tuples
# but not the class list / tuple itself.
ret = self._aggregate_multiple_funcs(func_or_funcs,
Expand Down Expand Up @@ -834,7 +834,7 @@ def _aggregate_multiple_funcs(self, arg, _level):
columns.append(com.get_callable_name(f))
arg = lzip(columns, arg)

results = collections.OrderedDict()
results = OrderedDict()
for name, func in arg:
obj = self
if name in results:
Expand Down Expand Up @@ -911,7 +911,7 @@ def _get_index():
name=self._selection_name)

def _aggregate_named(self, func, *args, **kwargs):
result = collections.OrderedDict()
result = OrderedDict()

for name, group in self:
group.name = name
Expand Down Expand Up @@ -1507,7 +1507,7 @@ def _apply_to_column_groupbys(self, func):
def _fill(self, direction, limit=None):
"""Overridden method to join grouped columns in output"""
res = super(DataFrameGroupBy, self)._fill(direction, limit=limit)
output = collections.OrderedDict(
output = OrderedDict(
(grp.name, grp.grouper) for grp in self.grouper.groupings)

from pandas import concat
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
Functions for preparing various inputs passed to the DataFrame or Series
constructors before passing them to a BlockManager.
"""
from collections import OrderedDict
from collections import OrderedDict, abc

import numpy as np
import numpy.ma as ma

from pandas._libs import lib
from pandas._libs.tslibs import IncompatibleFrequency
import pandas.compat as compat
from pandas.compat import lmap, lrange, raise_with_traceback

from pandas.core.dtypes.cast import (
Expand Down Expand Up @@ -395,7 +394,7 @@ def to_arrays(data, columns, coerce_float=False, dtype=None):
if isinstance(data[0], (list, tuple)):
return _list_to_arrays(data, columns, coerce_float=coerce_float,
dtype=dtype)
elif isinstance(data[0], compat.Mapping):
elif isinstance(data[0], abc.Mapping):
return _list_of_dict_to_arrays(data, columns,
coerce_float=coerce_float, dtype=dtype)
elif isinstance(data[0], ABCSeries):
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Data structure for 1-dimensional cross-sectional and time series data
"""
from collections import OrderedDict
from collections import OrderedDict, abc
from shutil import get_terminal_size
from textwrap import dedent
import warnings
Expand Down Expand Up @@ -224,8 +224,8 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
raise TypeError("{0!r} type is unordered"
"".format(data.__class__.__name__))
# If data is Iterable but not list-like, consume into list.
elif (isinstance(data, compat.Iterable)
and not isinstance(data, compat.Sized)):
elif (isinstance(data, abc.Iterable) and
not isinstance(data, abc.Sized)):
data = list(data)
else:

Expand Down Expand Up @@ -1496,7 +1496,7 @@ def to_dict(self, into=dict):
Parameters
----------
into : class, default dict
The collections.Mapping subclass to use as the return
The collections.abc.Mapping subclass to use as the return
object. Can be the actual class or an empty
instance of the mapping type you want. If you want a
collections.defaultdict, you must pass it initialized.
Expand All @@ -1505,7 +1505,7 @@ def to_dict(self, into=dict):

Returns
-------
collections.Mapping
collections.abc.Mapping
Key-value representation of Series.

Examples
Expand Down
Loading