Skip to content

Commit 66de372

Browse files
authored
ENH: Add ignore_index to dropna (#51009)
* ENH: Add ignore_index to dropna * Add return statement
1 parent fd0e2f1 commit 66de372

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Other enhancements
183183
- Added ``copy`` parameter to :meth:`Series.infer_objects` and :meth:`DataFrame.infer_objects`, passing ``False`` will avoid making copies for series or columns that are already non-object or where no better dtype can be inferred (:issue:`50096`)
184184
- :meth:`DataFrame.plot.hist` now recognizes ``xlabel`` and ``ylabel`` arguments (:issue:`49793`)
185185
- :meth:`Series.drop_duplicates` has gained ``ignore_index`` keyword to reset index (:issue:`48304`)
186+
- :meth:`Series.dropna` and :meth:`DataFrame.dropna` has gained ``ignore_index`` keyword to reset index (:issue:`31725`)
186187
- Improved error message in :func:`to_datetime` for non-ISO8601 formats, informing users about the position of the first error (:issue:`50361`)
187188
- Improved error message when trying to align :class:`DataFrame` objects (for example, in :func:`DataFrame.compare`) to clarify that "identically labelled" refers to both index and columns (:issue:`50083`)
188189
- Added :meth:`DatetimeIndex.as_unit` and :meth:`TimedeltaIndex.as_unit` to convert to different resolutions; supported resolutions are "s", "ms", "us", and "ns" (:issue:`50616`)

pandas/core/frame.py

+10
Original file line numberDiff line numberDiff line change
@@ -6211,6 +6211,7 @@ def dropna(
62116211
thresh: int | NoDefault = ...,
62126212
subset: IndexLabel = ...,
62136213
inplace: Literal[False] = ...,
6214+
ignore_index: bool = ...,
62146215
) -> DataFrame:
62156216
...
62166217

@@ -6223,6 +6224,7 @@ def dropna(
62236224
thresh: int | NoDefault = ...,
62246225
subset: IndexLabel = ...,
62256226
inplace: Literal[True],
6227+
ignore_index: bool = ...,
62266228
) -> None:
62276229
...
62286230

@@ -6234,6 +6236,7 @@ def dropna(
62346236
thresh: int | NoDefault = no_default,
62356237
subset: IndexLabel = None,
62366238
inplace: bool = False,
6239+
ignore_index: bool = False,
62376240
) -> DataFrame | None:
62386241
"""
62396242
Remove missing values.
@@ -6269,6 +6272,10 @@ def dropna(
62696272
these would be a list of columns to include.
62706273
inplace : bool, default False
62716274
Whether to modify the DataFrame rather than creating a new one.
6275+
ignore_index : bool, default ``False``
6276+
If ``True``, the resulting axis will be labeled 0, 1, …, n - 1.
6277+
6278+
.. versionadded:: 2.0.0
62726279
62736280
Returns
62746281
-------
@@ -6383,6 +6390,9 @@ def dropna(
63836390
else:
63846391
result = self.loc(axis=axis)[mask]
63856392

6393+
if ignore_index:
6394+
result.index = default_index(len(result))
6395+
63866396
if not inplace:
63876397
return result
63886398
self._update_inplace(result)

pandas/core/series.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -5517,6 +5517,7 @@ def dropna(
55175517
axis: Axis = ...,
55185518
inplace: Literal[False] = ...,
55195519
how: AnyAll | None = ...,
5520+
ignore_index: bool = ...,
55205521
) -> Series:
55215522
...
55225523

@@ -5527,6 +5528,7 @@ def dropna(
55275528
axis: Axis = ...,
55285529
inplace: Literal[True],
55295530
how: AnyAll | None = ...,
5531+
ignore_index: bool = ...,
55305532
) -> None:
55315533
...
55325534

@@ -5536,6 +5538,7 @@ def dropna(
55365538
axis: Axis = 0,
55375539
inplace: bool = False,
55385540
how: AnyAll | None = None,
5541+
ignore_index: bool = False,
55395542
) -> Series | None:
55405543
"""
55415544
Return a new Series with missing values removed.
@@ -5551,6 +5554,10 @@ def dropna(
55515554
If True, do operation inplace and return None.
55525555
how : str, optional
55535556
Not in use. Kept for compatibility.
5557+
ignore_index : bool, default ``False``
5558+
If ``True``, the resulting axis will be labeled 0, 1, …, n - 1.
5559+
5560+
.. versionadded:: 2.0.0
55545561
55555562
Returns
55565563
-------
@@ -5608,19 +5615,25 @@ def dropna(
56085615
dtype: object
56095616
"""
56105617
inplace = validate_bool_kwarg(inplace, "inplace")
5618+
ignore_index = validate_bool_kwarg(ignore_index, "ignore_index")
56115619
# Validate the axis parameter
56125620
self._get_axis_number(axis or 0)
56135621

56145622
if self._can_hold_na:
56155623
result = remove_na_arraylike(self)
5616-
if inplace:
5617-
self._update_inplace(result)
5618-
else:
5619-
return result
56205624
else:
56215625
if not inplace:
5622-
return self.copy(deep=None)
5623-
return None
5626+
result = self.copy(deep=None)
5627+
else:
5628+
result = self
5629+
5630+
if ignore_index:
5631+
result.index = default_index(len(result))
5632+
5633+
if inplace:
5634+
return self._update_inplace(result)
5635+
else:
5636+
return result
56245637

56255638
# ----------------------------------------------------------------------
56265639
# Time series-oriented methods

pandas/tests/frame/methods/test_dropna.py

+11
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,14 @@ def test_how_thresh_param_incompatible(self):
272272

273273
with pytest.raises(TypeError, match=msg):
274274
df.dropna(how=None, thresh=None)
275+
276+
@pytest.mark.parametrize("val", [1, 1.5])
277+
def test_dropna_ignore_index(self, val):
278+
# GH#31725
279+
df = DataFrame({"a": [1, 2, val]}, index=[3, 2, 1])
280+
result = df.dropna(ignore_index=True)
281+
expected = DataFrame({"a": [1, 2, val]})
282+
tm.assert_frame_equal(result, expected)
283+
284+
df.dropna(ignore_index=True, inplace=True)
285+
tm.assert_frame_equal(df, expected)

pandas/tests/series/methods/test_dropna.py

+11
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,14 @@ def test_datetime64_tz_dropna(self):
101101
)
102102
assert result.dtype == "datetime64[ns, Asia/Tokyo]"
103103
tm.assert_series_equal(result, expected)
104+
105+
@pytest.mark.parametrize("val", [1, 1.5])
106+
def test_dropna_ignore_index(self, val):
107+
# GH#31725
108+
ser = Series([1, 2, val], index=[3, 2, 1])
109+
result = ser.dropna(ignore_index=True)
110+
expected = Series([1, 2, val])
111+
tm.assert_series_equal(result, expected)
112+
113+
ser.dropna(ignore_index=True, inplace=True)
114+
tm.assert_series_equal(ser, expected)

0 commit comments

Comments
 (0)