Skip to content

Commit d367c8a

Browse files
authored
TST: ensure that DataFrameGroupBy.apply does not convert datetime.date to pd.Timestamp (#35504)
1 parent 969945e commit d367c8a

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

pandas/tests/groupby/test_apply.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime
1+
from datetime import date, datetime
22
from io import StringIO
33

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

10161016
tm.assert_frame_equal(result1, result2)
1017+
1018+
1019+
def test_apply_with_date_in_multiindex_does_not_convert_to_timestamp():
1020+
# GH 29617
1021+
1022+
df = pd.DataFrame(
1023+
{
1024+
"A": ["a", "a", "a", "b"],
1025+
"B": [
1026+
date(2020, 1, 10),
1027+
date(2020, 1, 10),
1028+
date(2020, 2, 10),
1029+
date(2020, 2, 10),
1030+
],
1031+
"C": [1, 2, 3, 4],
1032+
},
1033+
index=pd.Index([100, 101, 102, 103], name="idx"),
1034+
)
1035+
1036+
grp = df.groupby(["A", "B"])
1037+
result = grp.apply(lambda x: x.head(1))
1038+
1039+
expected = df.iloc[[0, 2, 3]]
1040+
expected = expected.reset_index()
1041+
expected.index = pd.MultiIndex.from_frame(expected[["A", "B", "idx"]])
1042+
expected = expected.drop(columns="idx")
1043+
1044+
tm.assert_frame_equal(result, expected)
1045+
for val in result.index.levels[1]:
1046+
assert type(val) is date

0 commit comments

Comments
 (0)