Skip to content

Commit 76195fb

Browse files
committed
Merge pull request pandas-dev#9498 from jreback/consist
Promote some code consistency in type testing methods
2 parents 3f24b87 + 00dbf3d commit 76195fb

16 files changed

+212
-215
lines changed

pandas/core/categorical.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
from pandas.core.algorithms import factorize
1111
from pandas.core.base import PandasObject, PandasDelegate
1212
from pandas.core.index import Index, _ensure_index
13-
from pandas.core.indexing import _is_null_slice
1413
from pandas.tseries.period import PeriodIndex
1514
import pandas.core.common as com
1615
from pandas.util.decorators import cache_readonly
1716

1817
from pandas.core.common import (CategoricalDtype, ABCSeries, isnull, notnull,
1918
is_categorical_dtype, is_integer_dtype, is_object_dtype,
2019
_possibly_infer_to_datetimelike, get_dtype_kinds,
21-
is_list_like, is_sequence,
20+
is_list_like, is_sequence, is_null_slice,
2221
_ensure_platform_int, _ensure_object, _ensure_int64,
2322
_coerce_indexer_dtype, _values_from_object, take_1d)
2423
from pandas.util.terminal import get_terminal_size
@@ -78,11 +77,7 @@ def f(self, other):
7877

7978
return f
8079

81-
def _is_categorical(array):
82-
""" return if we are a categorical possibility """
83-
return isinstance(array, Categorical) or isinstance(array.dtype, CategoricalDtype)
84-
85-
def _maybe_to_categorical(array):
80+
def maybe_to_categorical(array):
8681
""" coerce to a categorical if a series is given """
8782
if isinstance(array, ABCSeries):
8883
return array.values
@@ -1120,7 +1115,7 @@ def _slice(self, slicer):
11201115
# only allow 1 dimensional slicing, but can
11211116
# in a 2-d case be passd (slice(None),....)
11221117
if isinstance(slicer, tuple) and len(slicer) == 2:
1123-
if not _is_null_slice(slicer[0]):
1118+
if not is_null_slice(slicer[0]):
11241119
raise AssertionError("invalid slicing for a 1-ndim categorical")
11251120
slicer = slicer[1]
11261121

@@ -1267,7 +1262,7 @@ def __setitem__(self, key, value):
12671262
# only allow 1 dimensional slicing, but can
12681263
# in a 2-d case be passd (slice(None),....)
12691264
if len(key) == 2:
1270-
if not _is_null_slice(key[0]):
1265+
if not is_null_slice(key[0]):
12711266
raise AssertionError("invalid slicing for a 1-ndim categorical")
12721267
key = key[1]
12731268
elif len(key) == 1:

pandas/core/common.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def notnull(obj):
368368
return not res
369369
return ~res
370370

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

20862086

2087-
def _is_bool_indexer(key):
2087+
def is_bool_indexer(key):
20882088
if isinstance(key, (ABCSeries, np.ndarray)):
20892089
if key.dtype == np.object_:
20902090
key = np.asarray(_values_from_object(key))
@@ -2363,6 +2363,9 @@ def _maybe_make_list(obj):
23632363
return [obj]
23642364
return obj
23652365

2366+
########################
2367+
##### TYPE TESTING #####
2368+
########################
23662369

23672370
is_bool = lib.is_bool
23682371

@@ -2431,7 +2434,7 @@ def _get_dtype_type(arr_or_dtype):
24312434
return arr_or_dtype.dtype.type
24322435

24332436

2434-
def _is_any_int_dtype(arr_or_dtype):
2437+
def is_any_int_dtype(arr_or_dtype):
24352438
tipo = _get_dtype_type(arr_or_dtype)
24362439
return issubclass(tipo, np.integer)
24372440

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

24442447

2445-
def _is_int_or_datetime_dtype(arr_or_dtype):
2448+
def is_int_or_datetime_dtype(arr_or_dtype):
24462449
tipo = _get_dtype_type(arr_or_dtype)
24472450
return (issubclass(tipo, np.integer) or
24482451
issubclass(tipo, (np.datetime64, np.timedelta64)))
@@ -2467,12 +2470,12 @@ def is_timedelta64_ns_dtype(arr_or_dtype):
24672470
return tipo == _TD_DTYPE
24682471

24692472

2470-
def _is_datetime_or_timedelta_dtype(arr_or_dtype):
2473+
def is_datetime_or_timedelta_dtype(arr_or_dtype):
24712474
tipo = _get_dtype_type(arr_or_dtype)
24722475
return issubclass(tipo, (np.datetime64, np.timedelta64))
24732476

24742477

2475-
needs_i8_conversion = _is_datetime_or_timedelta_dtype
2478+
needs_i8_conversion = is_datetime_or_timedelta_dtype
24762479

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

