From b7376bfb42e5cb0af248c5f8cc73900abbf09120 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 30 Oct 2021 19:38:57 -0700 Subject: [PATCH 1/9] Add test for GH 16380 --- pandas/tests/apply/test_series_apply.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/apply/test_series_apply.py b/pandas/tests/apply/test_series_apply.py index 48f984c21623b..1d0b64c1835df 100644 --- a/pandas/tests/apply/test_series_apply.py +++ b/pandas/tests/apply/test_series_apply.py @@ -878,3 +878,15 @@ def test_apply_dictlike_transformer(string_series, ops): expected.name = string_series.name result = string_series.apply(ops) tm.assert_series_equal(result, expected) + + +def test_apply_retains_column_name(): + # GH 16380 + df = DataFrame({"x": range(3)}, Index(range(3), name="x")) + result = df.x.apply(lambda x: Series(range(x + 1), Index(range(x + 1), name="y"))) + expected = DataFrame( + [[0.0, np.nan, np.nan], [0.0, 1.0, np.nan], [0.0, 1.0, 2.0]], + columns=Index(range(3), name="y"), + index=Index(range(3), name="x"), + ) + tm.assert_frame_equal(result, expected) From 19988f6e3643767b55e4a4c8a7206ac2f4254fec Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 30 Oct 2021 19:52:01 -0700 Subject: [PATCH 2/9] Add test for GH 14135 --- pandas/tests/io/parser/test_read_fwf.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandas/tests/io/parser/test_read_fwf.py b/pandas/tests/io/parser/test_read_fwf.py index 6b136618de721..8d1fa97f9f8bb 100644 --- a/pandas/tests/io/parser/test_read_fwf.py +++ b/pandas/tests/io/parser/test_read_fwf.py @@ -853,3 +853,12 @@ def test_len_colspecs_len_names_with_index_col( index_col=index_col, ) tm.assert_frame_equal(result, expected) + + +def test_colspecs_with_comment(): + # GH 14135 + result = read_fwf( + StringIO("#\nA1K\n"), colspecs=[(1, 2), (2, 3)], comment="#", header=None + ) + expected = DataFrame([[1, "K"]], columns=[0, 1]) + tm.assert_frame_equal(result, expected) From 442459c28878ba8f4892aefcc7bae2c1cf41971c Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 30 Oct 2021 19:58:23 -0700 Subject: [PATCH 3/9] Add test for GH 16577 --- pandas/tests/groupby/test_rank.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/tests/groupby/test_rank.py b/pandas/tests/groupby/test_rank.py index c006d5a287bcd..83b8d5c29bbf0 100644 --- a/pandas/tests/groupby/test_rank.py +++ b/pandas/tests/groupby/test_rank.py @@ -648,3 +648,16 @@ def test_groupby_axis0_cummax_axis1(): expected = df[[0, 1]].astype(np.float64) expected[2] = expected[1] tm.assert_frame_equal(cmax, expected) + + +def test_non_unique_index(): + # GH 16577 + df = DataFrame( + {"A": [1.0, 2.0, 3.0, np.nan], "value": 1.0}, + index=[pd.Timestamp("20170101", tz="US/Eastern")] * 4, + ) + result = df.groupby([df.index, "A"]).value.rank(ascending=True, pct=True) + expected = Series( + [1.0] * 4, index=[pd.Timestamp("20170101", tz="US/Eastern")] * 4, name="value" + ) + tm.assert_series_equal(result, expected) From 833158d1ab5acb1e5caccfc34da6d6044e0936d0 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 30 Oct 2021 20:08:23 -0700 Subject: [PATCH 4/9] Add test for GH 16925 --- pandas/tests/frame/test_stack_unstack.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pandas/tests/frame/test_stack_unstack.py b/pandas/tests/frame/test_stack_unstack.py index 905b33b285625..404baecdfecac 100644 --- a/pandas/tests/frame/test_stack_unstack.py +++ b/pandas/tests/frame/test_stack_unstack.py @@ -2081,3 +2081,21 @@ def test_unstack_categorical_columns(self): ) expected.columns = MultiIndex.from_tuples([("cat", 0), ("cat", 1)]) tm.assert_frame_equal(result, expected) + + def test_stack_unsorted(self): + # GH 16925 + PAE = ["ITA", "FRA"] + VAR = ["A1", "A2"] + TYP = ["CRT", "DBT", "NET"] + MI = MultiIndex.from_product([PAE, VAR, TYP], names=["PAE", "VAR", "TYP"]) + + V = list(range(len(MI))) + DF = DataFrame(data=V, index=MI, columns=["VALUE"]) + + DF = DF.unstack(["VAR", "TYP"]) + DF.columns = DF.columns.droplevel(0) + DF.loc[:, ("A0", "NET")] = 9999 + + result = DF.stack(["VAR", "TYP"]).sort_index() + expected = DF.sort_index(axis=1).stack(["VAR", "TYP"]).sort_index() + tm.assert_series_equal(result, expected) From 5720d4939e3e517ef7903095e5e74227dea9eda5 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 30 Oct 2021 20:13:58 -0700 Subject: [PATCH 5/9] Add test for GH 17093 --- pandas/tests/groupby/transform/test_transform.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py index 441cbfe66f1d8..1e78bb1e58583 100644 --- a/pandas/tests/groupby/transform/test_transform.py +++ b/pandas/tests/groupby/transform/test_transform.py @@ -1289,3 +1289,11 @@ def test_transform_cumcount(): result = grp.transform("cumcount") tm.assert_series_equal(result, expected) + + +def test_null_group_lambda_self(): + # GH 17093 + df = DataFrame({"A": [1, np.nan], "B": [1, 1]}) + result = df.groupby("A").transform(lambda x: x) + expected = DataFrame([1], columns=["B"]) + tm.assert_frame_equal(result, expected) From 4aea48cbce87eb21798d9ca5980c3c984c315984 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 30 Oct 2021 20:22:09 -0700 Subject: [PATCH 6/9] Add test for GH 17382 --- pandas/tests/groupby/test_groupby.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 203d8abb465d0..26de48fc948d8 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2604,3 +2604,13 @@ def test_rolling_wrong_param_min_period(): result_error_msg = r"__init__\(\) got an unexpected keyword argument 'min_period'" with pytest.raises(TypeError, match=result_error_msg): test_df.groupby("name")["val"].rolling(window=2, min_period=1).sum() + + +def test_mean_on_timedelta(): + # GH 17382 + df = DataFrame({"time": pd.to_timedelta(range(10)), "cat": ["A", "B"] * 5}) + result = df.groupby("cat")["time"].mean() + expected = Series( + pd.to_timedelta([4, 5]), name="time", index=Index(["A", "B"], name="cat") + ) + tm.assert_series_equal(result, expected) From e9948a72d53dca5e12849ebe4162f397a0ca95db Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 30 Oct 2021 20:28:27 -0700 Subject: [PATCH 7/9] Add test for GH 18050 --- pandas/tests/arithmetic/test_categorical.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/arithmetic/test_categorical.py b/pandas/tests/arithmetic/test_categorical.py index 924f32b5ac9ac..d6f3a13ce6705 100644 --- a/pandas/tests/arithmetic/test_categorical.py +++ b/pandas/tests/arithmetic/test_categorical.py @@ -13,3 +13,13 @@ def test_categorical_nan_equality(self): expected = Series([True, True, True, False]) result = cat == cat tm.assert_series_equal(result, expected) + + def test_categorical_tuple_equality(self): + # GH 18050 + ser = Series([(0, 0), (0, 1), (0, 0), (1, 0), (1, 1)]) + expected = Series([True, False, True, False, False]) + result = ser == (0, 0) + tm.assert_series_equal(result, expected) + + result = ser.astype("category") == (0, 0) + tm.assert_series_equal(result, expected) From 2086c53288547ec71ca71fb58bcfe9654a456cd2 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sun, 31 Oct 2021 12:10:47 -0700 Subject: [PATCH 8/9] Move test --- pandas/tests/groupby/test_function.py | 10 ++++++++++ pandas/tests/groupby/test_groupby.py | 10 ---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pandas/tests/groupby/test_function.py b/pandas/tests/groupby/test_function.py index 3ae11847cc06b..3c402480ea2ec 100644 --- a/pandas/tests/groupby/test_function.py +++ b/pandas/tests/groupby/test_function.py @@ -1155,3 +1155,13 @@ def test_groupby_sum_below_mincount_nullable_integer(): result = grouped.sum(min_count=2) expected = DataFrame({"b": [pd.NA] * 3, "c": [pd.NA] * 3}, dtype="Int64", index=idx) tm.assert_frame_equal(result, expected) + + +def test_mean_on_timedelta(): + # GH 17382 + df = DataFrame({"time": pd.to_timedelta(range(10)), "cat": ["A", "B"] * 5}) + result = df.groupby("cat")["time"].mean() + expected = Series( + pd.to_timedelta([4, 5]), name="time", index=Index(["A", "B"], name="cat") + ) + tm.assert_series_equal(result, expected) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 26de48fc948d8..203d8abb465d0 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2604,13 +2604,3 @@ def test_rolling_wrong_param_min_period(): result_error_msg = r"__init__\(\) got an unexpected keyword argument 'min_period'" with pytest.raises(TypeError, match=result_error_msg): test_df.groupby("name")["val"].rolling(window=2, min_period=1).sum() - - -def test_mean_on_timedelta(): - # GH 17382 - df = DataFrame({"time": pd.to_timedelta(range(10)), "cat": ["A", "B"] * 5}) - result = df.groupby("cat")["time"].mean() - expected = Series( - pd.to_timedelta([4, 5]), name="time", index=Index(["A", "B"], name="cat") - ) - tm.assert_series_equal(result, expected) From c5ca52ea3860dc730bd6f54bd2090f38108e4fe6 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sun, 31 Oct 2021 19:23:30 -0700 Subject: [PATCH 9/9] Trigger CI