Skip to content

Commit

Permalink
modernize compat imports (#25192)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Feb 6, 2019
1 parent 93568cc commit 638ddeb
Show file tree
Hide file tree
Showing 28 changed files with 61 additions and 60 deletions.
11 changes: 0 additions & 11 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* lists: lrange(), lmap(), lzip(), lfilter()
* unicode: u() [no unicode builtin in Python 3]
* longs: long (int in Python 3)
* callable
* iterable method compatibility: iteritems, iterkeys, itervalues
* Uses the original method if available, otherwise uses items, keys, values.
* types:
Expand Down Expand Up @@ -378,14 +377,6 @@ class ResourceWarning(Warning):
string_and_binary_types = string_types + (binary_type,)


try:
# callable reintroduced in later versions of Python
callable = callable
except NameError:
def callable(obj):
return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)


if PY2:
# In PY2 functools.wraps doesn't provide metadata pytest needs to generate
# decorated tests using parametrization. See pytest GH issue #2782
Expand All @@ -411,8 +402,6 @@ def wrapper(cls):
return metaclass(cls.__name__, cls.__bases__, orig_vars)
return wrapper

from collections import OrderedDict, Counter

if PY3:
def raise_with_traceback(exc, traceback=Ellipsis):
if traceback == Ellipsis:
Expand Down
2 changes: 1 addition & 1 deletion pandas/compat/numpy/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
and methods that are spread throughout the codebase. This module will make it
easier to adjust to future upstream changes in the analogous numpy signatures.
"""
from collections import OrderedDict

from numpy import ndarray

from pandas.compat import OrderedDict
from pandas.errors import UnsupportedFunctionCall
from pandas.util._validators import (
validate_args, validate_args_and_kwargs, validate_kwargs)
Expand Down
11 changes: 6 additions & 5 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""
Base and utility classes for pandas objects.
"""
from collections import OrderedDict
import textwrap
import warnings

import numpy as np

