diff --git a/doc/source/api/series.rst b/doc/source/api/series.rst index 1631f04b1c72f..aa43c8b643d44 100644 --- a/doc/source/api/series.rst +++ b/doc/source/api/series.rst @@ -188,7 +188,6 @@ Computations / Descriptive Stats Series.is_monotonic_decreasing Series.value_counts Series.compound - Series.nonzero Reindexing / Selection / Label manipulation ------------------------------------------- diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 4ff1f96777b1c..d288ef8bf8449 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -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: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index aa78fc6c8e731..7bbbdd70e062e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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) @@ -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: diff --git a/pandas/core/series.py b/pandas/core/series.py index 5722fcb1b67a5..eb412add7bbbb 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -540,6 +540,9 @@ def nonzero(self): """ Return the *integer* indices of the elements that are non-zero. + .. deprecated:: 0.24.0 + 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), @@ -569,6 +572,10 @@ def nonzero(self): d 4 dtype: int64 """ + msg = ("Series.nonzero() is deprecated " + "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): diff --git a/pandas/io/stata.py b/pandas/io/stata.py index aad57fc489fb6..b5e7eb24465f5 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -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) diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py index b877ed93f07a2..f113140261aea 100644 --- a/pandas/tests/frame/test_indexing.py +++ b/pandas/tests/frame/test_indexing.py @@ -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) diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index ffd21fb449864..90ef465c5f239 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -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()