Skip to content

Commit 3e938c2

Browse files
Backport PR #48176 on branch 1.4.x (REGR: ensure DataFrame.select_dtypes() returns a copy) (#48219)
1 parent 4c60b14 commit 3e938c2

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.4.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Fixed regressions
2626
- Fixed regression in :meth:`DataFrame.loc` setting a length-1 array like value to a single value in the DataFrame (:issue:`46268`)
2727
- Fixed regression when slicing with :meth:`DataFrame.loc` with :class:`DateOffset`-index (:issue:`46671`)
2828
- Fixed regression in setting ``None`` or non-string value into a ``string``-dtype Series using a mask (:issue:`47628`)
29+
- Fixed regression in :meth:`DataFrame.select_dtypes` returning a view on the original DataFrame (:issue:`48090`)
2930
- Fixed regression using custom Index subclasses (for example, used in xarray) with :meth:`~DataFrame.reset_index` or :meth:`Index.insert` (:issue:`47071`)
3031
- Fixed regression in :meth:`DatetimeIndex.intersection` when the :class:`DatetimeIndex` has dates crossing daylight savings time (:issue:`46702`)
3132
- Fixed regression in :func:`merge` throwing an error when passing a :class:`Series` with a multi-level name (:issue:`47946`)

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4378,7 +4378,7 @@ def predicate(arr: ArrayLike) -> bool:
43784378

43794379
return True
43804380

4381-
mgr = self._mgr._get_data_subset(predicate)
4381+
mgr = self._mgr._get_data_subset(predicate).copy()
43824382
return type(self)(mgr).__finalize__(self)
43834383

43844384
def insert(

pandas/tests/frame/methods/test_select_dtypes.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
import pandas.util._test_decorators as td
5+
46
from pandas.core.dtypes.dtypes import ExtensionDtype
57

68
import pandas as pd
@@ -456,3 +458,13 @@ def test_np_bool_ea_boolean_include_number(self):
456458
result = df.select_dtypes(include="number")
457459
expected = DataFrame({"a": [1, 2, 3]})
458460
tm.assert_frame_equal(result, expected)
461+
462+
@td.skip_array_manager_invalid_test
463+
def test_select_dtypes_no_view(self):
464+
# https://github.com/pandas-dev/pandas/issues/48090
465+
# result of this method is not a view on the original dataframe
466+
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
467+
df_orig = df.copy()
468+
result = df.select_dtypes(include=["number"])
469+
result.iloc[0, 0] = 0
470+
tm.assert_frame_equal(df, df_orig)

0 commit comments

Comments
 (0)