Skip to content

CLN: avoid runtime imports #27461

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 14 commits into from
Jul 22, 2019
Merged
14 changes: 7 additions & 7 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ def is_scalar(val: object) -> bool:

return (cnp.PyArray_IsAnyScalar(val)
# PyArray_IsAnyScalar is always False for bytearrays on Py3
or isinstance(val, (Fraction, Number))
# We differ from numpy, which claims that None is not scalar;
# see np.isscalar
or val is None
or PyDate_Check(val)
or PyDelta_Check(val)
or PyTime_Check(val)
# We differ from numpy, which claims that None is not scalar;
# see np.isscalar
or val is None
or isinstance(val, (Fraction, Number))
or util.is_period_object(val)
or is_decimal(val)
or is_interval(val)
Expand Down Expand Up @@ -1192,7 +1192,9 @@ def infer_dtype(value: object, skipna: object=None) -> str:
# e.g. categoricals
try:
values = getattr(value, '_values', getattr(value, 'values', value))
except:
except TypeError:
# This gets hit if we have an EA, since cython expects `values`
# to be an ndarray
value = _try_infer_map(value)
if value is not None:
return value
Expand All @@ -1208,8 +1210,6 @@ def infer_dtype(value: object, skipna: object=None) -> str:
construct_1d_object_array_from_listlike)
values = construct_1d_object_array_from_listlike(value)

values = getattr(values, 'values', values)

# make contiguous
values = values.ravel()

Expand Down
1 change: 0 additions & 1 deletion pandas/compat/pickle_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pickle as pkl
import sys

import pandas # noqa
from pandas import Index


Expand Down
8 changes: 1 addition & 7 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1977,12 +1977,6 @@ def diff(arr, n, axis=0):
out_arr[res_indexer] = arr[res_indexer] - arr[lag_indexer]

if is_timedelta:
from pandas import TimedeltaIndex

out_arr = (
TimedeltaIndex(out_arr.ravel().astype("int64"))
.asi8.reshape(out_arr.shape)
.astype("timedelta64[ns]")
)
out_arr = out_arr.astype("int64").view("timedelta64[ns]")

return out_arr
3 changes: 1 addition & 2 deletions pandas/core/arrays/array_.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ def array(
"""
from pandas.core.arrays import (
period_array,
ExtensionArray,
IntervalArray,
PandasArray,
DatetimeArray,
Expand All @@ -226,7 +225,7 @@ def array(

data = extract_array(data, extract_numpy=True)

if dtype is None and isinstance(data, ExtensionArray):
if dtype is None and isinstance(data, ABCExtensionArray):
dtype = data.dtype

# this returns None for not-found dtypes.
Expand Down
24 changes: 11 additions & 13 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pandas._config import get_option

from pandas._libs import algos as libalgos, lib
from pandas._libs import algos as libalgos, hashtable as htable, lib
from pandas.compat.numpy import function as nv
from pandas.util._decorators import (
Appender,
Expand Down Expand Up @@ -50,7 +50,14 @@
from pandas.core import ops
from pandas.core.accessor import PandasDelegate, delegate_names
import pandas.core.algorithms as algorithms
from pandas.core.algorithms import factorize, take, take_1d, unique1d
from pandas.core.algorithms import (
_get_data_algo,
_hashtables,
factorize,
take,
take_1d,
unique1d,
)
from pandas.core.base import NoNewAttributesMixin, PandasObject, _shared_docs
import pandas.core.common as com
from pandas.core.missing import interpolate_2d
Expand Down Expand Up @@ -1527,9 +1534,7 @@ def value_counts(self, dropna=True):
See Also
--------
Series.value_counts

"""
from numpy import bincount
from pandas import Series, CategoricalIndex

code, cat = self._codes, self.categories
Expand All @@ -1538,9 +1543,9 @@ def value_counts(self, dropna=True):

if dropna or clean:
obs = code if clean else code[mask]
count = bincount(obs, minlength=ncat or 0)
count = np.bincount(obs, minlength=ncat or 0)
else:
count = bincount(np.where(mask, code, ncat))
count = np.bincount(np.where(mask, code, ncat))
ix = np.append(ix, -1)

ix = self._constructor(ix, dtype=self.dtype, fastpath=True)
Expand Down Expand Up @@ -2329,9 +2334,6 @@ def mode(self, dropna=True):
-------
modes : `Categorical` (sorted)
"""

import pandas._libs.hashtable as htable

codes = self._codes
if dropna:
good = self._codes != -1
Expand Down Expand Up @@ -2671,8 +2673,6 @@ def _get_codes_for_values(values, categories):
"""
utility routine to turn values into codes given the specified categories
"""
from pandas.core.algorithms import _get_data_algo, _hashtables

dtype_equal = is_dtype_equal(values.dtype, categories.dtype)

if dtype_equal:
Expand Down Expand Up @@ -2722,8 +2722,6 @@ def _recode_for_categories(codes, old_categories, new_categories):
>>> _recode_for_categories(codes, old_cat, new_cat)
array([ 1, 0, 0, -1])
"""
from pandas.core.algorithms import take_1d

if len(old_categories) == 0:
# All null anyway, so just retain the nulls
return codes.copy()
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
is_unsigned_integer_dtype,
pandas_dtype,
)
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCIndexClass,
ABCPeriodArray,
ABCSeries,
)
from pandas.core.dtypes.inference import is_array_like
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna

