Skip to content

Commit c5c5f4a

Browse files
authored
Fix: Make PolyFit stat robust to missing data (#3010)
* Fix: Make PolyFit stat robust to missing data Fixes #2992 * Update release notes
1 parent 15713b3 commit c5c5f4a

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/whatsnew/v0.12.1.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
v0.12.1 (Unreleased)
3+
--------------------
4+
5+
- |Fix| Make :class:`objects.PolyFit` robust to missing data (:pr:`3010`).

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)