From 477641e3b9e1ac0879ad1c9abe1ab9dd439af44e Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 10 Mar 2021 09:20:39 +0100 Subject: [PATCH 1/3] [ArrayManager] TST: enable apply tests --- .github/workflows/ci.yml | 1 + pandas/core/internals/array_manager.py | 1 + pandas/tests/apply/test_frame_apply.py | 7 +++++-- pandas/tests/apply/test_frame_transform.py | 9 ++++++++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6c60522092739..408bfc8e6bd11 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -178,6 +178,7 @@ jobs: pytest pandas/tests/indexing/multiindex/test_setitem.py::TestMultiIndexSetItem::test_frame_setitem_multi_column pytest pandas/tests/api/ + pytest pandas/tests/apply/ pytest pandas/tests/base/ pytest pandas/tests/computation/ pytest pandas/tests/config/ diff --git a/pandas/core/internals/array_manager.py b/pandas/core/internals/array_manager.py index 906c95c825cab..367887b836567 100644 --- a/pandas/core/internals/array_manager.py +++ b/pandas/core/internals/array_manager.py @@ -922,6 +922,7 @@ def insert(self, loc: int, item: Hashable, value, allow_duplicates: bool = False raise ValueError( f"Expected a 1D array, got an array with shape {value.shape}" ) + value = ensure_wrapped_if_datetimelike(value) # TODO self.arrays can be empty # assert len(value) == len(self.arrays[0]) diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index 8cebe1a888b77..5df44c301303a 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -1435,7 +1435,7 @@ def test_apply_dtype(col): tm.assert_series_equal(result, expected) -def test_apply_mutating(): +def test_apply_mutating(using_array_manager): # GH#35462 case where applied func pins a new BlockManager to a row df = DataFrame({"a": range(100), "b": range(100, 200)}) @@ -1451,7 +1451,10 @@ def func(row): result = df.apply(func, axis=1) tm.assert_frame_equal(result, expected) - tm.assert_frame_equal(df, result) + if not using_array_manager: + # INFO(ArrayManager) With BlockManager, the row is a view and mutated in place, + # with ArrayManager the row is not a view, and thus not mutated in place + tm.assert_frame_equal(df, result) def test_apply_empty_list_reduce(): diff --git a/pandas/tests/apply/test_frame_transform.py b/pandas/tests/apply/test_frame_transform.py index 212a54b78dead..1a12cbff47092 100644 --- a/pandas/tests/apply/test_frame_transform.py +++ b/pandas/tests/apply/test_frame_transform.py @@ -39,8 +39,15 @@ def test_transform_ufunc(axis, float_frame, frame_or_series): @pytest.mark.parametrize("op", frame_transform_kernels) -def test_transform_groupby_kernel(axis, float_frame, op): +def test_transform_groupby_kernel(axis, float_frame, op, using_array_manager, request): # GH 35964 + if using_array_manager and op == "pct_change" and axis in (1, "columns"): + # TODO(ArrayManager) shift with axis=1 + request.node.add_marker( + pytest.mark.xfail( + reason="shift axis=1 not yet implemented for ArrayManager" + ) + ) args = [0.0] if op == "fillna" else [] if axis == 0 or axis == "index": From 118c63abb2abc7e12e12787a0b3c081e031f0df8 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 10 Mar 2021 10:25:51 +0100 Subject: [PATCH 2/3] update error message --- pandas/tests/groupby/test_groupby.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index afde1daca74c1..12247e2445295 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -840,7 +840,12 @@ def test_omit_nuisance(df): # won't work with axis = 1 grouped = df.groupby({"A": 0, "C": 0, "D": 1, "E": 1}, axis=1) - msg = "reduction operation 'sum' not allowed for this dtype" + msg = "|".join( + [ + "reduction operation 'sum' not allowed for this dtype", + "'DatetimeArray' does not implement reduction 'sum'", + ] + ) with pytest.raises(TypeError, match=msg): grouped.agg(lambda x: x.sum(0, numeric_only=False)) From f3344352ca66da4d5b856a8bc2db7a4364d6684c Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 16 Mar 2021 09:03:01 +0100 Subject: [PATCH 3/3] update test --- pandas/tests/apply/test_frame_apply.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index 5df44c301303a..227037ecba664 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -1438,6 +1438,7 @@ def test_apply_dtype(col): def test_apply_mutating(using_array_manager): # GH#35462 case where applied func pins a new BlockManager to a row df = DataFrame({"a": range(100), "b": range(100, 200)}) + df_orig = df.copy() def func(row): mgr = row._mgr @@ -1455,6 +1456,8 @@ def func(row): # INFO(ArrayManager) With BlockManager, the row is a view and mutated in place, # with ArrayManager the row is not a view, and thus not mutated in place tm.assert_frame_equal(df, result) + else: + tm.assert_frame_equal(df, df_orig) def test_apply_empty_list_reduce():