From 3f69d6277c4961631b606a62cad117daa6b3c052 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 23 Nov 2019 13:57:23 -0800 Subject: [PATCH] REF: Create _lib/window directory (#29817) --- pandas/_libs/window/__init__.py | 0 .../{window.pyx => window/aggregations.pyx} | 0 .../indexers.pyx} | 35 +++---------------- pandas/core/window/ewm.py | 12 +++---- pandas/core/window/rolling.py | 12 +++---- setup.py | 12 ++++--- 6 files changed, 25 insertions(+), 46 deletions(-) create mode 100644 pandas/_libs/window/__init__.py rename pandas/_libs/{window.pyx => window/aggregations.pyx} (100%) rename pandas/_libs/{window_indexer.pyx => window/indexers.pyx} (83%) diff --git a/pandas/_libs/window/__init__.py b/pandas/_libs/window/__init__.py new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/pandas/_libs/window.pyx b/pandas/_libs/window/aggregations.pyx similarity index 100% rename from pandas/_libs/window.pyx rename to pandas/_libs/window/aggregations.pyx diff --git a/pandas/_libs/window_indexer.pyx b/pandas/_libs/window/indexers.pyx similarity index 83% rename from pandas/_libs/window_indexer.pyx rename to pandas/_libs/window/indexers.pyx index 8f49a8b9462d3..eab9f0f8aab43 100644 --- a/pandas/_libs/window_indexer.pyx +++ b/pandas/_libs/window/indexers.pyx @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py index 89c25c07b0dbf..c9837afd96356 100644 --- a/pandas/core/window/ewm.py +++ b/pandas/core/window/ewm.py @@ -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 @@ -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): @@ -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, @@ -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, @@ -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, diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 6a35664ece765..2f37ba9b8f725 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -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 @@ -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 @@ -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, diff --git a/setup.py b/setup.py index cfcef4f9fa075..e6a95d4e7afd8 100755 --- a/setup.py +++ b/setup.py @@ -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", ] @@ -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": {