Skip to content

Commit 4cf8c5f

Browse files
BUG: Groupby with as_index=True causes incorrect summarization (#34906)
* add test * PR comments * attempt to make the code cleaner
1 parent a154bf9 commit 4cf8c5f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pandas/tests/groupby/test_function.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ def test_max_min_non_numeric():
8585
assert "ss" in result
8686

8787

88+
def test_min_date_with_nans():
89+
# GH26321
90+
dates = pd.to_datetime(
91+
pd.Series(["2019-05-09", "2019-05-09", "2019-05-09"]), format="%Y-%m-%d"
92+
).dt.date
93+
df = pd.DataFrame({"a": [np.nan, "1", np.nan], "b": [0, 1, 1], "c": dates})
94+
95+
result = df.groupby("b", as_index=False)["c"].min()["c"]
96+
expected = pd.to_datetime(
97+
pd.Series(["2019-05-09", "2019-05-09"], name="c"), format="%Y-%m-%d"
98+
).dt.date
99+
tm.assert_series_equal(result, expected)
100+
101+
result = df.groupby("b")["c"].min()
102+
expected.index.name = "b"
103+
tm.assert_series_equal(result, expected)
104+
105+
88106
def test_intercept_builtin_sum():
89107
s = Series([1.0, 2.0, np.nan, 3.0])
90108
grouped = s.groupby([0, 1, 2, 2])

0 commit comments

Comments
 (0)