Skip to content

Commit 0144d18

Browse files
committed
Fix: Make PolyFit stat robust to missing data
Fixes #2992
1 parent 0f5a013 commit 0144d18

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

seaborn/_stats/regression.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ def _fit_predict(self, data):
3838

3939
def __call__(self, data, groupby, orient, scales):
4040

41-
return groupby.apply(data, self._fit_predict)
41+
return (
42+
groupby
43+
.apply(data.dropna(subset=["x", "y"]), self._fit_predict)
44+
)
4245

4346

4447
@dataclass

tests/_stats/test_regression.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
from numpy.testing import assert_array_equal, assert_array_almost_equal
7+
from pandas.testing import assert_frame_equal
78

89
from seaborn._core.groupby import GroupBy
910
from seaborn._stats.regression import PolyFit
@@ -50,3 +51,11 @@ def test_one_grouper(self, df):
5051
grid = np.linspace(part["x"].min(), part["x"].max(), gridsize)
5152
assert_array_equal(part["x"], grid)
5253
assert part["y"].diff().diff().dropna().abs().gt(0).all()
54+
55+
def test_missing_data(self, df):
56+
57+
groupby = GroupBy(["group"])
58+
df.iloc[5:10] = np.nan
59+
res1 = PolyFit()(df[["x", "y"]], groupby, "x", {})
60+
res2 = PolyFit()(df[["x", "y"]].dropna(), groupby, "x", {})
61+
assert_frame_equal(res1, res2)

0 commit comments

Comments
 (0)