import pandas._libs.lib as lib
import pandas.compat as compat
from pandas.compat import PYPY, OrderedDict, builtins, map, range
from pandas.compat import PYPY, builtins, map, range
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import Appender, Substitution, cache_readonly
Expand Down Expand Up @@ -376,7 +377,7 @@ def nested_renaming_depr(level=4):
# eg. {'A' : ['mean']}, normalize all to
# be list-likes
if any(is_aggregator(x) for x in compat.itervalues(arg)):
new_arg = compat.OrderedDict()
new_arg = OrderedDict()
for k, v in compat.iteritems(arg):
if not isinstance(v, (tuple, list, dict)):
new_arg[k] = [v]
Expand Down Expand Up @@ -444,22 +445,22 @@ def _agg(arg, func):
run the aggregations over the arg with func
return an OrderedDict
"""
result = compat.OrderedDict()
result = OrderedDict()
for fname, agg_how in compat.iteritems(arg):
result[fname] = func(fname, agg_how)
return result

# set the final keys
keys = list(compat.iterkeys(arg))
result = compat.OrderedDict()
result = OrderedDict()

# nested renamer
if is_nested_renamer:
result = list(_agg(arg, _agg_1dim).values())

if all(isinstance(r, dict) for r in result):

result, results = compat.OrderedDict(), result
result, results = OrderedDict(), result
for r in results:
result.update(r)
keys = list(compat.iterkeys(result))
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import collections
from collections import OrderedDict
from datetime import datetime, timedelta
from functools import partial
import inspect
Expand All @@ -13,7 +14,7 @@

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

from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
from pandas.core.dtypes.common import (
Expand Down
11 changes: 6 additions & 5 deletions pandas/core/computation/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

import numpy as np

from pandas._libs.tslibs import Timestamp
from pandas.compat import PY3, string_types, text_type

from pandas.core.dtypes.common import is_list_like, is_scalar

import pandas as pd
from pandas.core.base import StringMixin
import pandas.core.common as com
from pandas.core.computation.common import _ensure_decoded, _result_type_many
Expand Down Expand Up @@ -399,8 +399,9 @@ def evaluate(self, env, engine, parser, term_type, eval_in_python):
if self.op in eval_in_python:
res = self.func(left.value, right.value)
else:
res = pd.eval(self, local_dict=env, engine=engine,
parser=parser)
from pandas.core.computation.eval import eval
res = eval(self, local_dict=env, engine=engine,
parser=parser)

name = env.add_tmp(res)
return term_type(name, env=env)
Expand All @@ -422,7 +423,7 @@ def stringify(value):
v = rhs.value
if isinstance(v, (int, float)):
v = stringify(v)
v = pd.Timestamp(_ensure_decoded(v))
v = Timestamp(_ensure_decoded(v))
if v.tz is not None:
v = v.tz_convert('UTC')
self.rhs.update(v)
Expand All @@ -431,7 +432,7 @@ def stringify(value):
v = lhs.value
if isinstance(v, (int, float)):
v = stringify(v)
v = pd.Timestamp(_ensure_decoded(v))
v = Timestamp(_ensure_decoded(v))
if v.tz is not None:
v = v.tz_convert('UTC')
self.lhs.update(v)
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np

from pandas._libs.tslibs import Timedelta, Timestamp
from pandas.compat import DeepChainMap, string_types, u

from pandas.core.dtypes.common import is_list_like
Expand Down Expand Up @@ -185,12 +186,12 @@ def stringify(value):
if isinstance(v, (int, float)):
v = stringify(v)
v = _ensure_decoded(v)
v = pd.Timestamp(v)
v = Timestamp(v)
if v.tz is not None:
v = v.tz_convert('UTC')
return TermValue(v, v.value, kind)
elif kind == u('timedelta64') or kind == u('timedelta'):
v = pd.Timedelta(v, unit='s').value
v = Timedelta(v, unit='s').value
return TermValue(int(v), v, kind)
elif meta == u('category'):
metadata = com.values_from_object(self.metadata)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/computation/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

import numpy as np

from pandas._libs.tslibs import Timestamp
from pandas.compat import DeepChainMap, StringIO, map

import pandas as pd # noqa
from pandas.core.base import StringMixin
import pandas.core.computation as compu

Expand Down Expand Up @@ -48,7 +48,7 @@ def _raw_hex_id(obj):


_DEFAULT_GLOBALS = {
'Timestamp': pd._libs.tslib.Timestamp,
'Timestamp': Timestamp,
'datetime': datetime.datetime,
'True': True,
'False': False,
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from __future__ import division

import collections
from collections import OrderedDict
import functools
import itertools
import sys
Expand All @@ -33,7 +34,7 @@

from pandas import compat
from pandas.compat import (range, map, zip, lmap, lzip, StringIO, u,
OrderedDict, PY36, raise_with_traceback,
PY36, raise_with_traceback,
string_and_binary_types)
from pandas.compat.numpy import function as nv
from pandas.core.dtypes.cast import (
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class providing the base-class of operations.

from pandas._libs import Timestamp, groupby as libgroupby
import pandas.compat as compat
from pandas.compat import callable, range, set_function_name, zip
from pandas.compat import range, set_function_name, zip
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import Appender, Substitution, cache_readonly
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np

import pandas.compat as compat
from pandas.compat import callable, zip
from pandas.compat import zip
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.common import (
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
# pylint: disable=E1103,W0231,W0212,W0621
from __future__ import division

from collections import OrderedDict
import warnings

import numpy as np

import pandas.compat as compat
from pandas.compat import OrderedDict, map, range, u, zip
from pandas.compat import map, range, u, zip
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution, deprecate_kwarg
from pandas.util._validators import validate_axis_style_args
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
grouped = groupby(obj, by=None, grouper=grouper, axis=self.axis)

try:
if isinstance(obj, ABCDataFrame) and compat.callable(how):
if isinstance(obj, ABCDataFrame) and callable(how):
# Check if the function is reducing or not.
result = grouped._aggregate_item_by_item(how, *args, **kwargs)
else:
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"""
from __future__ import division

from collections import OrderedDict
from textwrap import dedent
import warnings

import numpy as np

