From 48782a8f6de2564cff08517a3f6753734690b711 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 26 Oct 2018 07:23:40 -0500 Subject: [PATCH] REF: SparseArray imports (#23329) --- pandas/core/arrays/sparse.py | 76 ++++++++++++++++-------------------- pandas/core/series.py | 3 +- setup.cfg | 2 - 3 files changed, 35 insertions(+), 46 deletions(-) diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index 72527cfa5d12e..08c961935a990 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -2,57 +2,47 @@ SparseArray data structure """ from __future__ import division -# pylint: disable=E1101,E1103,W0231 -import re -import operator import numbers -import numpy as np +import operator +import re import warnings -import pandas as pd -from pandas.core.base import PandasObject +import numpy as np +import pandas._libs.sparse as splib +import pandas.core.algorithms as algos +import pandas.core.common as com +import pandas.io.formats.printing as printing from pandas import compat -from pandas.errors import PerformanceWarning +from pandas._libs import index as libindex, lib +from pandas._libs.sparse import BlockIndex, IntIndex +from pandas._libs.tslibs import NaT from pandas.compat.numpy import function as nv - from pandas.core.accessor import PandasDelegate, delegate_names from pandas.core.arrays import ExtensionArray, ExtensionOpsMixin -import pandas.core.common as com +from pandas.core.base import PandasObject from pandas.core.dtypes.base import ExtensionDtype +from pandas.core.dtypes.cast import ( + astype_nansafe, construct_1d_arraylike_from_scalar, find_common_type, + infer_dtype_from_scalar, maybe_convert_platform +) +from pandas.core.dtypes.common import ( + is_array_like, is_bool_dtype, is_datetime64_any_dtype, is_dtype_equal, + is_integer, is_list_like, is_object_dtype, is_scalar, is_string_dtype, + pandas_dtype +) from pandas.core.dtypes.dtypes import register_extension_dtype from pandas.core.dtypes.generic import ( - ABCSparseSeries, ABCSeries, ABCIndexClass + ABCIndexClass, ABCSeries, ABCSparseSeries ) -from pandas.core.dtypes.common import ( - is_datetime64_any_dtype, - is_integer, - is_object_dtype, - is_array_like, - pandas_dtype, - is_bool_dtype, - is_list_like, - is_string_dtype, - is_scalar, is_dtype_equal) -from pandas.core.dtypes.cast import ( - maybe_convert_platform, - astype_nansafe, find_common_type, infer_dtype_from_scalar, - construct_1d_arraylike_from_scalar) -from pandas.core.dtypes.missing import isna, notna, na_value_for_dtype +from pandas.core.dtypes.missing import isna, na_value_for_dtype, notna from pandas.core.missing import interpolate_2d - -import pandas._libs.sparse as splib -from pandas._libs.sparse import BlockIndex, IntIndex -from pandas._libs import index as libindex -from pandas._libs import lib -import pandas.core.algorithms as algos -import pandas.io.formats.printing as printing +from pandas.errors import PerformanceWarning # ---------------------------------------------------------------------------- # Dtype - @register_extension_dtype class SparseDtype(ExtensionDtype): """ @@ -620,7 +610,7 @@ def __array__(self, dtype=None, copy=True): if is_datetime64_any_dtype(self.sp_values.dtype): # However, we *do* special-case the common case of # a datetime64 with pandas NaT. - if fill_value is pd.NaT: + if fill_value is NaT: # Can't put pd.NaT in a datetime64[ns] fill_value = np.datetime64('NaT') try: @@ -710,7 +700,7 @@ def _null_fill_value(self): def _fill_value_matches(self, fill_value): if self._null_fill_value: - return pd.isna(fill_value) + return isna(fill_value) else: return self.fill_value == fill_value @@ -855,7 +845,7 @@ def _first_fill_value_loc(self): return np.searchsorted(diff, 2) + 1 def unique(self): - uniques = list(pd.unique(self.sp_values)) + uniques = list(algos.unique(self.sp_values)) fill_loc = self._first_fill_value_loc() if fill_loc >= 0: uniques.insert(fill_loc, self.fill_value) @@ -871,8 +861,8 @@ def factorize(self, na_sentinel=-1): # ExtensionArray.factorize -> Tuple[EA, EA] # Given that we have to return a dense array of labels, why bother # implementing an efficient factorize? - labels, uniques = pd.factorize(np.asarray(self), - na_sentinel=na_sentinel) + labels, uniques = algos.factorize(np.asarray(self), + na_sentinel=na_sentinel) uniques = SparseArray(uniques, dtype=self.dtype) return labels, uniques @@ -889,6 +879,8 @@ def value_counts(self, dropna=True): ------- counts : Series """ + from pandas import Index, Series + keys, counts = algos._value_counts_arraylike(self.sp_values, dropna=dropna) fcounts = self.sp_index.ngaps @@ -897,7 +889,7 @@ def value_counts(self, dropna=True): pass else: if self._null_fill_value: - mask = pd.isna(keys) + mask = isna(keys) else: mask = keys == self.fill_value @@ -907,9 +899,9 @@ def value_counts(self, dropna=True): keys = np.insert(keys, 0, self.fill_value) counts = np.insert(counts, 0, fcounts) - if not isinstance(keys, pd.Index): - keys = pd.Index(keys) - result = pd.Series(counts, index=keys) + if not isinstance(keys, ABCIndexClass): + keys = Index(keys) + result = Series(counts, index=keys) return result # -------- diff --git a/pandas/core/series.py b/pandas/core/series.py index d813d8430d9e9..d03a88ea78f6f 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -24,7 +24,7 @@ from pandas.compat.numpy import function as nv from pandas.core import base, generic from pandas.core.accessor import CachedAccessor -from pandas.core.arrays import ExtensionArray, period_array +from pandas.core.arrays import ExtensionArray, SparseArray, period_array from pandas.core.arrays.categorical import Categorical, CategoricalAccessor from pandas.core.arrays.sparse import SparseAccessor from pandas.core.config import get_option @@ -1367,7 +1367,6 @@ def to_sparse(self, kind='block', fill_value=None): """ # TODO: deprecate from pandas.core.sparse.series import SparseSeries - from pandas.core.arrays import SparseArray values = SparseArray(self, kind=kind, fill_value=fill_value) return SparseSeries( diff --git a/setup.cfg b/setup.cfg index f26eac0d2ae62..a5006d66868f6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -175,8 +175,6 @@ skip= pandas/core/reshape/merge.py, pandas/core/reshape/reshape.py, pandas/core/reshape/pivot.py, - pandas/core/sparse/array.py, - pandas/core/arrays/sparse.py, pandas/core/sparse/api.py, pandas/core/sparse/series.py, pandas/core/sparse/frame.py,