Skip to content

Commit 2ce2e89

Browse files
code sample for pandas-dev#38752
1 parent b4b0ecb commit 2ce2e89

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

bisect/38752.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import numpy as np
2+
import math
3+
import pandas as pd
4+
import pandas._testing as tm
5+
6+
print(pd.__version__)
7+
8+
9+
id1 = (
10+
pd.date_range("2019-10-01", "2020-01-08", freq="2H")
11+
.to_frame(name="stamp")
12+
.reset_index(drop=True)
13+
)
14+
id2 = (
15+
pd.date_range("2019-10-01", "2020-01-08")
16+
.to_frame(name="stamp")
17+
.reset_index(drop=True)
18+
)
19+
id1["id"] = 1
20+
a = 2.5
21+
id1["value"] = (math.e + a) ** (((id1.index + 10) % 40) - 20)
22+
id2["id"] = 2
23+
id2["value"] = np.nan
24+
id2.loc[id2.stamp == "2019-11-15", "value"] = 10.0 ** -13
25+
26+
df = pd.concat([id1, id2], ignore_index=True)
27+
df.sort_values("stamp", inplace=True)
28+
29+
roll_sum = df.groupby("id").rolling("93D", on="stamp")["value"].sum().reset_index()
30+
31+
result1 = roll_sum[roll_sum.id == 2].iloc[-1].value
32+
print(result1)
33+
34+
roll_sum_2 = (
35+
df[df.id == 2].groupby("id").rolling("93D", on="stamp")["value"].sum().reset_index()
36+
)
37+
38+
result2 = roll_sum_2[roll_sum_2.id == 2].iloc[-1].value
39+
print(result2)
40+
41+
tm.assert_almost_equal(result1, result2)

0 commit comments

Comments
 (0)