Skip to content

Commit

Permalink
Merge pull request #9498 from jreback/consist
Browse files Browse the repository at this point in the history
Promote some code consistency in type testing methods
  • Loading branch information
jreback committed Feb 16, 2015
2 parents 3f24b87 + 00dbf3d commit 76195fb
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 215 deletions.
13 changes: 4 additions & 9 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
from pandas.core.algorithms import factorize
from pandas.core.base import PandasObject, PandasDelegate
from pandas.core.index import Index, _ensure_index
from pandas.core.indexing import _is_null_slice
from pandas.tseries.period import PeriodIndex
import pandas.core.common as com
from pandas.util.decorators import cache_readonly

from pandas.core.common import (CategoricalDtype, ABCSeries, isnull, notnull,
is_categorical_dtype, is_integer_dtype, is_object_dtype,
_possibly_infer_to_datetimelike, get_dtype_kinds,
is_list_like, is_sequence,
is_list_like, is_sequence, is_null_slice,
_ensure_platform_int, _ensure_object, _ensure_int64,
_coerce_indexer_dtype, _values_from_object, take_1d)
from pandas.util.terminal import get_terminal_size
Expand Down Expand Up @@ -78,11 +77,7 @@ def f(self, other):

return f

def _is_categorical(array):
""" return if we are a categorical possibility """
return isinstance(array, Categorical) or isinstance(array.dtype, CategoricalDtype)

def _maybe_to_categorical(array):
def maybe_to_categorical(array):
""" coerce to a categorical if a series is given """
if isinstance(array, ABCSeries):
return array.values
Expand Down Expand Up @@ -1120,7 +1115,7 @@ def _slice(self, slicer):
# only allow 1 dimensional slicing, but can
# in a 2-d case be passd (slice(None),....)
if isinstance(slicer, tuple) and len(slicer) == 2:
if not _is_null_slice(slicer[0]):
if not is_null_slice(slicer[0]):
raise AssertionError("invalid slicing for a 1-ndim categorical")
slicer = slicer[1]

Expand Down Expand Up @@ -1267,7 +1262,7 @@ def __setitem__(self, key, value):
# only allow 1 dimensional slicing, but can
# in a 2-d case be passd (slice(None),....)
if len(key) == 2:
if not _is_null_slice(key[0]):
if not is_null_slice(key[0]):
raise AssertionError("invalid slicing for a 1-ndim categorical")
key = key[1]
elif len(key) == 1:
Expand Down
27 changes: 19 additions & 8 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def notnull(obj):
return not res
return ~res

def _is_null_datelike_scalar(other):
def is_null_datelike_scalar(other):
""" test whether the object is a null datelike, e.g. Nat
but guard against passing a non-scalar """
if other is pd.NaT or other is None:
Expand Down Expand Up @@ -2084,7 +2084,7 @@ def _try_timedelta(v):
return value


def _is_bool_indexer(key):
def is_bool_indexer(key):
if isinstance(key, (ABCSeries, np.ndarray)):
if key.dtype == np.object_:
key = np.asarray(_values_from_object(key))
Expand Down Expand Up @@ -2363,6 +2363,9 @@ def _maybe_make_list(obj):
return [obj]
return obj

########################
##### TYPE TESTING #####
########################

is_bool = lib.is_bool

Expand Down Expand Up @@ -2431,7 +2434,7 @@ def _get_dtype_type(arr_or_dtype):
return arr_or_dtype.dtype.type


def _is_any_int_dtype(arr_or_dtype):
def is_any_int_dtype(arr_or_dtype):
tipo = _get_dtype_type(arr_or_dtype)
return issubclass(tipo, np.integer)

Expand All @@ -2442,7 +2445,7 @@ def is_integer_dtype(arr_or_dtype):
not issubclass(tipo, (np.datetime64, np.timedelta64)))


def _is_int_or_datetime_dtype(arr_or_dtype):
def is_int_or_datetime_dtype(arr_or_dtype):
tipo = _get_dtype_type(arr_or_dtype)
return (issubclass(tipo, np.integer) or
issubclass(tipo, (np.datetime64, np.timedelta64)))
Expand All @@ -2467,12 +2470,12 @@ def is_timedelta64_ns_dtype(arr_or_dtype):
return tipo == _TD_DTYPE


def _is_datetime_or_timedelta_dtype(arr_or_dtype):
def is_datetime_or_timedelta_dtype(arr_or_dtype):
tipo = _get_dtype_type(arr_or_dtype)
return issubclass(tipo, (np.datetime64, np.timedelta64))


needs_i8_conversion = _is_datetime_or_timedelta_dtype
needs_i8_conversion = is_datetime_or_timedelta_dtype

def i8_boxer(arr_or_dtype):
""" return the scalar boxer for the dtype """
Expand All @@ -2493,7 +2496,7 @@ def is_float_dtype(arr_or_dtype):
return issubclass(tipo, np.floating)


def _is_floating_dtype(arr_or_dtype):
def is_floating_dtype(arr_or_dtype):
tipo = _get_dtype_type(arr_or_dtype)
return isinstance(tipo, np.floating)

Expand All @@ -2502,6 +2505,10 @@ def is_bool_dtype(arr_or_dtype):
tipo = _get_dtype_type(arr_or_dtype)
return issubclass(tipo, np.bool_)

