Skip to content

TST: ensure that DataFrameGroupBy.apply does not convert datetime.date to pd.Timestamp #35504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import date, datetime
from io import StringIO

import numpy as np
Expand Down Expand Up @@ -1014,3 +1014,33 @@ def test_apply_with_timezones_aware():
result2 = df2.groupby("x", group_keys=False).apply(lambda df: df[["x", "y"]].copy())

tm.assert_frame_equal(result1, result2)


def test_apply_with_date_in_multiindex_does_not_convert_to_timestamp():
# GH 29617

df = pd.DataFrame(
{
"A": ["a", "a", "a", "b"],
"B": [
date(2020, 1, 10),
date(2020, 1, 10),
date(2020, 2, 10),
date(2020, 2, 10),
],
"C": [1, 2, 3, 4],
},
index=pd.Index([100, 101, 102, 103], name="idx"),
)

grp = df.groupby(["A", "B"])
result = grp.apply(lambda x: x.head(1))

expected = df.iloc[[0, 2, 3]]
expected = expected.reset_index()
expected.index = pd.MultiIndex.from_frame(expected[["A", "B", "idx"]])
expected = expected.drop(columns="idx")

tm.assert_frame_equal(result, expected)
for val in result.index.levels[1]:
assert type(val) is date