Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate series.nonzero (GH18262) #24048

Merged
merged 20 commits into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion doc/source/api/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ Computations / Descriptive Stats
Series.is_monotonic_decreasing
Series.value_counts
Series.compound
Series.nonzero

Reindexing / Selection / Label manipulation
-------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,7 @@ Deprecations
- The ``skipna`` parameter of :meth:`~pandas.api.types.infer_dtype` will switch to ``True`` by default in a future version of pandas (:issue:`17066`, :issue:`24050`)
- In :meth:`Series.where` with Categorical data, providing an ``other`` that is not present in the categories is deprecated. Convert the categorical to a different dtype or add the ``other`` to the categories first (:issue:`24077`).
- :meth:`Series.clip_lower`, :meth:`Series.clip_upper`, :meth:`DataFrame.clip_lower` and :meth:`DataFrame.clip_upper` are deprecated and will be removed in a future version. Use ``Series.clip(lower=threshold)``, ``Series.clip(upper=threshold)`` and the equivalent ``DataFrame`` methods (:issue:`24203`)
- :meth:`Series.nonzero` is deprecated and will be removed in a future version (:issue:`18262`)

.. _whatsnew_0240.deprecations.datetimelike_int_ops:

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4589,7 +4589,7 @@ def dropna(self, axis=0, how='any', thresh=None, subset=None,
else:
raise TypeError('must specify how or thresh')

result = self._take(mask.nonzero()[0], axis=axis)
result = self.loc(axis=axis)[mask]

if inplace:
self._update_inplace(result)
Expand Down Expand Up @@ -4624,7 +4624,7 @@ def drop_duplicates(self, subset=None, keep='first', inplace=False):
duplicated = self.duplicated(subset, keep=keep)

if inplace:
inds, = (-duplicated).nonzero()
inds, = (-duplicated)._ndarray_values.nonzero()
new_data = self._data.take(inds)
self._update_inplace(new_data)
else:
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,9 @@ def nonzero(self):
"""
Return the *integer* indices of the elements that are non-zero.

.. deprecated:: 0.24.0
jreback marked this conversation as resolved.
Show resolved Hide resolved
Please use .to_numpy().nonzero() as a replacement.

This method is equivalent to calling `numpy.nonzero` on the
series data. For compatibility with NumPy, the return value is
the same (a tuple with an array of indices for each dimension),
Expand Down Expand Up @@ -569,6 +572,10 @@ def nonzero(self):
d 4
dtype: int64
"""
msg = ("Series.nonzero() is deprecated "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you show what it is replaced by here

"and will be removed in a future version."
"Use Series.to_numpy().nonzero() instead")
warnings.warn(msg, FutureWarning, stacklevel=2)
return self._values.nonzero()

def put(self, *args, **kwargs):
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,8 @@ def _do_convert_missing(self, data, convert_missing):
continue

if convert_missing: # Replacement follows Stata notation
missing_loc = np.argwhere(missing)

missing_loc = np.argwhere(missing._ndarray_values)
umissing, umissing_loc = np.unique(series[missing],
return_inverse=True)
replacement = Series(series, dtype=np.object)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2168,7 +2168,7 @@ def test_reindex_level(self):

def verify_first_level(df, level, idx, check_index_type=True):
def f(val):
return np.nonzero(df[level] == val)[0]
return np.nonzero((df[level] == val).to_numpy())[0]
i = np.concatenate(list(map(f, idx)))
left = df.set_index(icol).reindex(idx, level=level)
right = df.iloc[i].set_index(icol)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,3 +1324,9 @@ def test_series_interpolate_intraday(self):
result = ts.reindex(new_index).interpolate(method='time')

tm.assert_numpy_array_equal(result.values, exp.values)

def test_nonzero_warning(self):
# GH 24048
ser = pd.Series([1, 0, 3, 4])
with tm.assert_produces_warning(FutureWarning):
ser.nonzero()