Skip to content

Allow float window when using .rolling #12714

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 1 commit 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
43 changes: 22 additions & 21 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ def _dir_additions(self):
def _get_window(self, other=None):
return self.window

def _prep_window(self, **kwargs):
""" provide validation for our window type, return the window """
window = self._get_window()

if isinstance(window, (list, tuple, np.ndarray)):
return com._asarray_tuplesafe(window).astype(float)
elif com.is_integer(window):
try:
import scipy.signal as sig
except ImportError:
raise ImportError('Please install scipy to generate window '
'weight')
# the below may pop from kwargs
win_type = _validate_win_type(self.win_type, kwargs)
return sig.get_window(win_type, window).astype(float)

raise ValueError('Invalid window %s' % str(window))

@property
def _window_type(self):
return self.__class__.__name__
Expand Down Expand Up @@ -317,24 +335,6 @@ class Window(_Window):
* ``slepian`` (needs width).
"""

def _prep_window(self, **kwargs):
""" provide validation for our window type, return the window """
window = self._get_window()

if isinstance(window, (list, tuple, np.ndarray)):
return com._asarray_tuplesafe(window).astype(float)
elif com.is_integer(window):
try:
import scipy.signal as sig
except ImportError:
raise ImportError('Please install scipy to generate window '
'weight')
# the below may pop from kwargs
win_type = _validate_win_type(self.win_type, kwargs)
return sig.get_window(win_type, window).astype(float)

raise ValueError('Invalid window %s' % str(window))

def _apply_window(self, mean=True, how=None, **kwargs):
"""
Applies a moving window of type ``window_type`` on the data.
Expand Down Expand Up @@ -437,8 +437,7 @@ def _apply(self, func, window=None, center=None, check_minp=None, how=None,
"""
if center is None:
center = self.center
if window is None:
window = self._get_window()
window = self._prep_window(**kwargs)

if check_minp is None:
check_minp = _use_window
Expand Down Expand Up @@ -1354,7 +1353,9 @@ def _get_center_of_mass(com, span, halflife, alpha):


def _offset(window, center):
if not com.is_integer(window):
if com.is_float(window) and int(window) == window:
window = int(window)
elif not com.is_integer(window):
window = len(window)
offset = (window - 1) / 2. if center else 0
try:
Expand Down
10 changes: 9 additions & 1 deletion pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
import warnings

from nose.tools import assert_raises
from nose.tools import assert_raises, raises
from datetime import datetime
from numpy.random import randn
from numpy.testing.decorators import slow
Expand Down Expand Up @@ -225,6 +225,14 @@ def b(x):
result = r.aggregate([a, b])
assert_frame_equal(result, expected)

@raises(ValueError)
def test_window_rolling_float(self):
pd.DataFrame(np.arange(10)).rolling(2.).median()

@raises(ValueError)
def test_window_rolling_center_float(self):
pd.DataFrame(np.arange(10)).rolling(2., center=True).median()

def test_preserve_metadata(self):
# GH 10565
s = Series(np.arange(100), name='foo')
Expand Down