From 0097f751c4af38107ffa31c5e19b90b6d88f045a Mon Sep 17 00:00:00 2001 From: Steven Rotondo Date: Tue, 5 Jul 2022 15:43:34 -0700 Subject: [PATCH 1/5] TST: Added test for consistent type with unique agg #22558 --- pandas/tests/groupby/aggregate/test_aggregate.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index d52b6ceaf8990..47a77f5801af6 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1413,3 +1413,15 @@ def test_multi_axis_1_raises(func): gb = df.groupby("a", axis=1) with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"): gb.agg(func) + + +def test_unique_agg_type(): + # GH#22558 + df1 = DataFrame({"a": [1, 2, 3], "b": [1, 1, 1]}) + df2 = DataFrame({"a": [2, 2, 2], "b": [1, 1, 1]}) + aggregation = {"a": "unique", "b": "unique"} + + agg1 = df1.agg(aggregation) + agg2 = df2.agg(aggregation) + + tm.assert_class_equal(agg1, agg2) From 06db464d458bd4771f56fcb14b75a1d0e62d4bdd Mon Sep 17 00:00:00 2001 From: Steven Rotondo Date: Tue, 5 Jul 2022 16:13:52 -0700 Subject: [PATCH 2/5] TST: Added test for consistent type with unique agg #22558 --- pandas/tests/groupby/aggregate/test_aggregate.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 54ee32502bbc9..d9dbd32974d3c 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1438,3 +1438,15 @@ def test_agg_of_mode_list(test, constant): expected = expected.set_index(0) tm.assert_frame_equal(result, expected) + + +def test_unique_agg_type(): + # GH#22558 + df1 = DataFrame({"a": [1, 2, 3], "b": [1, 1, 1]}) + df2 = DataFrame({"a": [2, 2, 2], "b": [1, 1, 1]}) + aggregation = {"a": "unique", "b": "unique"} + + agg1 = df1.agg(aggregation) + agg2 = df2.agg(aggregation) + + tm.assert_class_equal(agg1, agg2) From f977a15580edbe9ebd0d9da86e748c7b6e1f08a9 Mon Sep 17 00:00:00 2001 From: Steven Rotondo Date: Wed, 6 Jul 2022 12:35:30 -0700 Subject: [PATCH 3/5] TST: Moved and restructured test #22558 --- .../tests/groupby/aggregate/test_aggregate.py | 12 ------------ pandas/tests/util/test_assert_series_equal.py | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index d9dbd32974d3c..54ee32502bbc9 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1438,15 +1438,3 @@ def test_agg_of_mode_list(test, constant): expected = expected.set_index(0) tm.assert_frame_equal(result, expected) - - -def test_unique_agg_type(): - # GH#22558 - df1 = DataFrame({"a": [1, 2, 3], "b": [1, 1, 1]}) - df2 = DataFrame({"a": [2, 2, 2], "b": [1, 1, 1]}) - aggregation = {"a": "unique", "b": "unique"} - - agg1 = df1.agg(aggregation) - agg2 = df2.agg(aggregation) - - tm.assert_class_equal(agg1, agg2) diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index 2209bed67325c..4e5e45b315fc1 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -382,3 +382,21 @@ def test_assert_series_equal_identical_na(nulls_fixture): # while we're here do Index too idx = pd.Index(ser) tm.assert_index_equal(idx, idx.copy(deep=True)) + + +@pytest.mark.parametrize( + "test, constant", + [ + ({"a": [1, 2, 3], "b": [1, 1, 1]}, {"a": [1, 2, 3], "b": 1}), + ({"a": [2, 2, 2], "b": [1, 1, 1]}, {"a": 2, "b": 1}), + ], +) +def test_unique_agg_type_is_series(test, constant): + # GH#22558 + df1 = DataFrame(test) + expected = Series(data=constant, index=["a", "b"], dtype="object") + aggregation = {"a": "unique", "b": "unique"} + + result = df1.agg(aggregation) + + tm.assert_series_equal(result, expected) From e5f517ff4a2623ca0cdc752b20aa4683da31adaf Mon Sep 17 00:00:00 2001 From: Steven Rotondo Date: Fri, 8 Jul 2022 11:53:32 -0700 Subject: [PATCH 4/5] TYP: Fixed mypy issues in frequencies --- pandas/tseries/frequencies.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 7ad68a812c2e2..a4fe2161983b6 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -394,11 +394,11 @@ def _get_annual_rule(self) -> str | None: return None pos_check = self.month_position_check() - # error: Argument 1 to "get" of "dict" has incompatible type - # "Optional[str]"; expected "str" - return {"cs": "AS", "bs": "BAS", "ce": "A", "be": "BA"}.get( - pos_check # type: ignore[arg-type] - ) + + if pos_check is None: + return None + else: + return {"cs": "AS", "bs": "BAS", "ce": "A", "be": "BA"}.get(pos_check) def _get_quarterly_rule(self) -> str | None: if len(self.mdiffs) > 1: @@ -408,21 +408,21 @@ def _get_quarterly_rule(self) -> str | None: return None pos_check = self.month_position_check() - # error: Argument 1 to "get" of "dict" has incompatible type - # "Optional[str]"; expected "str" - return {"cs": "QS", "bs": "BQS", "ce": "Q", "be": "BQ"}.get( - pos_check # type: ignore[arg-type] - ) + + if pos_check is None: + return None + else: + return {"cs": "QS", "bs": "BQS", "ce": "Q", "be": "BQ"}.get(pos_check) def _get_monthly_rule(self) -> str | None: if len(self.mdiffs) > 1: return None pos_check = self.month_position_check() - # error: Argument 1 to "get" of "dict" has incompatible type - # "Optional[str]"; expected "str" - return {"cs": "MS", "bs": "BMS", "ce": "M", "be": "BM"}.get( - pos_check # type: ignore[arg-type] - ) + + if pos_check is None: + return None + else: + return {"cs": "MS", "bs": "BMS", "ce": "M", "be": "BM"}.get(pos_check) def _is_business_daily(self) -> bool: # quick check: cannot be business daily From b2432d78631a1b893b25a7a9b21e49e936542899 Mon Sep 17 00:00:00 2001 From: Steven Rotondo Date: Fri, 8 Jul 2022 12:00:55 -0700 Subject: [PATCH 5/5] TYP: Removed accidental inclusion --- pandas/tests/util/test_assert_series_equal.py | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index 4e5e45b315fc1..2209bed67325c 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -382,21 +382,3 @@ def test_assert_series_equal_identical_na(nulls_fixture): # while we're here do Index too idx = pd.Index(ser) tm.assert_index_equal(idx, idx.copy(deep=True)) - - -@pytest.mark.parametrize( - "test, constant", - [ - ({"a": [1, 2, 3], "b": [1, 1, 1]}, {"a": [1, 2, 3], "b": 1}), - ({"a": [2, 2, 2], "b": [1, 1, 1]}, {"a": 2, "b": 1}), - ], -) -def test_unique_agg_type_is_series(test, constant): - # GH#22558 - df1 = DataFrame(test) - expected = Series(data=constant, index=["a", "b"], dtype="object") - aggregation = {"a": "unique", "b": "unique"} - - result = df1.agg(aggregation) - - tm.assert_series_equal(result, expected)