Expand Down Expand Up @@ -1664,11 +1669,10 @@ def _ensure_datetimelike_to_i8(other, to_utc=False):
i8 1d array
"""
from pandas import Index
from pandas.core.arrays import PeriodArray

if lib.is_scalar(other) and isna(other):
return iNaT
elif isinstance(other, (PeriodArray, ABCIndexClass, DatetimeLikeArrayMixin)):
elif isinstance(other, (ABCPeriodArray, ABCIndexClass, DatetimeLikeArrayMixin)):
# convert tz if needed
if getattr(other, "tz", None) is not None:
if to_utc:
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pandas.core.dtypes.missing import isna, notna

from pandas.core import nanops, ops
from pandas.core.algorithms import take
from pandas.core.arrays import ExtensionArray, ExtensionOpsMixin
from pandas.core.tools.numeric import to_numeric

Expand Down Expand Up @@ -420,8 +421,6 @@ def __iter__(self):
yield self._data[i]

def take(self, indexer, allow_fill=False, fill_value=None):
from pandas.api.extensions import take

# we always fill with 1 internally
# to avoid upcasting
data_fill_value = 1 if isna(fill_value) else fill_value
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pandas.core.dtypes.dtypes import IntervalDtype
from pandas.core.dtypes.generic import (
ABCDatetimeIndex,
ABCIndexClass,
ABCInterval,
ABCIntervalIndex,
ABCPeriodIndex,
Expand All @@ -35,7 +36,7 @@
from pandas.core.arrays.base import ExtensionArray, _extension_array_shared_docs
from pandas.core.arrays.categorical import Categorical
import pandas.core.common as com
from pandas.core.indexes.base import Index, ensure_index
from pandas.core.indexes.base import ensure_index

_VALID_CLOSED = {"left", "right", "both", "neither"}
_interval_shared_docs = {}
Expand Down Expand Up @@ -510,7 +511,7 @@ def __getitem__(self, value):
right = self.right[value]

# scalar
if not isinstance(left, Index):
if not isinstance(left, ABCIndexClass):
if isna(left):
return self._fill_value
return Interval(left, right, self.closed)
Expand Down
9 changes: 2 additions & 7 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
from pandas.core.dtypes.inference import is_array_like, is_list_like
from pandas.core.dtypes.missing import isna

from pandas import compat
from pandas.core import nanops
from pandas.core.algorithms import searchsorted
from pandas.core.algorithms import searchsorted, take, unique
from pandas.core.missing import backfill_1d, pad_1d

from .base import ExtensionArray, ExtensionOpsMixin
Expand Down Expand Up @@ -249,8 +250,6 @@ def nbytes(self):
return self._ndarray.nbytes

def isna(self):
from pandas import isna

return isna(self._ndarray)

def fillna(self, value=None, method=None, limit=None):
Expand Down Expand Up @@ -281,8 +280,6 @@ def fillna(self, value=None, method=None, limit=None):
return new_values

def take(self, indices, allow_fill=False, fill_value=None):
from pandas.core.algorithms import take

result = take(
self._ndarray, indices, allow_fill=allow_fill, fill_value=fill_value
)
Expand All @@ -298,8 +295,6 @@ def _values_for_factorize(self):
return self._ndarray, -1

def unique(self):
from pandas import unique

return type(self)(unique(self._ndarray))

# ------------------------------------------------------------------------
Expand Down
10 changes: 0 additions & 10 deletions pandas/core/arrays/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ class SparseDtype(ExtensionDtype):
_metadata = ("_dtype", "_fill_value", "_is_na_fill_value")

def __init__(self, dtype: Dtype = np.float64, fill_value: Any = None) -> None:
from pandas.core.dtypes.missing import na_value_for_dtype
from pandas.core.dtypes.common import pandas_dtype, is_string_dtype, is_scalar

if isinstance(dtype, type(self)):
if fill_value is None:
Expand Down Expand Up @@ -178,20 +176,14 @@ def fill_value(self):

@property
def _is_na_fill_value(self):
from pandas.core.dtypes.missing import isna

return isna(self.fill_value)

@property
def _is_numeric(self):
from pandas.core.dtypes.common import is_object_dtype

return not is_object_dtype(self.subtype)

@property
def _is_boolean(self):
from pandas.core.dtypes.common import is_bool_dtype

return is_bool_dtype(self.subtype)

@property
Expand Down Expand Up @@ -928,8 +920,6 @@ def values(self):
return self.to_dense()

def isna(self):
from pandas import isna

# If null fill value, we want SparseDtype[bool, true]
# to preserve the same memory usage.
dtype = SparseDtype(bool, self._null_fill_value)
Expand Down
7 changes: 1 addition & 6 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from pandas.core import algorithms, common as com
from pandas.core.accessor import DirNamesMixin
from pandas.core.algorithms import duplicated, unique1d, value_counts
from pandas.core.arrays import ExtensionArray
import pandas.core.nanops as nanops

Expand Down Expand Up @@ -1381,8 +1382,6 @@ def value_counts(
1.0 1
dtype: int64
"""
from pandas.core.algorithms import value_counts

