Skip to content

Commit

Permalink
REF: Create _lib/window directory (pandas-dev#29817)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke authored and proost committed Dec 19, 2019
1 parent a109af9 commit 7445252
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 46 deletions.
Empty file added pandas/_libs/window/__init__.py
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# cython: boundscheck=False, wraparound=False, cdivision=True

from typing import Tuple

import numpy as np
from numpy cimport ndarray, int64_t

Expand All @@ -8,33 +10,6 @@ from numpy cimport ndarray, int64_t
# These define start/end indexers to compute offsets


class MockFixedWindowIndexer:
"""
We are just checking parameters of the indexer,
and returning a consistent API with fixed/variable
indexers.
Parameters
----------
values: ndarray
values data array
win: int64_t
window size
index: object
index of the values
closed: string
closed behavior
"""
def __init__(self, ndarray values, int64_t win, object closed, object index=None):

self.start = np.empty(0, dtype='int64')
self.end = np.empty(0, dtype='int64')

def get_window_bounds(self):
return self.start, self.end


class FixedWindowIndexer:
"""
create a fixed length window indexer object
Expand Down Expand Up @@ -66,7 +41,7 @@ class FixedWindowIndexer:
end_e = start_e + win
self.end = np.concatenate([end_s, end_e])[:N]

def get_window_bounds(self):
def get_window_bounds(self) -> Tuple[np.ndarray, np.ndarray]:
return self.start, self.end


Expand Down Expand Up @@ -108,7 +83,7 @@ class VariableWindowIndexer:

@staticmethod
def build(const int64_t[:] index, int64_t win, bint left_closed,
bint right_closed, int64_t N):
bint right_closed, int64_t N) -> Tuple[np.ndarray, np.ndarray]:

cdef:
ndarray[int64_t] start, end
Expand Down Expand Up @@ -161,5 +136,5 @@ class VariableWindowIndexer:
end[i] -= 1
return start, end

def get_window_bounds(self):
def get_window_bounds(self) -> Tuple[np.ndarray, np.ndarray]:
return self.start, self.end
12 changes: 6 additions & 6 deletions pandas/core/window/ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np

import pandas._libs.window as libwindow
import pandas._libs.window.aggregations as window_aggregations
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution

Expand Down Expand Up @@ -228,11 +228,11 @@ def _apply(self, func, **kwargs):

# if we have a string function name, wrap it
if isinstance(func, str):
cfunc = getattr(libwindow, func, None)
cfunc = getattr(window_aggregations, func, None)
if cfunc is None:
raise ValueError(
"we do not support this function "
"in libwindow.{func}".format(func=func)
"in window_aggregations.{func}".format(func=func)
)

def func(arg):
Expand Down Expand Up @@ -284,7 +284,7 @@ def var(self, bias=False, *args, **kwargs):
nv.validate_window_func("var", args, kwargs)

def f(arg):
return libwindow.ewmcov(
return window_aggregations.ewmcov(
arg,
arg,
self.com,
Expand Down Expand Up @@ -328,7 +328,7 @@ def cov(self, other=None, pairwise=None, bias=False, **kwargs):
def _get_cov(X, Y):
X = self._shallow_copy(X)
Y = self._shallow_copy(Y)
cov = libwindow.ewmcov(
cov = window_aggregations.ewmcov(
X._prep_values(),
Y._prep_values(),
self.com,
Expand Down Expand Up @@ -375,7 +375,7 @@ def _get_corr(X, Y):
Y = self._shallow_copy(Y)

def _cov(x, y):
return libwindow.ewmcov(
return window_aggregations.ewmcov(
x,
y,
self.com,
Expand Down
12 changes: 6 additions & 6 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import numpy as np

import pandas._libs.window as libwindow
import pandas._libs.window_indexer as libwindow_indexer
import pandas._libs.window.aggregations as window_aggregations
import pandas._libs.window.indexers as window_indexers
from pandas.compat._optional import import_optional_dependency
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution, cache_readonly
Expand Down Expand Up @@ -381,11 +381,11 @@ def _get_roll_func(self, func_name: str) -> Callable:
-------
func : callable
"""
window_func = getattr(libwindow, func_name, None)
window_func = getattr(window_aggregations, func_name, None)
if window_func is None:
raise ValueError(
"we do not support this function "
"in libwindow.{func_name}".format(func_name=func_name)
"in window_aggregations.{func_name}".format(func_name=func_name)
)
return window_func

Expand All @@ -406,8 +406,8 @@ def _get_window_indexer(self):
Return an indexer class that will compute the window start and end bounds
"""
if self.is_freq_type:
return libwindow_indexer.VariableWindowIndexer
return libwindow_indexer.FixedWindowIndexer
return window_indexers.VariableWindowIndexer
return window_indexers.FixedWindowIndexer

def _apply(
self,
Expand Down
12 changes: 8 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,13 @@ class CheckSDist(sdist_class):
"pandas/_libs/tslibs/resolution.pyx",
"pandas/_libs/tslibs/parsing.pyx",
"pandas/_libs/tslibs/tzconversion.pyx",
"pandas/_libs/window_indexer.pyx",
"pandas/_libs/window/indexers.pyx",
"pandas/_libs/writers.pyx",
"pandas/io/sas/sas.pyx",
]

_cpp_pyxfiles = [
"pandas/_libs/window.pyx",
"pandas/_libs/window/aggregations.pyx",
"pandas/io/msgpack/_packer.pyx",
"pandas/io/msgpack/_unpacker.pyx",
]
Expand Down Expand Up @@ -683,8 +683,12 @@ def srcpath(name=None, suffix=".pyx", subdir="src"):
"sources": np_datetime_sources,
},
"_libs.testing": {"pyxfile": "_libs/testing"},
"_libs.window": {"pyxfile": "_libs/window", "language": "c++", "suffix": ".cpp"},
"_libs.window_indexer": {"pyxfile": "_libs/window_indexer"},
"_libs.window.aggregations": {
"pyxfile": "_libs/window/aggregations",
"language": "c++",
"suffix": ".cpp"
},
"_libs.window.indexers": {"pyxfile": "_libs/window/indexers"},
"_libs.writers": {"pyxfile": "_libs/writers"},
"io.sas._sas": {"pyxfile": "io/sas/sas"},
"io.msgpack._packer": {
Expand Down

0 comments on commit 7445252

Please sign in to comment.