24952498

2496-
def _is_floating_dtype(arr_or_dtype):
2499+
def is_floating_dtype(arr_or_dtype):
24972500
tipo = _get_dtype_type(arr_or_dtype)
24982501
return isinstance(tipo, np.floating)
24992502

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

2508+
def is_categorical(array):
2509+
""" return if we are a categorical possibility """
2510+
return isinstance(array, ABCCategorical) or isinstance(array.dtype, CategoricalDtype)
2511+
25052512
def is_categorical_dtype(arr_or_dtype):
25062513
if hasattr(arr_or_dtype,'dtype'):
25072514
arr_or_dtype = arr_or_dtype.dtype
@@ -2537,9 +2544,13 @@ def is_re_compilable(obj):
25372544

25382545

25392546
def is_list_like(arg):
2540-
return (hasattr(arg, '__iter__') and
2547+
return (hasattr(arg, '__iter__') and
25412548
not isinstance(arg, compat.string_and_binary_types))
25422549

2550+
def is_null_slice(obj):
2551+
return (isinstance(obj, slice) and obj.start is None and
2552+
obj.stop is None and obj.step is None)
2553+
25432554

25442555
def is_hashable(arg):
25452556
"""Return True if hash(arg) will succeed, False otherwise.

pandas/core/frame.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
is_categorical_dtype)
3131
from pandas.core.generic import NDFrame, _shared_docs
3232
from pandas.core.index import Index, MultiIndex, _ensure_index
33-
from pandas.core.indexing import (_maybe_droplevels,
34-
_convert_to_index_sliceable,
35-
_check_bool_indexer)
33+
from pandas.core.indexing import (maybe_droplevels,
34+
convert_to_index_sliceable,
35+
check_bool_indexer)
3636
from pandas.core.internals import (BlockManager,
3737
create_block_manager_from_arrays,
3838
create_block_manager_from_blocks)
@@ -1765,7 +1765,7 @@ def __getitem__(self, key):
17651765
pass
17661766

17671767
# see if we can slice the rows
1768-
indexer = _convert_to_index_sliceable(self, key)
1768+
indexer = convert_to_index_sliceable(self, key)
17691769
if indexer is not None:
17701770
return self._getitem_slice(indexer)
17711771

@@ -1798,7 +1798,7 @@ def _getitem_slice(self, key):
17981798

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

20992099
# see if we can slice the rows
2100-
indexer = _convert_to_index_sliceable(self, key)
2100+
indexer = convert_to_index_sliceable(self, key)
21012101
if indexer is not None:
21022102
return self._setitem_slice(indexer, value)
21032103

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

21162116
def _setitem_array(self, key, value):
21172117
# also raises Exception if object array with NA values
2118-
if com._is_bool_indexer(key):
2118+
if com.is_bool_indexer(key):
21192119
if len(key) != len(self.index):
21202120
raise ValueError('Item wrong length %d instead of %d!' %
21212121
(len(key), len(self.index)))
2122-
key = _check_bool_indexer(self.index, key)
2122+
key = check_bool_indexer(self.index, key)
21232123
indexer = key.nonzero()[0]
21242124
self._check_setitem_copy()
21252125
self.ix._setitem_with_indexer(indexer, value)
@@ -2246,7 +2246,7 @@ def reindexer(value):
22462246
if isinstance(self.columns, MultiIndex) and key in self.columns:
22472247
loc = self.columns.get_loc(key)
22482248
if isinstance(loc, (slice, Series, np.ndarray, Index)):
2249-
cols = _maybe_droplevels(self.columns[loc], key)
2249+
cols = maybe_droplevels(self.columns[loc], key)
22502250
if len(cols) and not cols.equals(value.columns):
22512251
value = value.reindex_axis(cols, axis=1)
22522252
# now align rows

pandas/core/groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
notnull, _DATELIKE_DTYPES, is_numeric_dtype,
2626
is_timedelta64_dtype, is_datetime64_dtype,
2727
is_categorical_dtype, _values_from_object,
28-
_is_datetime_or_timedelta_dtype, is_bool_dtype)
28+
is_datetime_or_timedelta_dtype, is_bool_dtype)
2929
from pandas.core.config import option_context
3030
import pandas.lib as lib
3131
from pandas.lib import Timestamp
@@ -1491,7 +1491,7 @@ def aggregate(self, values, how, axis=0):
14911491

14921492
is_numeric = is_numeric_dtype(values.dtype)
14931493

1494-
if _is_datetime_or_timedelta_dtype(values.dtype):
1494+
if is_datetime_or_timedelta_dtype(values.dtype):
14951495
values = values.view('int64')
14961496
elif is_bool_dtype(values.dtype):
14971497
values = _algos.ensure_float64(values)

0 commit comments

Comments
 (0)