Skip to content

Commit 379e145

Browse files
committed
promote consistency among type typetesting routines
now all are is_*
1 parent 3f24b87 commit 379e145

13 files changed

+97
-93
lines changed

pandas/core/categorical.py

-4
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ def f(self, other):
7878

7979
return f
8080

81-
def _is_categorical(array):
82-
""" return if we are a categorical possibility """
83-
return isinstance(array, Categorical) or isinstance(array.dtype, CategoricalDtype)
84-
8581
def _maybe_to_categorical(array):
8682
""" coerce to a categorical if a series is given """
8783
if isinstance(array, ABCSeries):

pandas/core/common.py

+14-7
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

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -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
@@ -2115,7 +2115,7 @@ 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)))

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)

pandas/core/index.py

+24-23
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
from pandas.core.common import isnull, array_equivalent
2020
import pandas.core.common as com
2121
from pandas.core.common import (_values_from_object, is_float, is_integer,
22-
ABCSeries, _ensure_object, _ensure_int64)
22+
ABCSeries, _ensure_object, _ensure_int64, is_bool_indexer,
23+
is_list_like, is_bool_dtype, is_integer_dtype)
2324
from pandas.core.config import get_option
2425
from pandas.io.common import PerformanceWarning
2526

@@ -55,7 +56,7 @@ def wrapper(self, other):
5556

5657
# technically we could support bool dtyped Index
5758
# for now just return the indexing array directly
58-
if com.is_bool_dtype(result):
59+
if is_bool_dtype(result):
5960
return result
6061
try:
6162
return Index(result)
@@ -160,7 +161,7 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,
160161
return Int64Index(data, copy=copy, dtype=dtype, name=name)
161162
elif issubclass(data.dtype.type, np.floating):
162163
return Float64Index(data, copy=copy, dtype=dtype, name=name)
163-
elif issubclass(data.dtype.type, np.bool) or com.is_bool_dtype(data):
164+
elif issubclass(data.dtype.type, np.bool) or is_bool_dtype(data):
164165
subarr = data.astype('object')
165166
else:
166167
subarr = com._asarray_tuplesafe(data, dtype=object)
@@ -510,15 +511,15 @@ def set_names(self, names, level=None, inplace=False):
510511
if level is not None and self.nlevels == 1:
511512
raise ValueError('Level must be None for non-MultiIndex')
512513

513-
if level is not None and not com.is_list_like(level) and com.is_list_like(names):
514+
if level is not None and not is_list_like(level) and is_list_like(names):
514515
raise TypeError("Names must be a string")
515516

516-
if not com.is_list_like(names) and level is None and self.nlevels > 1:
517+
if not is_list_like(names) and level is None and self.nlevels > 1:
517518
raise TypeError("Must pass list-like as `names`.")
518519

519-
if not com.is_list_like(names):
520+
if not is_list_like(names):
520521
names = [names]
521-
if level is not None and not com.is_list_like(level):
522+
if level is not None and not is_list_like(level):
522523
level = [level]
523524

524525
if inplace:
@@ -768,7 +769,7 @@ def _convert_list_indexer_for_mixed(self, keyarr, typ=None):
768769
and we have a mixed index (e.g. number/labels). figure out
769770
the indexer. return None if we can't help
770771
"""
771-
if (typ is None or typ in ['iloc','ix']) and (com.is_integer_dtype(keyarr) and not self.is_floating()):
772+
if (typ is None or typ in ['iloc','ix']) and (is_integer_dtype(keyarr) and not self.is_floating()):
772773
if self.inferred_type != 'integer':
773774
keyarr = np.where(keyarr < 0,
774775
len(self) + keyarr, keyarr)
@@ -929,7 +930,7 @@ def __getitem__(self, key):
929930
# pessimization of basic indexing.
930931
return promote(getitem(key))
931932

932-
if com._is_bool_indexer(key):
933+
if is_bool_indexer(key):
933934
key = np.asarray(key)
934935

935936
key = _values_from_object(key)
@@ -2104,7 +2105,7 @@ def get_slice_bound(self, label, side):
21042105
if isinstance(slc, np.ndarray):
21052106
# get_loc may return a boolean array or an array of indices, which
21062107
# is OK as long as they are representable by a slice.
2107-
if com.is_bool_dtype(slc):
2108+
if is_bool_dtype(slc):
21082109
slc = lib.maybe_booleans_to_slice(slc.view('u1'))
21092110
else:
21102111
slc = lib.maybe_indices_to_slice(slc.astype('i8'))
@@ -2882,15 +2883,15 @@ def set_levels(self, levels, level=None, inplace=False, verify_integrity=True):
28822883
labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
28832884
names=[u'foo', u'bar'])
28842885
"""
2885-
if level is not None and not com.is_list_like(level):
2886-
if not com.is_list_like(levels):
2886+
if level is not None and not is_list_like(level):
2887+
if not is_list_like(levels):
28872888
raise TypeError("Levels must be list-like")
2888-
if com.is_list_like(levels[0]):
2889+
if is_list_like(levels[0]):
28892890
raise TypeError("Levels must be list-like")
28902891
level = [level]
28912892
levels = [levels]
2892-
elif level is None or com.is_list_like(level):
2893-
if not com.is_list_like(levels) or not com.is_list_like(levels[0]):
2893+
elif level is None or is_list_like(level):
2894+
if not is_list_like(levels) or not is_list_like(levels[0]):
28942895
raise TypeError("Levels must be list of lists-like")
28952896

28962897
if inplace:
@@ -2980,15 +2981,15 @@ def set_labels(self, labels, level=None, inplace=False, verify_integrity=True):
29802981
labels=[[1, 0, 1, 0], [0, 0, 1, 1]],
29812982
names=[u'foo', u'bar'])
29822983
"""
2983-
if level is not None and not com.is_list_like(level):
2984-
if not com.is_list_like(labels):
2984+
if level is not None and not is_list_like(level):
2985+
if not is_list_like(labels):
29852986
raise TypeError("Labels must be list-like")
2986-
if com.is_list_like(labels[0]):
2987+
if is_list_like(labels[0]):
29872988
raise TypeError("Labels must be list-like")
29882989
level = [level]
29892990
labels = [labels]
2990-
elif level is None or com.is_list_like(level):
2991-
if not com.is_list_like(labels) or not com.is_list_like(labels[0]):
2991+
elif level is None or is_list_like(level):
2992+
if not is_list_like(labels) or not is_list_like(labels[0]):
29922993
raise TypeError("Labels must be list of lists-like")
29932994

29942995
if inplace:
@@ -3642,7 +3643,7 @@ def __getitem__(self, key):
36423643

36433644
return tuple(retval)
36443645
else:
3645-
if com._is_bool_indexer(key):
3646+
if is_bool_indexer(key):
36463647
key = np.asarray(key)
36473648
sortorder = self.sortorder
36483649
else:
@@ -4404,14 +4405,14 @@ def _convert_indexer(r):
44044405
ranges = []
44054406
for i,k in enumerate(tup):
44064407

4407-
if com._is_bool_indexer(k):
4408+
if is_bool_indexer(k):
44084409
# a boolean indexer, must be the same length!
44094410
k = np.asarray(k)
44104411
if len(k) != len(self):
44114412
raise ValueError("cannot index with a boolean indexer that is"
44124413
" not the same length as the index")
44134414
ranges.append(k)
4414-
elif com.is_list_like(k):
4415+
elif is_list_like(k):
44154416
# a collection of labels to include from this level (these are or'd)
44164417
indexers = []
44174418
for x in k:

0 commit comments

Comments
 (0)