result = value_counts(
self,
sort=sort,
Expand All @@ -1400,8 +1399,6 @@ def unique(self):

result = values.unique()
else:
from pandas.core.algorithms import unique1d

result = unique1d(values)

return result
Expand Down Expand Up @@ -1631,8 +1628,6 @@ def drop_duplicates(self, keep="first", inplace=False):
return result

def duplicated(self, keep="first"):
from pandas.core.algorithms import duplicated

if isinstance(self, ABCIndexClass):
if self.is_unique:
return np.zeros(len(self), dtype=np.bool)
Expand Down
1 change: 0 additions & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ def asarray_tuplesafe(values, dtype=None):

if result.ndim == 2:
# Avoid building an array of arrays:
# TODO: verify whether any path hits this except #18819 (invalid)
values = [tuple(x) for x in values]
result = construct_1d_object_array_from_listlike(values)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ def maybe_cast_to_integer_array(arr, dtype, copy=False):
arr = np.asarray(arr)

if is_unsigned_integer_dtype(dtype) and (arr < 0).any():
raise OverflowError("Trying to coerce negative values " "to unsigned integers")
raise OverflowError("Trying to coerce negative values to unsigned integers")

if is_integer_dtype(dtype) and (is_float_dtype(arr) or is_object_dtype(arr)):
raise ValueError("Trying to coerce float values to integers")
6 changes: 2 additions & 4 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,7 @@ def _maybe_unwrap(x):
new_codes = np.concatenate(codes)

if sort_categories and not ignore_order and ordered:
raise TypeError(
"Cannot use sort_categories=True with " "ordered Categoricals"
)
raise TypeError("Cannot use sort_categories=True with ordered Categoricals")

if sort_categories and not categories.is_monotonic_increasing:
categories = categories.sort_values()
Expand All @@ -386,7 +384,7 @@ def _maybe_unwrap(x):
else:
# ordered - to show a proper error message
if all(c.ordered for c in to_union):
msg = "to union ordered Categoricals, " "all categories must be the same"
msg = "to union ordered Categoricals, all categories must be the same"
raise TypeError(msg)
else:
raise TypeError("Categorical.ordered must be the same")
Expand Down
Loading