From 9424073f9eb5b6eb70600bfab2d23a46d89529d2 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Mon, 13 Mar 2023 20:31:22 +0000 Subject: [PATCH] BUG: Count creates result with read_only array --- pandas/core/frame.py | 2 +- pandas/tests/copy_view/test_methods.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 70019030da182..2052d84b1e6ff 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -10870,7 +10870,7 @@ def count(self, axis: Axis = 0, numeric_only: bool = False): else: # GH13407 series_counts = notna(frame).sum(axis=axis) - counts = series_counts.values + counts = series_counts._values result = self._constructor_sliced( counts, index=frame._get_agg_axis(axis) ) diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index 7429a73717470..7603799d0a4de 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -1672,3 +1672,11 @@ def test_transpose_ea_single_column(using_copy_on_write): result = df.T assert not np.shares_memory(get_array(df, "a"), get_array(result, 0)) + + +def test_count_read_only_array(): + df = DataFrame({"a": [1, 2], "b": 3}) + result = df.count() + result.iloc[0] = 100 + expected = Series([100, 2], index=["a", "b"]) + tm.assert_series_equal(result, expected)