Skip to content

Commit e2ffa72

Browse files
jbrockmendelalanbato
authored andcommitted
Remove import of pandas as pd in core.window (pandas-dev#17233)
1 parent 03b63cf commit e2ffa72

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

pandas/core/window.py

+25-30
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
ABCDataFrame,
1818
ABCDatetimeIndex,
1919
ABCTimedeltaIndex,
20-
ABCPeriodIndex)
20+
ABCPeriodIndex,
21+
ABCDateOffset)
2122
from pandas.core.dtypes.common import (
2223
is_integer,
2324
is_bool,
@@ -28,13 +29,12 @@
2829
is_list_like,
2930
_ensure_float64,
3031
is_scalar)
31-
import pandas as pd
3232

3333
from pandas.core.base import (PandasObject, SelectionMixin,
3434
GroupByMixin)
3535
import pandas.core.common as com
3636
import pandas._libs.window as _window
37-
from pandas.tseries.offsets import DateOffset
37+
3838
from pandas import compat
3939
from pandas.compat.numpy import function as nv
4040
from pandas.util._decorators import (Substitution, Appender,
@@ -254,7 +254,8 @@ def _wrap_result(self, result, block=None, obj=None):
254254
# coerce if necessary
255255
if block is not None:
256256
if is_timedelta64_dtype(block.values.dtype):
257-
result = pd.to_timedelta(
257+
from pandas import to_timedelta
258+
result = to_timedelta(
258259
result.ravel(), unit='ns').values.reshape(result.shape)
259260

260261
if result.ndim == 1:
@@ -275,7 +276,7 @@ def _wrap_results(self, results, blocks, obj):
275276
obj : conformed data (may be resampled)
276277
"""
277278

278-
from pandas import Series
279+
from pandas import Series, concat
279280
from pandas.core.index import _ensure_index
280281

281282
final = []
@@ -290,8 +291,7 @@ def _wrap_results(self, results, blocks, obj):
290291
# we want to put it back into the results
291292
# in the same location
292293
columns = self._selected_obj.columns
293-
if self.on is not None \
294-
and not self._on.equals(obj.index):
294+
if self.on is not None and not self._on.equals(obj.index):
295295

296296
name = self._on.name
297297
final.append(Series(self._on, index=obj.index, name=name))
@@ -309,19 +309,17 @@ def _wrap_results(self, results, blocks, obj):
309309

310310
if not len(final):
311311
return obj.astype('float64')
312-
return pd.concat(final, axis=1).reindex(columns=columns,
313-
copy=False)
312+
return concat(final, axis=1).reindex(columns=columns, copy=False)
314313

315314
def _center_window(self, result, window):
316315
""" center the result in the window """
317316
if self.axis > result.ndim - 1:
318317
raise ValueError("Requested axis is larger then no. of argument "
319318
"dimensions")
320319

321-
from pandas import Series, DataFrame
322320
offset = _offset(window, True)
323321
if offset > 0:
324-
if isinstance(result, (Series, DataFrame)):
322+
if isinstance(result, (ABCSeries, ABCDataFrame)):
325323
result = result.slice_shift(-offset, axis=self.axis)
326324
else:
327325
lead_indexer = [slice(None)] * result.ndim
@@ -1085,7 +1083,8 @@ def _on(self):
10851083
return self.obj.index
10861084
elif (isinstance(self.obj, ABCDataFrame) and
10871085
self.on in self.obj.columns):
1088-
return pd.Index(self.obj[self.on])
1086+
from pandas import Index
1087+
return Index(self.obj[self.on])
10891088
else:
10901089
raise ValueError("invalid on specified as {0}, "
10911090
"must be a column (if DataFrame) "
@@ -1096,7 +1095,7 @@ def validate(self):
10961095

10971096
# we allow rolling on a datetimelike index
10981097
if ((self.obj.empty or self.is_datetimelike) and
1099-
isinstance(self.window, (compat.string_types, DateOffset,
1098+
isinstance(self.window, (compat.string_types, ABCDateOffset,
11001099
timedelta))):
11011100

11021101
self._validate_monotonic()
@@ -1871,19 +1870,19 @@ def _cov(x, y):
18711870

18721871

18731872
def _flex_binary_moment(arg1, arg2, f, pairwise=False):
1874-
from pandas import Series, DataFrame
18751873

1876-
if not (isinstance(arg1, (np.ndarray, Series, DataFrame)) and
1877-
isinstance(arg2, (np.ndarray, Series, DataFrame))):
1874+
if not (isinstance(arg1, (np.ndarray, ABCSeries, ABCDataFrame)) and
1875+
isinstance(arg2, (np.ndarray, ABCSeries, ABCDataFrame))):
18781876
raise TypeError("arguments to moment function must be of type "
18791877
"np.ndarray/Series/DataFrame")
18801878

1881-
if (isinstance(arg1, (np.ndarray, Series)) and
1882-
isinstance(arg2, (np.ndarray, Series))):
1879+
if (isinstance(arg1, (np.ndarray, ABCSeries)) and
1880+
isinstance(arg2, (np.ndarray, ABCSeries))):
18831881
X, Y = _prep_binary(arg1, arg2)
18841882
return f(X, Y)
18851883

1886-
elif isinstance(arg1, DataFrame):
1884+
elif isinstance(arg1, ABCDataFrame):
1885+
from pandas import DataFrame
18871886

18881887
def dataframe_from_int_dict(data, frame_template):
18891888
result = DataFrame(data, index=frame_template.index)
@@ -1892,7 +1891,7 @@ def dataframe_from_int_dict(data, frame_template):
18921891
return result
18931892

18941893
results = {}
1895-
if isinstance(arg2, DataFrame):
1894+
if isinstance(arg2, ABCDataFrame):
18961895
if pairwise is False:
18971896
if arg1 is arg2:
18981897
# special case in order to handle duplicate column names
@@ -1929,7 +1928,7 @@ def dataframe_from_int_dict(data, frame_template):
19291928

19301929
# TODO: not the most efficient (perf-wise)
19311930
# though not bad code-wise
1932-
from pandas import Panel, MultiIndex
1931+
from pandas import Panel, MultiIndex, concat
19331932

19341933
with warnings.catch_warnings(record=True):
19351934
p = Panel.from_dict(results).swapaxes('items', 'major')
@@ -1939,7 +1938,7 @@ def dataframe_from_int_dict(data, frame_template):
19391938
p.minor_axis = arg2.columns[p.minor_axis]
19401939

19411940
if len(p.items):
1942-
result = pd.concat(
1941+
result = concat(
19431942
[p.iloc[i].T for i in range(len(p.items))],
19441943
keys=p.items)
19451944
else:
@@ -2034,8 +2033,7 @@ def _zsqrt(x):
20342033
result = np.sqrt(x)
20352034
mask = x < 0
20362035

2037-
from pandas import DataFrame
2038-
if isinstance(x, DataFrame):
2036+
if isinstance(x, ABCDataFrame):
20392037
if mask.values.any():
20402038
result[mask] = 0
20412039
else:
@@ -2060,8 +2058,7 @@ def _prep_binary(arg1, arg2):
20602058

20612059

20622060
def rolling(obj, win_type=None, **kwds):
2063-
from pandas import Series, DataFrame
2064-
if not isinstance(obj, (Series, DataFrame)):
2061+
if not isinstance(obj, (ABCSeries, ABCDataFrame)):
20652062
raise TypeError('invalid type: %s' % type(obj))
20662063

20672064
if win_type is not None:
@@ -2074,8 +2071,7 @@ def rolling(obj, win_type=None, **kwds):
20742071

20752072

20762073
def expanding(obj, **kwds):
2077-
from pandas import Series, DataFrame
2078-
if not isinstance(obj, (Series, DataFrame)):
2074+
if not isinstance(obj, (ABCSeries, ABCDataFrame)):
20792075
raise TypeError('invalid type: %s' % type(obj))
20802076

20812077
return Expanding(obj, **kwds)
@@ -2085,8 +2081,7 @@ def expanding(obj, **kwds):
20852081

20862082

20872083
def ewm(obj, **kwds):
2088-
from pandas import Series, DataFrame
2089-
if not isinstance(obj, (Series, DataFrame)):
2084+
if not isinstance(obj, (ABCSeries, ABCDataFrame)):
20902085
raise TypeError('invalid type: %s' % type(obj))
20912086

20922087
return EWM(obj, **kwds)

0 commit comments

Comments
 (0)