Skip to content

Commit 513ff84

Browse files
makbigcPingviinituutti
authored andcommitted
Deprecate series.nonzero (GH18262) (pandas-dev#24048)
1 parent cfe982f commit 513ff84

File tree

7 files changed

+19
-5
lines changed

7 files changed

+19
-5
lines changed

doc/source/api/series.rst

-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ Computations / Descriptive Stats
188188
Series.is_monotonic_decreasing
189189
Series.value_counts
190190
Series.compound
191-
Series.nonzero
192191

193192
Reindexing / Selection / Label manipulation
194193
-------------------------------------------

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,7 @@ Deprecations
12581258
- 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`)
12591259
- 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`).
12601260
- :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`)
1261+
- :meth:`Series.nonzero` is deprecated and will be removed in a future version (:issue:`18262`)
12611262

12621263
.. _whatsnew_0240.deprecations.datetimelike_int_ops:
12631264

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4589,7 +4589,7 @@ def dropna(self, axis=0, how='any', thresh=None, subset=None,
45894589
else:
45904590
raise TypeError('must specify how or thresh')
45914591

4592-
result = self._take(mask.nonzero()[0], axis=axis)
4592+
result = self.loc(axis=axis)[mask]
45934593

45944594
if inplace:
45954595
self._update_inplace(result)
@@ -4624,7 +4624,7 @@ def drop_duplicates(self, subset=None, keep='first', inplace=False):
46244624
duplicated = self.duplicated(subset, keep=keep)
46254625

46264626
if inplace:
4627-
inds, = (-duplicated).nonzero()
4627+
inds, = (-duplicated)._ndarray_values.nonzero()
46284628
new_data = self._data.take(inds)
46294629
self._update_inplace(new_data)
46304630
else:

pandas/core/series.py

+7
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ def nonzero(self):
540540
"""
541541
Return the *integer* indices of the elements that are non-zero.
542542
543+
.. deprecated:: 0.24.0
544+
Please use .to_numpy().nonzero() as a replacement.
545+
543546
This method is equivalent to calling `numpy.nonzero` on the
544547
series data. For compatibility with NumPy, the return value is
545548
the same (a tuple with an array of indices for each dimension),
@@ -569,6 +572,10 @@ def nonzero(self):
569572
d 4
570573
dtype: int64
571574
"""
575+
msg = ("Series.nonzero() is deprecated "
576+
"and will be removed in a future version."
577+
"Use Series.to_numpy().nonzero() instead")
578+
warnings.warn(msg, FutureWarning, stacklevel=2)
572579
return self._values.nonzero()
573580

574581
def put(self, *args, **kwargs):

pandas/io/stata.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,8 @@ def _do_convert_missing(self, data, convert_missing):
16291629
continue
16301630

16311631
if convert_missing: # Replacement follows Stata notation
1632-
missing_loc = np.argwhere(missing)
1632+
1633+
missing_loc = np.argwhere(missing._ndarray_values)
16331634
umissing, umissing_loc = np.unique(series[missing],
16341635
return_inverse=True)
16351636
replacement = Series(series, dtype=np.object)

pandas/tests/frame/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2168,7 +2168,7 @@ def test_reindex_level(self):
21682168

21692169
def verify_first_level(df, level, idx, check_index_type=True):
21702170
def f(val):
2171-
return np.nonzero(df[level] == val)[0]
2171+
return np.nonzero((df[level] == val).to_numpy())[0]
21722172
i = np.concatenate(list(map(f, idx)))
21732173
left = df.set_index(icol).reindex(idx, level=level)
21742174
right = df.iloc[i].set_index(icol)

pandas/tests/series/test_missing.py

+6
Original file line numberDiff line numberDiff line change
@@ -1324,3 +1324,9 @@ def test_series_interpolate_intraday(self):
13241324
result = ts.reindex(new_index).interpolate(method='time')
13251325

13261326
tm.assert_numpy_array_equal(result.values, exp.values)
1327+
1328+
def test_nonzero_warning(self):
1329+
# GH 24048
1330+
ser = pd.Series([1, 0, 3, 4])
1331+
with tm.assert_produces_warning(FutureWarning):
1332+
ser.nonzero()

0 commit comments

Comments
 (0)