Skip to content

Commit 1cd87c9

Browse files
Christopher C. Aycockjreback
authored andcommitted
ENH: replace uses of np.isscalar with pd.lib.isscalar
closes #8708 closes #12459
1 parent 1d6d7d4 commit 1cd87c9

34 files changed

+82
-66
lines changed

pandas/computation/ops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pandas as pd
1111
from pandas.compat import PY3, string_types, text_type
1212
import pandas.core.common as com
13+
import pandas.lib as lib
1314
from pandas.core.base import StringMixin
1415
from pandas.computation.common import _ensure_decoded, _result_type_many
1516
from pandas.computation.scope import _DEFAULT_GLOBALS
@@ -98,7 +99,7 @@ def update(self, value):
9899

99100
@property
100101
def isscalar(self):
101-
return np.isscalar(self._value)
102+
return lib.isscalar(self._value)
102103

103104
@property
104105
def type(self):

pandas/computation/tests/test_eval.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import pandas.computation.expr as expr
3232
import pandas.util.testing as tm
33+
import pandas.lib as lib
3334
from pandas.util.testing import (assert_frame_equal, randbool,
3435
assertRaisesRegexp, assert_numpy_array_equal,
3536
assert_produces_warning, assert_series_equal)
@@ -196,7 +197,7 @@ def check_complex_cmp_op(self, lhs, cmp1, rhs, binop, cmp2):
196197
ex = '(lhs {cmp1} rhs) {binop} (lhs {cmp2} rhs)'.format(cmp1=cmp1,
197198
binop=binop,
198199
cmp2=cmp2)
199-
scalar_with_in_notin = (np.isscalar(rhs) and (cmp1 in skip_these or
200+
scalar_with_in_notin = (lib.isscalar(rhs) and (cmp1 in skip_these or
200201
cmp2 in skip_these))
201202
if scalar_with_in_notin:
202203
with tm.assertRaises(TypeError):
@@ -327,7 +328,7 @@ def check_pow(self, lhs, arith1, rhs):
327328
expected = self.get_expected_pow_result(lhs, rhs)
328329
result = pd.eval(ex, engine=self.engine, parser=self.parser)
329330

330-
if (np.isscalar(lhs) and np.isscalar(rhs) and
331+
if (lib.isscalar(lhs) and lib.isscalar(rhs) and
331332
_is_py3_complex_incompat(result, expected)):
332333
self.assertRaises(AssertionError, tm.assert_numpy_array_equal,
333334
result, expected)
@@ -360,16 +361,16 @@ def check_compound_invert_op(self, lhs, cmp1, rhs):
360361
skip_these = 'in', 'not in'
361362
ex = '~(lhs {0} rhs)'.format(cmp1)
362363

363-
if np.isscalar(rhs) and cmp1 in skip_these:
364+
if lib.isscalar(rhs) and cmp1 in skip_these:
364365
self.assertRaises(TypeError, pd.eval, ex, engine=self.engine,
365366
parser=self.parser, local_dict={'lhs': lhs,
366367
'rhs': rhs})
367368
else:
368369
# compound
369-
if np.isscalar(lhs) and np.isscalar(rhs):
370+
if lib.isscalar(lhs) and lib.isscalar(rhs):
370371
lhs, rhs = map(lambda x: np.array([x]), (lhs, rhs))
371372
expected = _eval_single_bin(lhs, cmp1, rhs, self.engine)
372-
if np.isscalar(expected):
373+
if lib.isscalar(expected):
373374
expected = not expected
374375
else:
375376
expected = ~expected
@@ -639,17 +640,17 @@ def test_identical(self):
639640
x = 1
640641
result = pd.eval('x', engine=self.engine, parser=self.parser)
641642
self.assertEqual(result, 1)
642-
self.assertTrue(np.isscalar(result))
643+
self.assertTrue(lib.isscalar(result))
643644

644645
x = 1.5
645646
result = pd.eval('x', engine=self.engine, parser=self.parser)
646647
self.assertEqual(result, 1.5)
647-
self.assertTrue(np.isscalar(result))
648+
self.assertTrue(lib.isscalar(result))
648649

649650
x = False
650651
result = pd.eval('x', engine=self.engine, parser=self.parser)
651652
self.assertEqual(result, False)
652-
self.assertTrue(np.isscalar(result))
653+
self.assertTrue(lib.isscalar(result))
653654

654655
x = np.array([1])
655656
result = pd.eval('x', engine=self.engine, parser=self.parser)

pandas/core/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def _get_score(at):
468468

469469
return score
470470

471-
if np.isscalar(q):
471+
if lib.isscalar(q):
472472
return _get_score(q)
473473
else:
474474
q = np.asarray(q, np.float64)

pandas/core/categorical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,7 @@ def _convert_to_list_like(list_like):
19011901
if (is_sequence(list_like) or isinstance(list_like, tuple) or
19021902
isinstance(list_like, types.GeneratorType)):
19031903
return list(list_like)
1904-
elif np.isscalar(list_like):
1904+
elif lib.isscalar(list_like):
19051905
return [list_like]
19061906
else:
19071907
# is this reached?

pandas/core/common.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def notnull(obj):
333333
pandas.isnull : boolean inverse of pandas.notnull
334334
"""
335335
res = isnull(obj)
336-
if np.isscalar(res):
336+
if lib.isscalar(res):
337337
return not res
338338
return ~res
339339

@@ -343,7 +343,7 @@ def is_null_datelike_scalar(other):
343343
but guard against passing a non-scalar """
344344
if other is pd.NaT or other is None:
345345
return True
346-
elif np.isscalar(other):
346+
elif lib.isscalar(other):
347347

348348
# a timedelta
349349
if hasattr(other, 'dtype'):
@@ -489,7 +489,7 @@ def mask_missing(arr, values_to_mask):
489489

490490
# if x is a string and arr is not, then we get False and we must
491491
# expand the mask to size arr.shape
492-
if np.isscalar(mask):
492+
if lib.isscalar(mask):
493493
mask = np.zeros(arr.shape, dtype=bool)
494494
else:
495495

@@ -1276,7 +1276,7 @@ def changeit():
12761276

12771277
# we have a scalar or len 0 ndarray
12781278
# and its nan and we are changing some values
1279-
if (np.isscalar(other) or
1279+
if (lib.isscalar(other) or
12801280
(isinstance(other, np.ndarray) and other.ndim < 1)):
12811281
if isnull(other):
12821282
return changeit()
@@ -1336,7 +1336,7 @@ def _possibly_downcast_to_dtype(result, dtype):
13361336
or could be an astype of float64->float32
13371337
"""
13381338

1339-
if np.isscalar(result):
1339+
if lib.isscalar(result):
13401340
return result
13411341

13421342
def trans(x):

pandas/core/generic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ def bool(self):
898898
v = self.squeeze()
899899
if isinstance(v, (bool, np.bool_)):
900900
return bool(v)
901-
elif np.isscalar(v):
901+
elif lib.isscalar(v):
902902
raise ValueError("bool cannot act on a non-boolean single element "
903903
"{0}".format(self.__class__.__name__))
904904

@@ -1750,10 +1750,10 @@ def xs(self, key, axis=0, level=None, copy=None, drop_level=True):
17501750
else:
17511751
return self.take(loc, axis=axis, convert=True)
17521752

1753-
if not np.isscalar(loc):
1753+
if not lib.isscalar(loc):
17541754
new_index = self.index[loc]
17551755

1756-
if np.isscalar(loc):
1756+
if lib.isscalar(loc):
17571757
from pandas import Series
17581758
new_values = self._data.fast_xs(loc)
17591759

pandas/core/groupby.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ def _try_cast(self, result, obj):
712712
else:
713713
dtype = obj.dtype
714714

715-
if not np.isscalar(result):
715+
if not lib.isscalar(result):
716716
result = _possibly_downcast_to_dtype(result, dtype)
717717

718718
return result
@@ -2384,7 +2384,8 @@ def is_in_obj(gpr):
23842384

23852385

23862386
def _is_label_like(val):
2387-
return isinstance(val, compat.string_types) or np.isscalar(val)
2387+
return (isinstance(val, compat.string_types) or
2388+
(val is not None and lib.isscalar(val)))
23882389

23892390

23902391
def _convert_grouper(axis, grouper):

pandas/core/indexing.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pandas.compat import range, zip
55
import pandas.compat as compat
66
import pandas.core.common as com
7+
import pandas.lib as lib
78
from pandas.core.common import (is_bool_indexer, is_integer_dtype,
89
_asarray_tuplesafe, is_list_like, isnull,
910
is_null_slice, is_full_slice, ABCSeries,
@@ -67,7 +68,7 @@ def __getitem__(self, key):
6768
if type(key) is tuple:
6869
try:
6970
values = self.obj.get_value(*key)
70-
if np.isscalar(values):
71+
if lib.isscalar(values):
7172
return values
7273
except Exception:
7374
pass
@@ -677,7 +678,7 @@ def _align_series(self, indexer, ser, multiindex_indexer=False):
677678

678679
return ser
679680

680-
elif np.isscalar(indexer):
681+
elif lib.isscalar(indexer):
681682
ax = self.obj._get_axis(1)
682683

683684
if ser.index.equals(ax):
@@ -753,7 +754,7 @@ def _align_frame(self, indexer, df):
753754
val = df.reindex(index=ax)._values
754755
return val
755756

756-
elif np.isscalar(indexer) and is_panel:
757+
elif lib.isscalar(indexer) and is_panel:
757758
idx = self.obj.axes[1]
758759
cols = self.obj.axes[2]
759760

@@ -960,7 +961,7 @@ def _getitem_nested_tuple(self, tup):
960961
axis += 1
961962

962963
# if we have a scalar, we are done
963-
if np.isscalar(obj) or not hasattr(obj, 'ndim'):
964+
if lib.isscalar(obj) or not hasattr(obj, 'ndim'):
964965
break
965966

966967
# has the dim of the obj changed?

pandas/core/internals.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ def _is_scalar_indexer(indexer):
665665
if arr_value.ndim == 1:
666666
if not isinstance(indexer, tuple):
667667
indexer = tuple([indexer])
668-
return all([np.isscalar(idx) for idx in indexer])
668+
return all([lib.isscalar(idx) for idx in indexer])
669669
return False
670670

671671
def _is_empty_indexer(indexer):
@@ -702,7 +702,7 @@ def _is_empty_indexer(indexer):
702702
values[indexer] = value
703703

704704
# coerce and try to infer the dtypes of the result
705-
if np.isscalar(value):
705+
if lib.isscalar(value):
706706
dtype, _ = _infer_dtype_from_scalar(value)
707707
else:
708708
dtype = 'infer'
@@ -3209,7 +3209,7 @@ def get(self, item, fastpath=True):
32093209
indexer = np.arange(len(self.items))[isnull(self.items)]
32103210

32113211
# allow a single nan location indexer
3212-
if not np.isscalar(indexer):
3212+
if not lib.isscalar(indexer):
32133213
if len(indexer) == 1:
32143214
loc = indexer.item()
32153215
else:

pandas/core/nanops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def _get_counts_nanvar(mask, axis, ddof, dtype=float):
351351
d = count - dtype.type(ddof)
352352

353353
# always return NaN, never inf
354-
if np.isscalar(count):
354+
if lib.isscalar(count):
355355
if count <= ddof:
356356
count = np.nan
357357
d = np.nan
@@ -623,7 +623,7 @@ def _get_counts(mask, axis, dtype=float):
623623
return dtype.type(mask.size - mask.sum())
624624

625625
count = mask.shape[axis] - mask.sum(axis)
626-
if np.isscalar(count):
626+
if lib.isscalar(count):
627627
return dtype.type(count)
628628
try:
629629
return count.astype(dtype)

0 commit comments

Comments
 (0)