def is_categorical(array):
""" return if we are a categorical possibility """
return isinstance(array, ABCCategorical) or isinstance(array.dtype, CategoricalDtype)

def is_categorical_dtype(arr_or_dtype):
if hasattr(arr_or_dtype,'dtype'):
arr_or_dtype = arr_or_dtype.dtype
Expand Down Expand Up @@ -2537,9 +2544,13 @@ def is_re_compilable(obj):


def is_list_like(arg):
return (hasattr(arg, '__iter__') and
return (hasattr(arg, '__iter__') and
not isinstance(arg, compat.string_and_binary_types))

def is_null_slice(obj):
return (isinstance(obj, slice) and obj.start is None and
obj.stop is None and obj.step is None)


def is_hashable(arg):
"""Return True if hash(arg) will succeed, False otherwise.
Expand Down
24 changes: 12 additions & 12 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
is_categorical_dtype)
from pandas.core.generic import NDFrame, _shared_docs
from pandas.core.index import Index, MultiIndex, _ensure_index
from pandas.core.indexing import (_maybe_droplevels,
_convert_to_index_sliceable,
_check_bool_indexer)
from pandas.core.indexing import (maybe_droplevels,
convert_to_index_sliceable,
check_bool_indexer)
from pandas.core.internals import (BlockManager,
create_block_manager_from_arrays,
create_block_manager_from_blocks)
Expand Down Expand Up @@ -1765,7 +1765,7 @@ def __getitem__(self, key):
pass

# see if we can slice the rows
indexer = _convert_to_index_sliceable(self, key)
indexer = convert_to_index_sliceable(self, key)
if indexer is not None:
return self._getitem_slice(indexer)

Expand Down Expand Up @@ -1798,7 +1798,7 @@ def _getitem_slice(self, key):

def _getitem_array(self, key):
# also raises Exception if object array with NA values
if com._is_bool_indexer(key):
if com.is_bool_indexer(key):
# warning here just in case -- previously __setitem__ was
# reindexing but __getitem__ was not; it seems more reasonable to
# go with the __setitem__ behavior since that is more consistent
Expand All @@ -1809,9 +1809,9 @@ def _getitem_array(self, key):
elif len(key) != len(self.index):
raise ValueError('Item wrong length %d instead of %d.' %
(len(key), len(self.index)))
# _check_bool_indexer will throw exception if Series key cannot
# check_bool_indexer will throw exception if Series key cannot
# be reindexed to match DataFrame rows
key = _check_bool_indexer(self.index, key)
key = check_bool_indexer(self.index, key)
indexer = key.nonzero()[0]
return self.take(indexer, axis=0, convert=False)
else:
Expand All @@ -1822,7 +1822,7 @@ def _getitem_multilevel(self, key):
loc = self.columns.get_loc(key)
if isinstance(loc, (slice, Series, np.ndarray, Index)):
new_columns = self.columns[loc]
result_columns = _maybe_droplevels(new_columns, key)
result_columns = maybe_droplevels(new_columns, key)
if self._is_mixed_type:
result = self.reindex(columns=new_columns)
result.columns = result_columns
Expand Down Expand Up @@ -2097,7 +2097,7 @@ def _box_col_values(self, values, items):
def __setitem__(self, key, value):

# see if we can slice the rows
indexer = _convert_to_index_sliceable(self, key)
indexer = convert_to_index_sliceable(self, key)
if indexer is not None:
return self._setitem_slice(indexer, value)

Expand All @@ -2115,11 +2115,11 @@ def _setitem_slice(self, key, value):

def _setitem_array(self, key, value):
# also raises Exception if object array with NA values
if com._is_bool_indexer(key):
if com.is_bool_indexer(key):
if len(key) != len(self.index):
raise ValueError('Item wrong length %d instead of %d!' %
(len(key), len(self.index)))
key = _check_bool_indexer(self.index, key)
key = check_bool_indexer(self.index, key)
indexer = key.nonzero()[0]
self._check_setitem_copy()
self.ix._setitem_with_indexer(indexer, value)
Expand Down Expand Up @@ -2246,7 +2246,7 @@ def reindexer(value):
if isinstance(self.columns, MultiIndex) and key in self.columns:
loc = self.columns.get_loc(key)
if isinstance(loc, (slice, Series, np.ndarray, Index)):
cols = _maybe_droplevels(self.columns[loc], key)
cols = maybe_droplevels(self.columns[loc], key)
if len(cols) and not cols.equals(value.columns):
value = value.reindex_axis(cols, axis=1)
# now align rows
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
notnull, _DATELIKE_DTYPES, is_numeric_dtype,
is_timedelta64_dtype, is_datetime64_dtype,
is_categorical_dtype, _values_from_object,
_is_datetime_or_timedelta_dtype, is_bool_dtype)
is_datetime_or_timedelta_dtype, is_bool_dtype)
from pandas.core.config import option_context
import pandas.lib as lib
from pandas.lib import Timestamp
Expand Down Expand Up @@ -1491,7 +1491,7 @@ def aggregate(self, values, how, axis=0):

is_numeric = is_numeric_dtype(values.dtype)

if _is_datetime_or_timedelta_dtype(values.dtype):
if is_datetime_or_timedelta_dtype(values.dtype):
values = values.view('int64')
elif is_bool_dtype(values.dtype):
values = _algos.ensure_float64(values)
Expand Down
Loading

0 comments on commit 76195fb

Please sign in to comment.