Skip to content

Commit 59a38d4

Browse files
committed
COMPAT: change neg of boolean to inv (deprecation in numpy 1.9)
COMPAT: remove deprecation warnings on __new__ (in computation/pytables)
1 parent 3b13646 commit 59a38d4

25 files changed

+65
-45
lines changed

ci/script.sh

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ fi
1616
"$TRAVIS_BUILD_DIR"/ci/build_docs.sh 2>&1 > /tmp/doc.log &
1717
# doc build log will be shown after tests
1818

19+
# export the testing mode
20+
if [ -n "$NUMPY_BUILD" ]; then
21+
22+
export PANDAS_TESTING_MODE="numpy_deprecate"
23+
24+
fi
25+
1926
echo nosetests --exe -w /tmp -A "$NOSE_ARGS" pandas --with-xunit --xunit-file=/tmp/nosetests.xml
2027
nosetests --exe -w /tmp -A "$NOSE_ARGS" pandas --with-xunit --xunit-file=/tmp/nosetests.xml
2128

doc/source/release.rst

+3
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ Prior Version Deprecations/Changes
240240
- Remove ``time_rule`` from several rolling-moment statistical functions, such
241241
as :func:`rolling_sum` (:issue:`1042`)
242242

243+
- Removed neg (-) boolean operations on numpy arrays in favor of inv (~), as this is going to
244+
be deprecated in numpy 1.9 (:issue:`6960`)
245+
243246
Experimental Features
244247
~~~~~~~~~~~~~~~~~~~~~
245248

doc/source/v0.14.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ There are prior version deprecations that are taking effect as of 0.14.0.
368368
( `commit 3136390 <https://github.com/pydata/pandas/commit/3136390>`__ )
369369
- Remove ``time_rule`` from several rolling-moment statistical functions, such
370370
as :func:`rolling_sum` (:issue:`1042`)
371+
- Removed neg (-) boolean operations on numpy arrays in favor of inv (~), as this is going to
372+
be deprecated in numpy 1.9 (:issue:`6960`)
371373

372374
.. _whatsnew_0140.deprecations:
373375

