Skip to content

ENH: Poisson exponential window #26243

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

Merged
merged 4 commits into from
May 7, 2019
Merged
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
3 changes: 2 additions & 1 deletion doc/source/user_guide/computation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ The list of recognized types are the `scipy.signal window functions
* ``kaiser`` (needs beta)
* ``gaussian`` (needs std)
* ``general_gaussian`` (needs power, width)
* ``slepian`` (needs width).
* ``slepian`` (needs width)
* ``exponential`` (needs tau).

.. ipython:: python

Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Other Enhancements
- :class:`datetime.timezone` objects are now supported as arguments to timezone methods and constructors (:issue:`25065`)
- :meth:`DataFrame.query` and :meth:`DataFrame.eval` now supports quoting column names with backticks to refer to names with spaces (:issue:`6508`)
- :func:`merge_asof` now gives a more clear error message when merge keys are categoricals that are not equal (:issue:`26136`)
- :meth:`pandas.core.window.Rolling` supports exponential (or Poisson) window type (:issue:`21303`)

.. _whatsnew_0250.api_breaking:

Expand Down
19 changes: 14 additions & 5 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,8 @@ class Window(_Window):
* ``kaiser`` (needs beta)
* ``gaussian`` (needs std)
* ``general_gaussian`` (needs power, width)
* ``slepian`` (needs width).
* ``slepian`` (needs width)
* ``exponential`` (needs tau), center is set to None.

If ``win_type=None`` all points are evenly weighted. To learn more about
different window types see `scipy.signal window functions
Expand Down Expand Up @@ -623,11 +624,19 @@ def _validate_win_type(win_type, kwargs):
arg_map = {'kaiser': ['beta'],
'gaussian': ['std'],
'general_gaussian': ['power', 'width'],
'slepian': ['width']}
'slepian': ['width'],
'exponential': ['tau'],
}

if win_type in arg_map:
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you changing the structure of passing args here? was there some reason the existing would not just work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For exponential window kwargs contains {tau: 10} but I need to get back tuple (None, 10) (first None is the center parameter required for exponential window). So I pass the immutable_args to _pop_args function that is the second source of params (another than kwargs).

Another possible option can be add {"center": None} to kwargs for exponential window and let _pop_args without change.
More info about exponential window is in comments above.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes pls do this instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code is now without immutable_args. As I wrote in another comment, I simplyfied my code even more because other window (that would be possibly implemented in future) in scipy will not need such handling like the exponential one. So please check the last code version.
Thanks.

return tuple([win_type] + _pop_args(win_type,
arg_map[win_type],
kwargs))
win_args = _pop_args(win_type, arg_map[win_type], kwargs)
if win_type == 'exponential':
# exponential window requires the first arg (center)
# to be set to None (necessary for symmetric window)
win_args.insert(0, None)

return tuple([win_type] + win_args)

return win_type

def _pop_args(win_type, arg_names, kwargs):
Expand Down
13 changes: 9 additions & 4 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def win_types(request):
return request.param


@pytest.fixture(params=['kaiser', 'gaussian', 'general_gaussian'])
@pytest.fixture(params=['kaiser', 'gaussian', 'general_gaussian',
'exponential'])
def win_types_special(request):
return request.param

Expand Down Expand Up @@ -1260,7 +1261,8 @@ def test_cmov_window_special(self, win_types_special):
kwds = {
'kaiser': {'beta': 1.},
'gaussian': {'std': 1.},
'general_gaussian': {'power': 2., 'width': 2.}}
'general_gaussian': {'power': 2., 'width': 2.},
'exponential': {'tau': 10}}

vals = np.array([6.95, 15.21, 4.72, 9.12, 13.81, 13.49, 16.68, 9.48,
10.63, 14.48])
Expand All @@ -1271,7 +1273,9 @@ def test_cmov_window_special(self, win_types_special):
'general_gaussian': [np.nan, np.nan, 9.85011, 10.71589, 11.73161,
13.08516, 12.95111, 12.74577, np.nan, np.nan],
'kaiser': [np.nan, np.nan, 9.86851, 11.02969, 11.65161, 12.75129,
12.90702, 12.83757, np.nan, np.nan]
12.90702, 12.83757, np.nan, np.nan],
'exponential': [np.nan, np.nan, 9.83364, 11.10472, 11.64551,
12.66138, 12.92379, 12.83770, np.nan, np.nan],
}

xp = Series(xps[win_types_special])
Expand All @@ -1287,7 +1291,8 @@ def test_cmov_window_special_linear_range(self, win_types_special):
'kaiser': {'beta': 1.},
'gaussian': {'std': 1.},
'general_gaussian': {'power': 2., 'width': 2.},
'slepian': {'width': 0.5}}
'slepian': {'width': 0.5},
'exponential': {'tau': 10}}

vals = np.array(range(10), dtype=np.float)
xp = vals.copy()
Expand Down