Skip to content

Raising TypeError on invalid window when using .rolling #12669 #12718

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,4 @@ Bug Fixes

- Bug in ``pivot_table`` when ``margins=True`` and ``dropna=True`` where nulls still contributed to margin count (:issue:`12577`)
- Bug in ``Series.name`` when ``name`` attribute can be a hashable type (:issue:`12610`)
- Bug in ``window`` Better error message on invalid window when using .rolling (:issue:`12669`)
2 changes: 2 additions & 0 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,8 @@ def _get_center_of_mass(com, span, halflife, alpha):


def _offset(window, center):
if com.is_float(window):
raise TypeError("Window should be of type Integer, given Float")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use if not com.is_integer(window): raise TypeError but that will clearly need to happen at a another point in the code, because at this point we accept arrays (see line below)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to happen in the _Rolling constructor. if window is not None then it can be tested for being an integer (or raise TypeError)

if not com.is_integer(window):
window = len(window)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test exists for Window as well, but pls confirm.

offset = (window - 1) / 2. if center else 0
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,9 @@ def test_rolling_median(self):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
self._check_moment_func(mom.rolling_median, np.median,
name='median')
# GH 12669
with self.assertRaises(TypeError):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a 1-liner explaining what the issue is

pd.DataFrame(np.arange(10)).rolling(2., center=True).median()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use pd., don't use center arg. Don't actually evaluate (with .median()), this should raise before that

def test_rolling_min(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test with a Series as well

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loop thru a couple of things that are unacceptable for window (e.g. string, datetime, numpy array) (as this is NOT a window, but a rolling op).

Expand Down