Skip to content

Commit

Permalink
COMPAT: change neg of boolean to inv (deprecation in numpy 1.9)
Browse files Browse the repository at this point in the history
COMPAT: remove deprecation warnings on __new__ (in computation/pytables)
  • Loading branch information
jreback committed Apr 27, 2014
1 parent 3b13646 commit 59a38d4
Show file tree
Hide file tree
Showing 25 changed files with 65 additions and 45 deletions.
7 changes: 7 additions & 0 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ fi
"$TRAVIS_BUILD_DIR"/ci/build_docs.sh 2>&1 > /tmp/doc.log &
# doc build log will be shown after tests

# export the testing mode
if [ -n "$NUMPY_BUILD" ]; then

export PANDAS_TESTING_MODE="numpy_deprecate"

fi

echo nosetests --exe -w /tmp -A "$NOSE_ARGS" pandas --with-xunit --xunit-file=/tmp/nosetests.xml
nosetests --exe -w /tmp -A "$NOSE_ARGS" pandas --with-xunit --xunit-file=/tmp/nosetests.xml

Expand Down
3 changes: 3 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ Prior Version Deprecations/Changes
- Remove ``time_rule`` from several rolling-moment statistical functions, such
as :func:`rolling_sum` (:issue:`1042`)

- Removed neg (-) boolean operations on numpy arrays in favor of inv (~), as this is going to
be deprecated in numpy 1.9 (:issue:`6960`)

Experimental Features
~~~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 2 additions & 0 deletions doc/source/v0.14.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ There are prior version deprecations that are taking effect as of 0.14.0.
( `commit 3136390 <https://github.com/pydata/pandas/commit/3136390>`__ )
- Remove ``time_rule`` from several rolling-moment statistical functions, such
as :func:`rolling_sum` (:issue:`1042`)
- Removed neg (-) boolean operations on numpy arrays in favor of inv (~), as this is going to
be deprecated in numpy 1.9 (:issue:`6960`)

.. _whatsnew_0140.deprecations:

Expand Down
2 changes: 1 addition & 1 deletion pandas/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def rank_1d_generic(object in_arr, bint retry=1, ties_method='average',
if not retry:
raise

valid_locs = (-mask).nonzero()[0]
valid_locs = (~mask).nonzero()[0]
ranks.put(valid_locs, rank_1d_generic(values.take(valid_locs), 0,
ties_method=ties_method,
ascending=ascending))
Expand Down
4 changes: 1 addition & 3 deletions pandas/computation/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ class Term(StringMixin):
def __new__(cls, name, env, side=None, encoding=None):
klass = Constant if not isinstance(name, string_types) else cls
supr_new = super(Term, klass).__new__
if PY3:
return supr_new(klass)
return supr_new(klass, name, env, side=side, encoding=encoding)
return supr_new(klass)

def __init__(self, name, env, side=None, encoding=None):
self._name = name
Expand Down
4 changes: 1 addition & 3 deletions pandas/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ class Term(ops.Term):
def __new__(cls, name, env, side=None, encoding=None):
klass = Constant if not isinstance(name, string_types) else cls
supr_new = StringMixin.__new__
if PY3:
return supr_new(klass)
return supr_new(klass, name, env, side=side, encoding=encoding)
return supr_new(klass)

def __init__(self, name, env, side=None, encoding=None):
super(Term, self).__init__(name, env, side=side, encoding=encoding)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def quantile(x, q, interpolation_method='fraction'):
x = np.asarray(x)
mask = com.isnull(x)

x = x[-mask]
x = x[~mask]

values = np.sort(x)

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

idx = at * (len(values) - 1)
if idx % 1 == 0:
score = values[idx]
score = values[int(idx)]
else:
if interpolation_method == 'fraction':
score = _interpolate(values[int(idx)], values[int(idx) + 1],
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def _isnull_ndarraylike_old(obj):
# this is the NaT pattern
result = values.view('i8') == tslib.iNaT
else:
result = -np.isfinite(values)
result = ~np.isfinite(values)

# box
if isinstance(obj, ABCSeries):
Expand Down Expand Up @@ -280,7 +280,7 @@ def notnull(obj):
res = isnull(obj)
if np.isscalar(res):
return not res
return -res
return ~res

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

na_mask = isnull(values_to_mask)
nonna = values_to_mask[-na_mask]
nonna = values_to_mask[~na_mask]

mask = None
for x in nonna:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3041,7 +3041,7 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
this = self[col].values
that = other[col].values
if filter_func is not None:
mask = -filter_func(this) | isnull(that)
mask = ~filter_func(this) | isnull(that)
else:
if raise_conflict:
mask_this = notnull(that)
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,11 @@ def _indexed_same(self, other):
for a in self._AXIS_ORDERS])

