豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 1016fd9

Browse files
villebromichael-s-molina
authored andcommitted
fix(postprocessing): resample with holes (#27487)
(cherry picked from commit 7f19d29)
1 parent 017e0fc commit 1016fd9

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

superset/utils/pandas_postprocessing/resample.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@ def resample(
4343
raise InvalidPostProcessingError(_("Resample operation requires DatetimeIndex"))
4444
if method not in RESAMPLE_METHOD:
4545
raise InvalidPostProcessingError(
46-
_("Resample method should in ") + ", ".join(RESAMPLE_METHOD) + "."
46+
_("Resample method should be in ") + ", ".join(RESAMPLE_METHOD) + "."
4747
)
4848

4949
if method == "asfreq" and fill_value is not None:
5050
_df = df.resample(rule).asfreq(fill_value=fill_value)
51+
_df = _df.fillna(fill_value)
5152
elif method == "linear":
5253
_df = df.resample(rule).interpolate()
5354
else:
5455
_df = getattr(df.resample(rule), method)()
56+
if method in ("ffill", "bfill"):
57+
_df = _df.fillna(method=method)
5558
return _df

tests/unit_tests/pandas_postprocessing/test_resample.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121

2222
from superset.exceptions import InvalidPostProcessingError
2323
from superset.utils import pandas_postprocessing as pp
24-
from tests.unit_tests.fixtures.dataframes import categories_df, timeseries_df
24+
from tests.unit_tests.fixtures.dataframes import (
25+
categories_df,
26+
timeseries_df,
27+
timeseries_with_gap_df,
28+
)
2529

2630

2731
def test_resample_should_not_side_effect():
@@ -63,6 +67,29 @@ def test_resample():
6367
)
6468

6569

70+
def test_resample_ffill_with_gaps():
71+
post_df = pp.resample(df=timeseries_with_gap_df, rule="1D", method="ffill")
72+
assert post_df.equals(
73+
pd.DataFrame(
74+
index=pd.to_datetime(
75+
[
76+
"2019-01-01",
77+
"2019-01-02",
78+
"2019-01-03",
79+
"2019-01-04",
80+
"2019-01-05",
81+
"2019-01-06",
82+
"2019-01-07",
83+
]
84+
),
85+
data={
86+
"label": ["x", "y", "y", "y", "z", "z", "q"],
87+
"y": [1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 4.0],
88+
},
89+
)
90+
)
91+
92+
6693
def test_resample_zero_fill():
6794
post_df = pp.resample(df=timeseries_df, rule="1D", method="asfreq", fill_value=0)
6895
assert post_df.equals(
@@ -86,6 +113,31 @@ def test_resample_zero_fill():
86113
)
87114

88115

116+
def test_resample_zero_fill_with_gaps():
117+
post_df = pp.resample(
118+
df=timeseries_with_gap_df, rule="1D", method="asfreq", fill_value=0
119+
)
120+
assert post_df.equals(
121+
pd.DataFrame(
122+
index=pd.to_datetime(
123+
[
124+
"2019-01-01",
125+
"2019-01-02",
126+
"2019-01-03",
127+
"2019-01-04",
128+
"2019-01-05",
129+
"2019-01-06",
130+
"2019-01-07",
131+
]
132+
),
133+
data={
134+
"label": ["x", "y", 0, 0, "z", 0, "q"],
135+
"y": [1.0, 2.0, 0, 0, 0, 0, 4.0],
136+
},
137+
)
138+
)
139+
140+
89141
def test_resample_after_pivot():
90142
df = pd.DataFrame(
91143
data={

0 commit comments

Comments
 (0)