From 18ba23512697ef1a4b1022fb2d317f1368304828 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 6 Feb 2019 10:44:38 -0800 Subject: [PATCH] modernize compat imports (#25192) --- pandas/compat/__init__.py | 11 ----------- pandas/compat/numpy/function.py | 2 +- pandas/core/base.py | 11 ++++++----- pandas/core/common.py | 3 ++- pandas/core/computation/ops.py | 11 ++++++----- pandas/core/computation/pytables.py | 5 +++-- pandas/core/computation/scope.py | 4 ++-- pandas/core/frame.py | 3 ++- pandas/core/groupby/groupby.py | 2 +- pandas/core/groupby/grouper.py | 2 +- pandas/core/panel.py | 3 ++- pandas/core/resample.py | 2 +- pandas/core/series.py | 3 ++- pandas/core/sparse/scipy_sparse.py | 4 +++- pandas/io/excel.py | 5 +++-- pandas/io/formats/html.py | 3 ++- pandas/io/packers.py | 2 +- pandas/tests/frame/test_constructors.py | 4 ++-- pandas/tests/frame/test_dtypes.py | 7 ++++--- pandas/tests/groupby/aggregate/test_aggregate.py | 3 +-- pandas/tests/groupby/test_groupby.py | 5 ++--- pandas/tests/internals/test_internals.py | 4 ++-- pandas/tests/io/json/test_pandas.py | 4 ++-- pandas/tests/io/msgpack/test_pack.py | 4 ++-- pandas/tests/resample/test_resample_api.py | 3 ++- pandas/tests/test_panel.py | 4 ++-- pandas/util/_decorators.py | 2 +- pandas/util/testing.py | 5 +++-- 28 files changed, 61 insertions(+), 60 deletions(-) diff --git a/pandas/compat/__init__.py b/pandas/compat/__init__.py index f9c659106a516d..d7ca7f8963f70d 100644 --- a/pandas/compat/__init__.py +++ b/pandas/compat/__init__.py @@ -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: @@ -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 @@ -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: diff --git a/pandas/compat/numpy/function.py b/pandas/compat/numpy/function.py index 417ddd0d8af179..f15783ad642b41 100644 --- a/pandas/compat/numpy/function.py +++ b/pandas/compat/numpy/function.py @@ -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) diff --git a/pandas/core/base.py b/pandas/core/base.py index 24695dd4cfd9c8..726266b39cdee3 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1,6 +1,7 @@ """ Base and utility classes for pandas objects. """ +from collections import OrderedDict import textwrap import warnings @@ -8,7 +9,7 @@ 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 @@ -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] @@ -444,14 +445,14 @@ 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: @@ -459,7 +460,7 @@ def _agg(arg, func): 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)) diff --git a/pandas/core/common.py b/pandas/core/common.py index 0e92a5056daae8..5b83cb344b1e70 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -5,6 +5,7 @@ """ import collections +from collections import OrderedDict from datetime import datetime, timedelta from functools import partial import inspect @@ -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 ( diff --git a/pandas/core/computation/ops.py b/pandas/core/computation/ops.py index 8c3218a976b6b6..5c70255982e549 100644 --- a/pandas/core/computation/ops.py +++ b/pandas/core/computation/ops.py @@ -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 @@ -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) @@ -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) @@ -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) diff --git a/pandas/core/computation/pytables.py b/pandas/core/computation/pytables.py index 00de29b07c75d5..678c1e678fe19e 100644 --- a/pandas/core/computation/pytables.py +++ b/pandas/core/computation/pytables.py @@ -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 @@ -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) diff --git a/pandas/core/computation/scope.py b/pandas/core/computation/scope.py index 33c5a1c2e0f0ad..e158bc8c568eb1 100644 --- a/pandas/core/computation/scope.py +++ b/pandas/core/computation/scope.py @@ -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 @@ -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, diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0c160d1a2bb549..9c2f000c26c797 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -13,6 +13,7 @@ from __future__ import division import collections +from collections import OrderedDict import functools import itertools import sys @@ -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 ( diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index bee806df824df8..bfb5ba4fc8c907 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -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 diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 260417bc0d5983..b0d7cf9d431ccc 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -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 ( diff --git a/pandas/core/panel.py b/pandas/core/panel.py index de535eeea4b5e1..16bcc17a6b4ea4 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -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 diff --git a/pandas/core/resample.py b/pandas/core/resample.py index fbddc9ff29ce94..42ca488572e369 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -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: diff --git a/pandas/core/series.py b/pandas/core/series.py index fb84a36d215e54..ae6da1d5f015b8 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3,6 +3,7 @@ """ from __future__ import division +from collections import OrderedDict from textwrap import dedent import warnings @@ -10,7 +11,7 @@ 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 diff --git a/pandas/core/sparse/scipy_sparse.py b/pandas/core/sparse/scipy_sparse.py index 8bb79d753e5f9c..5a39a1529a33ab 100644 --- a/pandas/core/sparse/scipy_sparse.py +++ b/pandas/core/sparse/scipy_sparse.py @@ -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 diff --git a/pandas/io/excel.py b/pandas/io/excel.py index 3d85ae7fd1f468..11e5e78fa3e80a 100644 --- a/pandas/io/excel.py +++ b/pandas/io/excel.py @@ -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 @@ -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 @@ -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 diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py index 2d8b40016c9af0..456583509565e3 100644 --- a/pandas/io/formats/html.py +++ b/pandas/io/formats/html.py @@ -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 diff --git a/pandas/io/packers.py b/pandas/io/packers.py index efe4e3a91c69c8..588d63d73515f8 100644 --- a/pandas/io/packers.py +++ b/pandas/io/packers.py @@ -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) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index b97f5e0b6edf97..a8a78b26e317c3 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2,6 +2,7 @@ from __future__ import print_function +from collections import OrderedDict from datetime import datetime, timedelta import functools import itertools @@ -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 diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index a9f8ab47b16de2..a8776c84b98ca8 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -2,6 +2,7 @@ from __future__ import print_function +from collections import OrderedDict from datetime import timedelta import numpy as np @@ -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), @@ -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_), @@ -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')), diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 62ec0555f9033b..9de8a088090090 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -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 diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 0bfc7ababd18af..f14c9dcdd8e421 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -1,15 +1,14 @@ # -*- coding: utf-8 -*- from __future__ import print_function -from collections import defaultdict +from collections import OrderedDict, defaultdict from datetime import datetime from decimal import Decimal import numpy as np import pytest -from pandas.compat import ( - OrderedDict, StringIO, lmap, lrange, lzip, map, range, zip) +from pandas.compat import StringIO, lmap, lrange, lzip, map, range, zip from pandas.errors import PerformanceWarning import pandas as pd diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index fe0706efdc4f8d..bda486411e01e3 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=W0102 - +from collections import OrderedDict from datetime import date, datetime from distutils.version import LooseVersion import itertools @@ -12,7 +12,7 @@ import pytest from pandas._libs.internals import BlockPlacement -from pandas.compat import OrderedDict, lrange, u, zip +from pandas.compat import lrange, u, zip import pandas as pd from pandas import ( diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 23c40276072d63..c5fcb9fb0f672f 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable-msg=W0612,E1101 +from collections import OrderedDict from datetime import timedelta import json import os @@ -7,8 +8,7 @@ import numpy as np import pytest -from pandas.compat import ( - OrderedDict, StringIO, is_platform_32bit, lrange, range) +from pandas.compat import StringIO, is_platform_32bit, lrange, range import pandas.util._test_decorators as td import pandas as pd diff --git a/pandas/tests/io/msgpack/test_pack.py b/pandas/tests/io/msgpack/test_pack.py index 8c82d0d2cf870d..078d9f4ceb649d 100644 --- a/pandas/tests/io/msgpack/test_pack.py +++ b/pandas/tests/io/msgpack/test_pack.py @@ -1,10 +1,10 @@ # coding: utf-8 - +from collections import OrderedDict import struct import pytest -from pandas.compat import OrderedDict, u +from pandas.compat import u from pandas import compat diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index a694942cc4c406..69acf4ba6bde8a 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -1,11 +1,12 @@ # pylint: disable=E1101 +from collections import OrderedDict from datetime import datetime import numpy as np import pytest -from pandas.compat import OrderedDict, range +from pandas.compat import range import pandas as pd from pandas import DataFrame, Series diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index 6b20acc8448294..1fd791fc81009c 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=W0612,E1101 - +from collections import OrderedDict from datetime import datetime import operator from warnings import catch_warnings, simplefilter @@ -8,7 +8,7 @@ import numpy as np import pytest -from pandas.compat import OrderedDict, StringIO, lrange, range, signature +from pandas.compat import StringIO, lrange, range, signature import pandas.util._test_decorators as td from pandas.core.dtypes.common import is_float_dtype diff --git a/pandas/util/_decorators.py b/pandas/util/_decorators.py index 2f7816e3a6d001..e92051ebbea9aa 100644 --- a/pandas/util/_decorators.py +++ b/pandas/util/_decorators.py @@ -4,7 +4,7 @@ import warnings from pandas._libs.properties import cache_readonly # noqa -from pandas.compat import PY2, callable, signature +from pandas.compat import PY2, signature def deprecate(name, alternative, version, alt_name=None, diff --git a/pandas/util/testing.py b/pandas/util/testing.py index f441dd20f39829..47bde267156ed6 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -1,5 +1,6 @@ from __future__ import division +from collections import Counter from contextlib import contextmanager from datetime import datetime from functools import wraps @@ -20,8 +21,8 @@ from pandas._libs import testing as _testing import pandas.compat as compat from pandas.compat import ( - PY2, PY3, Counter, callable, filter, httplib, lmap, lrange, lzip, map, - raise_with_traceback, range, string_types, u, unichr, zip) + PY2, PY3, filter, httplib, lmap, lrange, lzip, map, raise_with_traceback, + range, string_types, u, unichr, zip) from pandas.core.dtypes.common import ( is_bool, is_categorical_dtype, is_datetime64_dtype, is_datetime64tz_dtype,