Skip to content

ENH: Add ignore_index to dropna #51009

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

Merged
merged 2 commits into from
Jan 30, 2023
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: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ Other enhancements
- 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`)
- :meth:`DataFrame.plot.hist` now recognizes ``xlabel`` and ``ylabel`` arguments (:issue:`49793`)
- :meth:`Series.drop_duplicates` has gained ``ignore_index`` keyword to reset index (:issue:`48304`)
- :meth:`Series.dropna` and :meth:`DataFrame.dropna` has gained ``ignore_index`` keyword to reset index (:issue:`31725`)
- Improved error message in :func:`to_datetime` for non-ISO8601 formats, informing users about the position of the first error (:issue:`50361`)
- 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`)
- 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`)
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6211,6 +6211,7 @@ def dropna(
thresh: int | NoDefault = ...,
subset: IndexLabel = ...,
inplace: Literal[False] = ...,
ignore_index: bool = ...,
) -> DataFrame:
...

Expand All @@ -6223,6 +6224,7 @@ def dropna(
thresh: int | NoDefault = ...,
subset: IndexLabel = ...,
inplace: Literal[True],
ignore_index: bool = ...,
) -> None:
...

Expand All @@ -6234,6 +6236,7 @@ def dropna(
thresh: int | NoDefault = no_default,
subset: IndexLabel = None,
inplace: bool = False,
ignore_index: bool = False,
) -> DataFrame | None:
"""
Remove missing values.
Expand Down Expand Up @@ -6269,6 +6272,10 @@ def dropna(
these would be a list of columns to include.
inplace : bool, default False
Whether to modify the DataFrame rather than creating a new one.
ignore_index : bool, default ``False``
If ``True``, the resulting axis will be labeled 0, 1, …, n - 1.

.. versionadded:: 2.0.0

Returns
-------
Expand Down Expand Up @@ -6383,6 +6390,9 @@ def dropna(
else:
result = self.loc(axis=axis)[mask]

if ignore_index:
result.index = default_index(len(result))

if not inplace:
return result
self._update_inplace(result)
Expand Down
25 changes: 19 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5515,6 +5515,7 @@ def dropna(
axis: Axis = ...,
inplace: Literal[False] = ...,
how: AnyAll | None = ...,
ignore_index: bool = ...,
) -> Series:
...

Expand All @@ -5525,6 +5526,7 @@ def dropna(
axis: Axis = ...,
inplace: Literal[True],
how: AnyAll | None = ...,
ignore_index: bool = ...,
) -> None:
...

Expand All @@ -5534,6 +5536,7 @@ def dropna(
axis: Axis = 0,
inplace: bool = False,
how: AnyAll | None = None,
ignore_index: bool = False,
) -> Series | None:
"""
Return a new Series with missing values removed.
Expand All @@ -5549,6 +5552,10 @@ def dropna(
If True, do operation inplace and return None.
how : str, optional
Not in use. Kept for compatibility.
ignore_index : bool, default ``False``
If ``True``, the resulting axis will be labeled 0, 1, …, n - 1.

.. versionadded:: 2.0.0

Returns
-------
Expand Down Expand Up @@ -5606,19 +5613,25 @@ def dropna(
dtype: object
"""
inplace = validate_bool_kwarg(inplace, "inplace")
ignore_index = validate_bool_kwarg(ignore_index, "ignore_index")
# Validate the axis parameter
self._get_axis_number(axis or 0)

if self._can_hold_na:
result = remove_na_arraylike(self)
if inplace:
self._update_inplace(result)
else:
return result
else:
if not inplace:
return self.copy(deep=None)
return None
result = self.copy(deep=None)
else:
result = self

if ignore_index:
result.index = default_index(len(result))

if inplace:
return self._update_inplace(result)
else:
return result

# ----------------------------------------------------------------------
# Time series-oriented methods
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/frame/methods/test_dropna.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,14 @@ def test_how_thresh_param_incompatible(self):

with pytest.raises(TypeError, match=msg):
df.dropna(how=None, thresh=None)

@pytest.mark.parametrize("val", [1, 1.5])
def test_dropna_ignore_index(self, val):
# GH#31725
df = DataFrame({"a": [1, 2, val]}, index=[3, 2, 1])
result = df.dropna(ignore_index=True)
expected = DataFrame({"a": [1, 2, val]})
tm.assert_frame_equal(result, expected)

df.dropna(ignore_index=True, inplace=True)
tm.assert_frame_equal(df, expected)
11 changes: 11 additions & 0 deletions pandas/tests/series/methods/test_dropna.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,14 @@ def test_datetime64_tz_dropna(self):
)
assert result.dtype == "datetime64[ns, Asia/Tokyo]"
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("val", [1, 1.5])
def test_dropna_ignore_index(self, val):
# GH#31725
ser = Series([1, 2, val], index=[3, 2, 1])
result = ser.dropna(ignore_index=True)
expected = Series([1, 2, val])
tm.assert_series_equal(result, expected)

ser.dropna(ignore_index=True, inplace=True)
tm.assert_series_equal(ser, expected)