Skip to content

Commit ee93b9f

Browse files
code sample for pandas-dev#43206
1 parent 0293a58 commit ee93b9f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

bisect/43206.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# BUG: inconsistent groupby.apply behaviour depending on column dtypes #43206
2+
3+
import pandas as pd
4+
5+
print(pd.__version__)
6+
7+
df = pd.DataFrame(
8+
{
9+
"date_val": {
10+
0: pd.Timestamp("2017-01-01 00:00:00"),
11+
1: pd.Timestamp("2017-01-01 00:00:00"),
12+
2: pd.Timestamp("2017-02-01 00:00:00"),
13+
3: pd.Timestamp("2017-02-01 00:00:00"),
14+
},
15+
"uid": {0: "1", 1: "2", 2: "1", 3: "2"},
16+
"str_val": {
17+
0: str("2017-01-01 00:00:00"),
18+
1: str("2017-01-01 00:00:00"),
19+
2: str("2017-02-01 00:00:00"),
20+
3: str("2017-02-01 00:00:00"),
21+
},
22+
}
23+
)
24+
25+
df1 = df.groupby("uid", as_index=False)[["uid", "str_val", "date_val"]].apply(
26+
lambda x: x.sort_values(by="str_val", ascending=True)
27+
)
28+
print(df1)
29+
30+
expected = pd.MultiIndex.from_tuples(
31+
[(0, 0), (0, 2), (1, 1), (1, 3)],
32+
)
33+
pd.testing.assert_index_equal(df1.index, expected)

0 commit comments

Comments
 (0)