Skip to content

Commit a3f3762

Browse files
authored
BUG: dropna changing index when nothing is dropped (#45027)
1 parent fd65ab4 commit a3f3762

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ Missing
726726
- Bug in constructing a :class:`DataFrame` with a dictionary ``np.datetime64`` as a value and ``dtype='timedelta64[ns]'``, or vice-versa, incorrectly casting instead of raising (:issue:`??`)
727727
- Bug in :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` with ``inplace=True`` not writing to the underlying array(s) in-place (:issue:`44749`)
728728
- Bug in :meth:`Index.fillna` incorrectly returning an un-filled :class:`Index` when NA values are present and ``downcast`` argument is specified. This now raises ``NotImplementedError`` instead; do not pass ``downcast`` argument (:issue:`44873`)
729-
-
729+
- Bug in :meth:`DataFrame.dropna` changing :class:`Index` even if no entries were dropped (:issue:`41965`)
730730

731731
MultiIndex
732732
^^^^^^^^^^

pandas/core/frame.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5999,7 +5999,10 @@ def dropna(
59995999
else:
60006000
raise TypeError("must specify how or thresh")
60016001

6002-
result = self.loc(axis=axis)[mask]
6002+
if np.all(mask):
6003+
result = self.copy()
6004+
else:
6005+
result = self.loc(axis=axis)[mask]
60036006

60046007
if inplace:
60056008
self._update_inplace(result)

pandas/tests/frame/methods/test_dropna.py

+7
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,10 @@ def test_subset_is_nparray(self):
267267
expected = DataFrame({"A": [1.0], "B": ["a"], "C": [4.0]})
268268
result = df.dropna(subset=np.array(["A", "C"]))
269269
tm.assert_frame_equal(result, expected)
270+
271+
def test_no_nans_in_frame(self, axis):
272+
# GH#41965
273+
df = DataFrame([[1, 2], [3, 4]], columns=pd.RangeIndex(0, 2))
274+
expected = df.copy()
275+
result = df.dropna(axis=axis)
276+
tm.assert_frame_equal(result, expected, check_index_type=True)

0 commit comments

Comments
 (0)