from pandas._libs import iNaT, index as libindex, lib, tslibs
import pandas.compat as compat
from pandas.compat import PY36, OrderedDict, StringIO, u, zip
from pandas.compat import PY36, StringIO, u, zip
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution, deprecate
from pandas.util._validators import validate_bool_kwarg
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/sparse/scipy_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
Currently only includes SparseSeries.to_coo helpers.
"""
from pandas.compat import OrderedDict, lmap
from collections import OrderedDict

from pandas.compat import lmap

from pandas.core.index import Index, MultiIndex
from pandas.core.series import Series
Expand Down
5 changes: 3 additions & 2 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# ---------------------------------------------------------------------
# ExcelFile class
import abc
from collections import OrderedDict
from datetime import date, datetime, time, timedelta
from distutils.version import LooseVersion
from io import UnsupportedOperation
Expand All @@ -17,7 +18,7 @@
import pandas._libs.json as json
import pandas.compat as compat
from pandas.compat import (
OrderedDict, add_metaclass, lrange, map, range, string_types, u, zip)
add_metaclass, lrange, map, range, string_types, u, zip)
from pandas.errors import EmptyDataError
from pandas.util._decorators import Appender, deprecate_kwarg

Expand Down Expand Up @@ -274,7 +275,7 @@ def register_writer(klass):
"""Adds engine to the excel writer registry. You must use this method to
integrate with ``to_excel``. Also adds config options for any new
``supported_extensions`` defined on the writer."""
if not compat.callable(klass):
if not callable(klass):
raise ValueError("Can only register callables as engines")
engine_name = klass.engine
_writers[engine_name] = klass
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

from __future__ import print_function

from collections import OrderedDict
from textwrap import dedent

from pandas.compat import OrderedDict, lzip, map, range, u, unichr, zip
from pandas.compat import lzip, map, range, u, unichr, zip

from pandas.core.dtypes.generic import ABCMultiIndex

Expand Down
2 changes: 1 addition & 1 deletion pandas/io/packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def read(fh):
finally:
if fh is not None:
fh.close()
elif hasattr(path_or_buf, 'read') and compat.callable(path_or_buf.read):
elif hasattr(path_or_buf, 'read') and callable(path_or_buf.read):
# treat as a buffer like
return read(path_or_buf)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import print_function

from collections import OrderedDict
from datetime import datetime, timedelta
import functools
import itertools
Expand All @@ -11,8 +12,7 @@
import pytest

from pandas.compat import (
PY3, PY36, OrderedDict, is_platform_little_endian, lmap, long, lrange,
lzip, range, zip)
PY3, PY36, is_platform_little_endian, lmap, long, lrange, lzip, range, zip)

from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
from pandas.core.dtypes.common import is_integer_dtype
Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/frame/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import print_function

from collections import OrderedDict
from datetime import timedelta

import numpy as np
Expand Down Expand Up @@ -66,7 +67,7 @@ def test_empty_frame_dtypes_ftypes(self):
assert_series_equal(norows_int_df.ftypes, pd.Series(
'int32:dense', index=list("abc")))

odict = compat.OrderedDict
odict = OrderedDict
df = pd.DataFrame(odict([('a', 1), ('b', True), ('c', 1.0)]),
index=[1, 2, 3])
ex_dtypes = pd.Series(odict([('a', np.int64),
Expand Down Expand Up @@ -100,7 +101,7 @@ def test_datetime_with_tz_dtypes(self):
def test_dtypes_are_correct_after_column_slice(self):
# GH6525
df = pd.DataFrame(index=range(5), columns=list("abc"), dtype=np.float_)
odict = compat.OrderedDict
odict = OrderedDict
assert_series_equal(df.dtypes,
pd.Series(odict([('a', np.float_),
('b', np.float_),
Expand Down Expand Up @@ -295,7 +296,7 @@ def test_select_dtypes_include_exclude_mixed_scalars_lists(self):

def test_select_dtypes_duplicate_columns(self):
# GH20839
odict = compat.OrderedDict
odict = OrderedDict
df = DataFrame(odict([('a', list('abc')),
('b', list(range(1, 4))),
('c', np.arange(3, 6).astype('u1')),
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
"""
test .agg behavior / note that .apply is tested generally in test_groupby.py
"""
from collections import OrderedDict

import numpy as np
import pytest

from pandas.compat import OrderedDict

import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series, concat
from pandas.core.base import SpecificationError
Expand Down
Loading

0 comments on commit 638ddeb

Please sign in to comment.