def __neg__(self):
arr = operator.neg(_values_from_object(self))
values = _values_from_object(self)
if values.dtype == np.bool_:
arr = operator.inv(values)
else:
arr = operator.neg(values)
return self._wrap_array(arr, self.axes, copy=False)

def __invert__(self):
Expand Down Expand Up @@ -1459,10 +1463,10 @@ def drop(self, labels, axis=0, level=None, inplace=False, **kwargs):
if level is not None:
if not isinstance(axis, MultiIndex):
raise AssertionError('axis must be a MultiIndex')
indexer = -lib.ismember(axis.get_level_values(level),
indexer = ~lib.ismember(axis.get_level_values(level),
set(labels))
else:
indexer = -axis.isin(labels)
indexer = ~axis.isin(labels)

slicer = [slice(None)] * self.ndim
slicer[self._get_axis_number(axis_name)] = indexer
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,7 @@ def __init__(self, index, grouper=None, obj=None, name=None, level=None,

labels = np.empty(len(inds), dtype=inds.dtype)
labels[mask] = ok_labels
labels[-mask] = -1
labels[~mask] = -1

if len(uniques) < len(level_index):
level_index = level_index.take(uniques)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2997,7 +2997,7 @@ def _drop_from_level(self, labels, level):
index = self.levels[i]
values = index.get_indexer(labels)

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

return self[mask]

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ def to_native_types(self, slicer=None, na_rep='', float_format=None,
mask = isnull(values)
values[mask] = na_rep
if float_format:
imask = (-mask).ravel()
imask = (~mask).ravel()
values.flat[imask] = np.array(
[float_format % val for val in values.ravel()[imask]])
return values.tolist()
Expand Down Expand Up @@ -1181,7 +1181,7 @@ def to_native_types(self, slicer=None, na_rep=None, **kwargs):
if na_rep is None:
na_rep = 'NaT'
rvalues[mask] = na_rep
imask = (-mask).ravel()
imask = (~mask).ravel()
rvalues.flat[imask] = np.array([lib.repr_timedelta64(val)
for val in values.ravel()[imask]],
dtype=object)
Expand Down Expand Up @@ -1531,7 +1531,7 @@ def to_native_types(self, slicer=None, na_rep=None, date_format=None,
if na_rep is None:
na_rep = 'NaT'
rvalues[mask] = na_rep
imask = (-mask).ravel()
imask = (~mask).ravel()

if date_format is None:
date_formatter = lambda x: Timestamp(x)._repr_base
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ def _isfinite(values):
if issubclass(values.dtype.type, (np.timedelta64, np.datetime64)):
return isnull(values)
elif isinstance(values.dtype, object):
return -np.isfinite(values.astype('float64'))
return ~np.isfinite(values.astype('float64'))

return -np.isfinite(values)
return ~np.isfinite(values)


def _na_ok_dtype(dtype):
Expand Down
14 changes: 7 additions & 7 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def na_op(x, y):
mask = notnull(x)
result[mask] = op(x[mask], y)

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

result = com._fill_zeros(result, x, y, name, fill_zeros)
return result
Expand Down Expand Up @@ -746,7 +746,7 @@ def na_op(x, y):
if np.prod(xrav.shape):
result[mask] = op(xrav, y)

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

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

if op == operator.ne: # pragma: no cover
np.putmask(result, -mask, True)
np.putmask(result, ~mask, True)
else:
np.putmask(result, -mask, False)
np.putmask(result, ~mask, False)
result = result.reshape(x.shape)

return result
Expand Down Expand Up @@ -911,7 +911,7 @@ def na_op(x, y):
result = pa.empty(len(x), dtype=x.dtype)
mask = notnull(x)
result[mask] = op(x[mask], y)
result, changed = com._maybe_upcast_putmask(result, -mask, pa.NA)
result, changed = com._maybe_upcast_putmask(result, ~mask, pa.NA)

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

if op == operator.ne: # pragma: no cover
np.putmask(result, -mask, True)
np.putmask(result, ~mask, True)
else:
np.putmask(result, -mask, False)
np.putmask(result, ~mask, False)
result = result.reshape(x.shape)

return result
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,11 @@ def iteritems(self):

# inversion
def __neg__(self):
arr = operator.neg(self.values)
values = self.values
if values.dtype == np.bool_:
arr = operator.inv(values)
else:
arr = operator.neg(values)
return self._constructor(arr, self.index).__finalize__(self)

def __invert__(self):
Expand Down Expand Up @@ -1646,7 +1650,7 @@ def argsort(self, axis=0, kind='quicksort', order=None):
if mask.any():
result = Series(
-1, index=self.index, name=self.name, dtype='int64')
notmask = -mask
notmask = ~mask
result[notmask] = np.argsort(values[notmask], kind=kind)
return self._constructor(result,
index=self.index).__finalize__(self)
Expand Down Expand Up @@ -1767,7 +1771,7 @@ def _try_kind_sort(arr):

bad = isnull(arr)

good = -bad
good = ~bad
idx = pa.arange(len(self))

argsorted = _try_kind_sort(arr[good])
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def str_cat(arr, others=None, sep=None, na_rep=None):
result = np.empty(n, dtype=object)
np.putmask(result, na_mask, np.nan)

notmask = -na_mask
notmask = ~na_mask

tuples = zip(*[x[notmask] for x in arrays])
cats = [sep.join(tup) for tup in tuples]
Expand Down
2 changes: 1 addition & 1 deletion pandas/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def make_sparse(arr, kind='block', fill_value=nan):
length = len(arr)

if np.isnan(fill_value):
mask = -np.isnan(arr)
mask = ~np.isnan(arr)
else:
mask = arr != fill_value

Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4786,7 +4786,8 @@ def _check_unary_op(op):
_check_bin_op(operator.or_)
_check_bin_op(operator.xor)

_check_unary_op(operator.neg)
# operator.neg is deprecated in numpy >= 1.9
_check_unary_op(operator.inv)

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


def test_rank_na_option(self):
from pandas.compat.scipy import rankdata

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def test_interpolate_index_values(self):

expected = s.copy()
bad = isnull(expected.values)
good = -bad
good = ~bad
expected = Series(
np.interp(vals[bad], vals[good], s.values[good]), index=s.index[bad])

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_tseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def test_rank():
from pandas.compat.scipy import rankdata

def _check(arr):
mask = -np.isfinite(arr)
mask = ~np.isfinite(arr)
arr = arr.copy()
result = algos.rank_1d_float64(arr)
arr[mask] = np.inf
Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ def _format_native_types(self, na_rep=u('NaT'), **kwargs):
mask = isnull(self.values)
values[mask] = na_rep

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

Expand Down
4 changes: 2 additions & 2 deletions pandas/tseries/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_daily(self):
annual = pivot_annual(ts, 'D')

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

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

annual = pivot_annual(ts_hourly)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def calc(carg):
def calc_with_mask(carg,mask):
result = np.empty(carg.shape, dtype='M8[ns]')
iresult = result.view('i8')
iresult[-mask] = tslib.iNaT
iresult[~mask] = tslib.iNaT
result[mask] = calc(carg[mask].astype(np.float64).astype(np.int64)).astype('M8[ns]')
return result

Expand Down
7 changes: 5 additions & 2 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,19 @@
K = 4
_RAISE_NETWORK_ERROR_DEFAULT = False

# set testing_mode
testing_mode = os.environ.get('PANDAS_TESTING_MODE','None')
if 'numpy_deprecate' in testing_mode:
warnings.simplefilter('always', DeprecationWarning)

class TestCase(unittest.TestCase):

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

@classmethod
def tearDownClass(cls):
#print("tearing down up: {0}".format(cls))
pass

def assert_numpy_array_equal(self, np_array, assert_equal):
Expand Down

0 comments on commit 59a38d4

Please sign in to comment.