pandas/algos.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ def rank_1d_generic(object in_arr, bint retry=1, ties_method='average',
510510
if not retry:
511511
raise
512512

513-
valid_locs = (-mask).nonzero()[0]
513+
valid_locs = (~mask).nonzero()[0]
514514
ranks.put(valid_locs, rank_1d_generic(values.take(valid_locs), 0,
515515
ties_method=ties_method,
516516
ascending=ascending))

pandas/computation/ops.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ class Term(StringMixin):
4242
def __new__(cls, name, env, side=None, encoding=None):
4343
klass = Constant if not isinstance(name, string_types) else cls
4444
supr_new = super(Term, klass).__new__
45-
if PY3:
46-
return supr_new(klass)
47-
return supr_new(klass, name, env, side=side, encoding=encoding)
45+
return supr_new(klass)
4846

4947
def __init__(self, name, env, side=None, encoding=None):
5048
self._name = name

pandas/computation/pytables.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ class Term(ops.Term):
3333
def __new__(cls, name, env, side=None, encoding=None):
3434
klass = Constant if not isinstance(name, string_types) else cls
3535
supr_new = StringMixin.__new__
36-
if PY3:
37-
return supr_new(klass)
38-
return supr_new(klass, name, env, side=side, encoding=encoding)
36+
return supr_new(klass)
3937

4038
def __init__(self, name, env, side=None, encoding=None):
4139
super(Term, self).__init__(name, env, side=side, encoding=encoding)

pandas/core/algorithms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def quantile(x, q, interpolation_method='fraction'):
329329
x = np.asarray(x)
330330
mask = com.isnull(x)
331331

332-
x = x[-mask]
332+
x = x[~mask]
333333

334334
values = np.sort(x)
335335

@@ -339,7 +339,7 @@ def _get_score(at):
339339

340340
idx = at * (len(values) - 1)
341341
if idx % 1 == 0:
342-
score = values[idx]
342+
score = values[int(idx)]
343343
else:
344344
if interpolation_method == 'fraction':
345345
score = _interpolate(values[int(idx)], values[int(idx) + 1],

pandas/core/common.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def _isnull_ndarraylike_old(obj):
248248
# this is the NaT pattern
249249
result = values.view('i8') == tslib.iNaT
250250
else:
251-
result = -np.isfinite(values)
251+
result = ~np.isfinite(values)
252252

253253
# box
254254
if isinstance(obj, ABCSeries):
@@ -280,7 +280,7 @@ def notnull(obj):
280280
res = isnull(obj)
281281
if np.isscalar(res):
282282
return not res
283-
return -res
283+
return ~res
284284

285285
def _is_null_datelike_scalar(other):
286286
""" test whether the object is a null datelike, e.g. Nat
@@ -363,7 +363,7 @@ def mask_missing(arr, values_to_mask):
363363
values_to_mask = np.array(values_to_mask, dtype=object)
364364

365365
na_mask = isnull(values_to_mask)
366-
nonna = values_to_mask[-na_mask]
366+
nonna = values_to_mask[~na_mask]
367367

368368
mask = None
369369
for x in nonna:

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3041,7 +3041,7 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
30413041
this = self[col].values
30423042
that = other[col].values
30433043
if filter_func is not None:
3044-
mask = -filter_func(this) | isnull(that)
3044+
mask = ~filter_func(this) | isnull(that)
30453045
else:
30463046
if raise_conflict:
30473047
mask_this = notnull(that)

pandas/core/generic.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,11 @@ def _indexed_same(self, other):
606606
for a in self._AXIS_ORDERS])
607607

608608
def __neg__(self):
609-
arr = operator.neg(_values_from_object(self))
609+
values = _values_from_object(self)
610+
if values.dtype == np.bool_:
611+
arr = operator.inv(values)
612+
else:
613+
arr = operator.neg(values)
610614
return self._wrap_array(arr, self.axes, copy=False)
611615

612616
def __invert__(self):
@@ -1459,10 +1463,10 @@ def drop(self, labels, axis=0, level=None, inplace=False, **kwargs):
14591463
if level is not None:
14601464
if not isinstance(axis, MultiIndex):
14611465
raise AssertionError('axis must be a MultiIndex')
1462-
indexer = -lib.ismember(axis.get_level_values(level),
1466+
indexer = ~lib.ismember(axis.get_level_values(level),
14631467
set(labels))
14641468
else:
1465-
indexer = -axis.isin(labels)
1469+
indexer = ~axis.isin(labels)
14661470

14671471
slicer = [slice(None)] * self.ndim
14681472
slicer[self._get_axis_number(axis_name)] = indexer

pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ def __init__(self, index, grouper=None, obj=None, name=None, level=None,
16981698

16991699
labels = np.empty(len(inds), dtype=inds.dtype)
17001700
labels[mask] = ok_labels
1701-
labels[-mask] = -1
1701+
labels[~mask] = -1
17021702

17031703
if len(uniques) < len(level_index):
17041704
level_index = level_index.take(uniques)

pandas/core/index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2997,7 +2997,7 @@ def _drop_from_level(self, labels, level):
29972997
index = self.levels[i]
29982998
values = index.get_indexer(labels)
29992999

3000-
mask = -lib.ismember(self.labels[i], set(values))
3000+
mask = ~lib.ismember(self.labels[i], set(values))
30013001

30023002
return self[mask]
30033003

pandas/core/internals.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ def to_native_types(self, slicer=None, na_rep='', float_format=None,
10491049
mask = isnull(values)
10501050
values[mask] = na_rep
10511051
if float_format:
1052-
imask = (-mask).ravel()
1052+
imask = (~mask).ravel()
10531053
values.flat[imask] = np.array(
10541054
[float_format % val for val in values.ravel()[imask]])
10551055
return values.tolist()
@@ -1181,7 +1181,7 @@ def to_native_types(self, slicer=None, na_rep=None, **kwargs):
11811181
if na_rep is None:
11821182
na_rep = 'NaT'
11831183
rvalues[mask] = na_rep
1184-
imask = (-mask).ravel()
1184+
imask = (~mask).ravel()
11851185
rvalues.flat[imask] = np.array([lib.repr_timedelta64(val)
11861186
for val in values.ravel()[imask]],
11871187
dtype=object)
@@ -1531,7 +1531,7 @@ def to_native_types(self, slicer=None, na_rep=None, date_format=None,
15311531
if na_rep is None:
15321532
na_rep = 'NaT'
15331533
rvalues[mask] = na_rep
1534-
imask = (-mask).ravel()
1534+
imask = (~mask).ravel()
15351535

15361536
if date_format is None:
15371537
date_formatter = lambda x: Timestamp(x)._repr_base

pandas/core/nanops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ def _isfinite(values):
190190
if issubclass(values.dtype.type, (np.timedelta64, np.datetime64)):
191191
return isnull(values)
192192
elif isinstance(values.dtype, object):
193-
return -np.isfinite(values.astype('float64'))
193+
return ~np.isfinite(values.astype('float64'))
194194

195-
return -np.isfinite(values)
195+
return ~np.isfinite(values)
196196

197197

198198
def _na_ok_dtype(dtype):

pandas/core/ops.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def na_op(x, y):
452452
mask = notnull(x)
453453
result[mask] = op(x[mask], y)
454454

455-
result, changed = com._maybe_upcast_putmask(result, -mask, pa.NA)
455+
result, changed = com._maybe_upcast_putmask(result, ~mask, pa.NA)
456456

457457
result = com._fill_zeros(result, x, y, name, fill_zeros)
458458
return result
@@ -746,7 +746,7 @@ def na_op(x, y):
746746
if np.prod(xrav.shape):
747747
result[mask] = op(xrav, y)
748748

749-
result, changed = com._maybe_upcast_putmask(result, -mask, np.nan)
749+
result, changed = com._maybe_upcast_putmask(result, ~mask, np.nan)
750750
result = result.reshape(x.shape)
751751

752752
result = com._fill_zeros(result, x, y, name, fill_zeros)
@@ -817,9 +817,9 @@ def na_op(x, y):
817817
result[mask] = op(np.array(list(xrav[mask])), y)
818818

819819
if op == operator.ne: # pragma: no cover
820-
np.putmask(result, -mask, True)
820+
np.putmask(result, ~mask, True)
821821
else:
822-
np.putmask(result, -mask, False)
822+
np.putmask(result, ~mask, False)
823823
result = result.reshape(x.shape)
824824

825825
return result
@@ -911,7 +911,7 @@ def na_op(x, y):
911911
result = pa.empty(len(x), dtype=x.dtype)
912912
mask = notnull(x)
913913
result[mask] = op(x[mask], y)
914-
result, changed = com._maybe_upcast_putmask(result, -mask, pa.NA)
914+
result, changed = com._maybe_upcast_putmask(result, ~mask, pa.NA)
915915

916916
result = com._fill_zeros(result, x, y, name, fill_zeros)
917917
return result
@@ -947,9 +947,9 @@ def na_op(x, y):
947947
result[mask] = op(np.array(list(xrav[mask])), y)
948948

949949
if op == operator.ne: # pragma: no cover
950-
np.putmask(result, -mask, True)
950+
np.putmask(result, ~mask, True)
951951
else:
952-
np.putmask(result, -mask, False)
952+
np.putmask(result, ~mask, False)
953953
result = result.reshape(x.shape)
954954

955955
return result

pandas/core/series.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,11 @@ def iteritems(self):
961961

962962
# inversion
963963
def __neg__(self):
964-
arr = operator.neg(self.values)
964+
values = self.values
965+
if values.dtype == np.bool_:
966+
arr = operator.inv(values)
967+
else:
968+
arr = operator.neg(values)
965969
return self._constructor(arr, self.index).__finalize__(self)
966970

967971
def __invert__(self):
@@ -1646,7 +1650,7 @@ def argsort(self, axis=0, kind='quicksort', order=None):
16461650
if mask.any():
16471651
result = Series(
16481652
-1, index=self.index, name=self.name, dtype='int64')
1649-
notmask = -mask
1653+
notmask = ~mask
16501654
result[notmask] = np.argsort(values[notmask], kind=kind)
16511655
return self._constructor(result,
16521656
index=self.index).__finalize__(self)
@@ -1767,7 +1771,7 @@ def _try_kind_sort(arr):
17671771

17681772
bad = isnull(arr)
17691773

1770-
good = -bad
1774+
good = ~bad
17711775
idx = pa.arange(len(self))
17721776

17731777
argsorted = _try_kind_sort(arr[good])

pandas/core/strings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def str_cat(arr, others=None, sep=None, na_rep=None):
5151
result = np.empty(n, dtype=object)
5252
np.putmask(result, na_mask, np.nan)
5353

54-
notmask = -na_mask
54+
notmask = ~na_mask
5555

5656
tuples = zip(*[x[notmask] for x in arrays])
5757
cats = [sep.join(tup) for tup in tuples]

pandas/sparse/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def make_sparse(arr, kind='block', fill_value=nan):
509509
length = len(arr)
510510

511511
if np.isnan(fill_value):
512-
mask = -np.isnan(arr)
512+
mask = ~np.isnan(arr)
513513
else:
514514
mask = arr != fill_value
515515

pandas/tests/test_frame.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4786,7 +4786,8 @@ def _check_unary_op(op):
47864786
_check_bin_op(operator.or_)
47874787
_check_bin_op(operator.xor)
47884788

4789-
_check_unary_op(operator.neg)
4789+
# operator.neg is deprecated in numpy >= 1.9
4790+
_check_unary_op(operator.inv)
47904791

47914792
def test_logical_typeerror(self):
47924793
if not compat.PY3:
@@ -11110,7 +11111,7 @@ def test_rank2(self):
1111011111
exp = DataFrame({"a":[ 3.5, 1. , 3.5, 5. , 6. , 7. , 2. ]})
1111111112
assert_frame_equal(df.rank(), exp)
1111211113

11113-
11114+
1111411115
def test_rank_na_option(self):
1111511116
from pandas.compat.scipy import rankdata
1111611117

pandas/tests/test_generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ def test_interpolate_index_values(self):
526526

527527
expected = s.copy()
528528
bad = isnull(expected.values)
529-
good = -bad
529+
good = ~bad
530530
expected = Series(
531531
np.interp(vals[bad], vals[good], s.values[good]), index=s.index[bad])
532532

pandas/tests/test_tseries.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def test_rank():
338338
from pandas.compat.scipy import rankdata
339339

340340
def _check(arr):
341-
mask = -np.isfinite(arr)
341+
mask = ~np.isfinite(arr)
342342
arr = arr.copy()
343343
result = algos.rank_1d_float64(arr)
344344
arr[mask] = np.inf

pandas/tseries/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ def _format_native_types(self, na_rep=u('NaT'), **kwargs):
10651065
mask = isnull(self.values)
10661066
values[mask] = na_rep
10671067

1068-
imask = -mask
1068+
imask = ~mask
10691069
values[imask] = np.array([u('%s') % dt for dt in values[imask]])
10701070
return values.tolist()
10711071

pandas/tseries/tests/test_util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_daily(self):
2424
annual = pivot_annual(ts, 'D')
2525

2626
doy = ts.index.dayofyear
27-
doy[(-isleapyear(ts.index.year)) & (doy >= 60)] += 1
27+
doy[(~isleapyear(ts.index.year)) & (doy >= 60)] += 1
2828

2929
for i in range(1, 367):
3030
subset = ts[doy == i]
@@ -47,7 +47,7 @@ def test_hourly(self):
4747
grouped = ts_hourly.groupby(ts_hourly.index.year)
4848
hoy = grouped.apply(lambda x: x.reset_index(drop=True))
4949
hoy = hoy.index.droplevel(0).values
50-
hoy[-isleapyear(ts_hourly.index.year) & (hoy >= 1416)] += 24
50+
hoy[~isleapyear(ts_hourly.index.year) & (hoy >= 1416)] += 24
5151
hoy += 1
5252

5353
annual = pivot_annual(ts_hourly)

pandas/tseries/tools.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def calc(carg):
334334
def calc_with_mask(carg,mask):
335335
result = np.empty(carg.shape, dtype='M8[ns]')
336336
iresult = result.view('i8')
337-
iresult[-mask] = tslib.iNaT
337+
iresult[~mask] = tslib.iNaT
338338
result[mask] = calc(carg[mask].astype(np.float64).astype(np.int64)).astype('M8[ns]')
339339
return result
340340

pandas/util/testing.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,19 @@
5454
K = 4
5555
_RAISE_NETWORK_ERROR_DEFAULT = False
5656

57+
# set testing_mode
58+
testing_mode = os.environ.get('PANDAS_TESTING_MODE','None')
59+
if 'numpy_deprecate' in testing_mode:
60+
warnings.simplefilter('always', DeprecationWarning)
61+
5762
class TestCase(unittest.TestCase):
5863

5964
@classmethod
6065
def setUpClass(cls):
6166
pd.set_option('chained_assignment','raise')
62-
#print("setting up: {0}".format(cls))
6367

6468
@classmethod
6569
def tearDownClass(cls):
66-
#print("tearing down up: {0}".format(cls))
6770
pass
6871

6972
def assert_numpy_array_equal(self, np_array, assert_equal):

0 commit comments

Comments
 (0)