From eab143faef17b2c930453989021e91b01676fd37 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 11 Aug 2017 15:32:31 -0700 Subject: [PATCH 1/2] Check against ABCClasses; Remove import of pandas as pd --- pandas/core/window.py | 51 ++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/pandas/core/window.py b/pandas/core/window.py index 5866f1e8a76bd..fbd9e89bc64b8 100644 --- a/pandas/core/window.py +++ b/pandas/core/window.py @@ -17,7 +17,8 @@ ABCDataFrame, ABCDatetimeIndex, ABCTimedeltaIndex, - ABCPeriodIndex) + ABCPeriodIndex, + ABCDateOffset) from pandas.core.dtypes.common import ( is_integer, is_bool, @@ -28,13 +29,12 @@ is_list_like, _ensure_float64, is_scalar) -import pandas as pd from pandas.core.base import (PandasObject, SelectionMixin, GroupByMixin) import pandas.core.common as com import pandas._libs.window as _window -from pandas.tseries.offsets import DateOffset + from pandas import compat from pandas.compat.numpy import function as nv from pandas.util._decorators import (Substitution, Appender, @@ -254,7 +254,8 @@ def _wrap_result(self, result, block=None, obj=None): # coerce if necessary if block is not None: if is_timedelta64_dtype(block.values.dtype): - result = pd.to_timedelta( + from pandas import to_timedelta + result = to_timedelta( result.ravel(), unit='ns').values.reshape(result.shape) if result.ndim == 1: @@ -275,7 +276,7 @@ def _wrap_results(self, results, blocks, obj): obj : conformed data (may be resampled) """ - from pandas import Series + from pandas import Series, concat from pandas.core.index import _ensure_index final = [] @@ -309,7 +310,7 @@ def _wrap_results(self, results, blocks, obj): if not len(final): return obj.astype('float64') - return pd.concat(final, axis=1).reindex(columns=columns, + return concat(final, axis=1).reindex(columns=columns, copy=False) def _center_window(self, result, window): @@ -318,10 +319,9 @@ def _center_window(self, result, window): raise ValueError("Requested axis is larger then no. of argument " "dimensions") - from pandas import Series, DataFrame offset = _offset(window, True) if offset > 0: - if isinstance(result, (Series, DataFrame)): + if isinstance(result, (ABCSeries, ABCDataFrame)): result = result.slice_shift(-offset, axis=self.axis) else: lead_indexer = [slice(None)] * result.ndim @@ -1085,7 +1085,8 @@ def _on(self): return self.obj.index elif (isinstance(self.obj, ABCDataFrame) and self.on in self.obj.columns): - return pd.Index(self.obj[self.on]) + from pandas import Index + return Index(self.obj[self.on]) else: raise ValueError("invalid on specified as {0}, " "must be a column (if DataFrame) " @@ -1096,7 +1097,7 @@ def validate(self): # we allow rolling on a datetimelike index if ((self.obj.empty or self.is_datetimelike) and - isinstance(self.window, (compat.string_types, DateOffset, + isinstance(self.window, (compat.string_types, ABCDateOffset, timedelta))): self._validate_monotonic() @@ -1871,19 +1872,19 @@ def _cov(x, y): def _flex_binary_moment(arg1, arg2, f, pairwise=False): - from pandas import Series, DataFrame - if not (isinstance(arg1, (np.ndarray, Series, DataFrame)) and - isinstance(arg2, (np.ndarray, Series, DataFrame))): + if not (isinstance(arg1, (np.ndarray, ABCSeries, ABCDataFrame)) and + isinstance(arg2, (np.ndarray, ABCSeries, ABCDataFrame))): raise TypeError("arguments to moment function must be of type " "np.ndarray/Series/DataFrame") - if (isinstance(arg1, (np.ndarray, Series)) and - isinstance(arg2, (np.ndarray, Series))): + if (isinstance(arg1, (np.ndarray, ABCSeries)) and + isinstance(arg2, (np.ndarray, ABCSeries))): X, Y = _prep_binary(arg1, arg2) return f(X, Y) - elif isinstance(arg1, DataFrame): + elif isinstance(arg1, ABCDataFrame): + from pandas import DataFrame def dataframe_from_int_dict(data, frame_template): result = DataFrame(data, index=frame_template.index) @@ -1892,7 +1893,7 @@ def dataframe_from_int_dict(data, frame_template): return result results = {} - if isinstance(arg2, DataFrame): + if isinstance(arg2, ABCDataFrame): if pairwise is False: if arg1 is arg2: # special case in order to handle duplicate column names @@ -1929,7 +1930,7 @@ def dataframe_from_int_dict(data, frame_template): # TODO: not the most efficient (perf-wise) # though not bad code-wise - from pandas import Panel, MultiIndex + from pandas import Panel, MultiIndex, concat with warnings.catch_warnings(record=True): p = Panel.from_dict(results).swapaxes('items', 'major') @@ -1939,7 +1940,7 @@ def dataframe_from_int_dict(data, frame_template): p.minor_axis = arg2.columns[p.minor_axis] if len(p.items): - result = pd.concat( + result = concat( [p.iloc[i].T for i in range(len(p.items))], keys=p.items) else: @@ -2034,8 +2035,7 @@ def _zsqrt(x): result = np.sqrt(x) mask = x < 0 - from pandas import DataFrame - if isinstance(x, DataFrame): + if isinstance(x, ABCDataFrame): if mask.values.any(): result[mask] = 0 else: @@ -2060,8 +2060,7 @@ def _prep_binary(arg1, arg2): def rolling(obj, win_type=None, **kwds): - from pandas import Series, DataFrame - if not isinstance(obj, (Series, DataFrame)): + if not isinstance(obj, (ABCSeries, ABCDataFrame)): raise TypeError('invalid type: %s' % type(obj)) if win_type is not None: @@ -2074,8 +2073,7 @@ def rolling(obj, win_type=None, **kwds): def expanding(obj, **kwds): - from pandas import Series, DataFrame - if not isinstance(obj, (Series, DataFrame)): + if not isinstance(obj, (ABCSeries, ABCDataFrame)): raise TypeError('invalid type: %s' % type(obj)) return Expanding(obj, **kwds) @@ -2085,8 +2083,7 @@ def expanding(obj, **kwds): def ewm(obj, **kwds): - from pandas import Series, DataFrame - if not isinstance(obj, (Series, DataFrame)): + if not isinstance(obj, (ABCSeries, ABCDataFrame)): raise TypeError('invalid type: %s' % type(obj)) return EWM(obj, **kwds) From f525d13791be492d264fc20459d67c65e1a8d198 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 11 Aug 2017 18:23:49 -0700 Subject: [PATCH 2/2] Flake8 alignment fixup --- pandas/core/window.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/core/window.py b/pandas/core/window.py index fbd9e89bc64b8..4bd959f52673c 100644 --- a/pandas/core/window.py +++ b/pandas/core/window.py @@ -291,8 +291,7 @@ def _wrap_results(self, results, blocks, obj): # we want to put it back into the results # in the same location columns = self._selected_obj.columns - if self.on is not None \ - and not self._on.equals(obj.index): + if self.on is not None and not self._on.equals(obj.index): name = self._on.name final.append(Series(self._on, index=obj.index, name=name)) @@ -310,8 +309,7 @@ def _wrap_results(self, results, blocks, obj): if not len(final): return obj.astype('float64') - return concat(final, axis=1).reindex(columns=columns, - copy=False) + return concat(final, axis=1).reindex(columns=columns, copy=False) def _center_window(self, result, window): """ center the result in the window """