-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
BUG: Aligned skew and kurt results with scipy.stats #62925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SebastianGoldmann
wants to merge
13
commits into
pandas-dev:main
Choose a base branch
from
SebastianGoldmann:skew_kurt_fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
99a602f
kurtosis adjustment to return NaN for constant variance
612ec5f
adjusted kurt for series to align behaviour
a5e4680
adjusted for other condition as well
ade3f0c
finalized kurt and skew fix pd.series
b74c98a
adjusted window tests
7e1ac6a
updated whatsnew docstring
6275874
removed depreciated kurtosis functionality
86f416d
removed depreciated skew corner case
3369506
adjusted test on rolling kurt and skew
e2b8b7f
Apply suggestion from @Alvaro-Kothe
SebastianGoldmann 98356d6
Apply suggestion from @Alvaro-Kothe
SebastianGoldmann 2dd44a8
Apply suggestion from @Alvaro-Kothe
SebastianGoldmann 251d809
Apply suggestion from @Alvaro-Kothe
SebastianGoldmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,6 @@ | |
|
|
||
| import pandas as pd | ||
| from pandas import ( | ||
| DataFrame, | ||
| Series, | ||
| date_range, | ||
| ) | ||
|
|
@@ -228,47 +227,10 @@ def test_sem(self): | |
| result = s.sem(ddof=1) | ||
| assert pd.isna(result) | ||
|
|
||
| def test_skew(self): | ||
| sp_stats = pytest.importorskip("scipy.stats") | ||
|
|
||
| string_series = Series(range(20), dtype=np.float64, name="series") | ||
|
|
||
| alt = lambda x: sp_stats.skew(x, bias=False) | ||
| self._check_stat_op("skew", alt, string_series) | ||
|
|
||
| # test corner cases, skew() returns NaN unless there's at least 3 | ||
| # values | ||
| min_N = 3 | ||
| for i in range(1, min_N + 1): | ||
| s = Series(np.ones(i)) | ||
| df = DataFrame(np.ones((i, i))) | ||
| if i < min_N: | ||
| assert np.isnan(s.skew()) | ||
| assert np.isnan(df.skew()).all() | ||
| else: | ||
| assert 0 == s.skew() | ||
| assert isinstance(s.skew(), np.float64) # GH53482 | ||
| assert (df.skew() == 0).all() | ||
|
|
||
| def test_kurt(self): | ||
| sp_stats = pytest.importorskip("scipy.stats") | ||
|
|
||
| string_series = Series(range(20), dtype=np.float64, name="series") | ||
|
|
||
| alt = lambda x: sp_stats.kurtosis(x, bias=False) | ||
| self._check_stat_op("kurt", alt, string_series) | ||
|
|
||
| def test_kurt_corner(self): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you removing this test? |
||
| # test corner cases, kurt() returns NaN unless there's at least 4 | ||
| # values | ||
| min_N = 4 | ||
| for i in range(1, min_N + 1): | ||
| s = Series(np.ones(i)) | ||
| df = DataFrame(np.ones((i, i))) | ||
| if i < min_N: | ||
| assert np.isnan(s.kurt()) | ||
| assert np.isnan(df.kurt()).all() | ||
| else: | ||
| assert 0 == s.kurt() | ||
| assert isinstance(s.kurt(), np.float64) # GH53482 | ||
| assert (df.kurt() == 0).all() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -559,10 +559,10 @@ def _skew_kurt_wrap(self, values, axis=None, func=None): | |||||||||||||||||
| result = func(values, axis=axis, bias=False) | ||||||||||||||||||
| # fix for handling cases where all elements in an axis are the same | ||||||||||||||||||
| if isinstance(result, np.ndarray): | ||||||||||||||||||
| result[np.max(values, axis=axis) == np.min(values, axis=axis)] = 0 | ||||||||||||||||||
| result[np.max(values, axis=axis) == np.min(values, axis=axis)] = np.nan | ||||||||||||||||||
| return result | ||||||||||||||||||
| elif np.max(values) == np.min(values): | ||||||||||||||||||
| return 0.0 | ||||||||||||||||||
| return np.nan | ||||||||||||||||||
|
Comment on lines
560
to
+565
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these branches can be removed, since SciPy already assigns
Suggested change
|
||||||||||||||||||
| return result | ||||||||||||||||||
|
|
||||||||||||||||||
| def test_nanskew(self, skipna): | ||||||||||||||||||
|
|
@@ -1021,7 +1021,7 @@ def test_constant_series(self, val): | |||||||||||||||||
| # xref GH 11974 | ||||||||||||||||||
| data = val * np.ones(300) | ||||||||||||||||||
| skew = nanops.nanskew(data) | ||||||||||||||||||
| assert skew == 0.0 | ||||||||||||||||||
| assert np.isnan(skew) | ||||||||||||||||||
|
|
||||||||||||||||||
| def test_all_finite(self): | ||||||||||||||||||
| alpha, beta = 0.3, 0.1 | ||||||||||||||||||
|
|
@@ -1089,7 +1089,7 @@ def test_constant_series(self, val): | |||||||||||||||||
| # xref GH 11974 | ||||||||||||||||||
| data = val * np.ones(300) | ||||||||||||||||||
| kurt = nanops.nankurt(data) | ||||||||||||||||||
| tm.assert_equal(kurt, 0.0) | ||||||||||||||||||
| tm.assert_equal(kurt, np.nan) | ||||||||||||||||||
|
|
||||||||||||||||||
| def test_all_finite(self): | ||||||||||||||||||
| alpha, beta = 0.3, 0.1 | ||||||||||||||||||
|
|
||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you removing this test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HI Alvaro,
Thank you for the review.
In my previous commit I thought these were duplicated tests, but looking at it again these should not be removed. I will revert and update the tests.