Skip to content

Commit a9d8e04

Browse files
jbrockmendeljreback
authored andcommitted
cleanup inconsistently used imports (#19292)
1 parent 4003a74 commit a9d8e04

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+304
-334
lines changed

ci/lint.sh

+9
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ if [ "$LINT" ]; then
9191
fi
9292
echo "Check for invalid testing DONE"
9393

94+
# Check for imports from pandas.core.common instead
95+
# of `import pandas.core.common as com`
96+
echo "Check for non-standard imports"
97+
grep -R --include="*.py*" -E "from pandas.core.common import " pandas
98+
if [ $? = "0" ]; then
99+
RET=1
100+
fi
101+
echo "Check for non-standard imports DONE"
102+
94103
echo "Check for use of lists instead of generators in built-in Python functions"
95104

96105
# Example: Avoid `any([i for i in some_iterator])` in favor of `any(i for i in some_iterator)`

pandas/core/arrays/categorical.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
is_list_like, is_sequence,
2929
is_scalar,
3030
is_dict_like)
31-
from pandas.core.common import is_null_slice, _maybe_box_datetimelike
3231

3332
from pandas.core.algorithms import factorize, take_1d, unique1d
3433
from pandas.core.accessor import PandasDelegate
@@ -468,7 +467,7 @@ def tolist(self):
468467
(for Timestamp/Timedelta/Interval/Period)
469468
"""
470469
if is_datetimelike(self.categories):
471-
return [_maybe_box_datetimelike(x) for x in self]
470+
return [com._maybe_box_datetimelike(x) for x in self]
472471
return np.array(self).tolist()
473472

474473
@property
@@ -1686,7 +1685,7 @@ def _slice(self, slicer):
16861685
# only allow 1 dimensional slicing, but can
16871686
# in a 2-d case be passd (slice(None),....)
16881687
if isinstance(slicer, tuple) and len(slicer) == 2:
1689-
if not is_null_slice(slicer[0]):
1688+
if not com.is_null_slice(slicer[0]):
16901689
raise AssertionError("invalid slicing for a 1-ndim "
16911690
"categorical")
16921691
slicer = slicer[1]
@@ -1847,7 +1846,7 @@ def __setitem__(self, key, value):
18471846
# only allow 1 dimensional slicing, but can
18481847
# in a 2-d case be passd (slice(None),....)
18491848
if len(key) == 2:
1850-
if not is_null_slice(key[0]):
1849+
if not com.is_null_slice(key[0]):
18511850
raise AssertionError("invalid slicing for a 1-ndim "
18521851
"categorical")
18531852
key = key[1]

pandas/core/base.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from pandas.compat import PYPY
2525
from pandas.util._decorators import (Appender, cache_readonly,
2626
deprecate_kwarg, Substitution)
27-
from pandas.core.common import AbstractMethodError, _maybe_box_datetimelike
2827

2928
from pandas.core.accessor import DirNamesMixin
3029

@@ -46,7 +45,7 @@ class StringMixin(object):
4645
# Formatting
4746

4847
def __unicode__(self):
49-
raise AbstractMethodError(self)
48+
raise com.AbstractMethodError(self)
5049

5150
def __str__(self):
5251
"""
@@ -278,10 +277,10 @@ def _gotitem(self, key, ndim, subset=None):
278277
subset to act on
279278
280279
"""
281-
raise AbstractMethodError(self)
280+
raise com.AbstractMethodError(self)
282281

283282
def aggregate(self, func, *args, **kwargs):
284-
raise AbstractMethodError(self)
283+
raise com.AbstractMethodError(self)
285284

286285
agg = aggregate
287286

@@ -815,7 +814,7 @@ def tolist(self):
815814
"""
816815

817816
if is_datetimelike(self):
818-
return [_maybe_box_datetimelike(x) for x in self._values]
817+
return [com._maybe_box_datetimelike(x) for x in self._values]
819818
else:
820819
return self._values.tolist()
821820

@@ -1238,4 +1237,4 @@ def duplicated(self, keep='first'):
12381237
# abstracts
12391238

12401239
def _update_inplace(self, result, **kwargs):
1241-
raise AbstractMethodError(self)
1240+
raise com.AbstractMethodError(self)

pandas/core/computation/align.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import pandas as pd
1111
from pandas import compat
1212
from pandas.errors import PerformanceWarning
13-
from pandas.core.common import flatten
13+
import pandas.core.common as com
1414
from pandas.core.computation.common import _result_type_many
1515

1616

@@ -117,7 +117,7 @@ def _align(terms):
117117
"""Align a set of terms"""
118118
try:
119119
# flatten the parse tree (a nested list, really)
120-
terms = list(flatten(terms))
120+
terms = list(com.flatten(terms))
121121
except TypeError:
122122
# can't iterate so it must just be a constant or single variable
123123
if isinstance(terms.value, pd.core.generic.NDFrame):

pandas/core/computation/expressions.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import warnings
1010
import numpy as np
11-
from pandas.core.common import _values_from_object
11+
12+
import pandas.core.common as com
1213
from pandas.core.computation.check import _NUMEXPR_INSTALLED
1314
from pandas.core.config import get_option
1415

@@ -122,8 +123,8 @@ def _evaluate_numexpr(op, op_str, a, b, truediv=True,
122123

123124

124125
def _where_standard(cond, a, b):
125-
return np.where(_values_from_object(cond), _values_from_object(a),
126-
_values_from_object(b))
126+
return np.where(com._values_from_object(cond), com._values_from_object(a),
127+
com._values_from_object(b))
127128

128129

129130
def _where_numexpr(cond, a, b):

pandas/core/frame.py

+24-32
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@
6262
from pandas.core.dtypes.missing import isna, notna
6363

6464

65-
from pandas.core.common import (_try_sort,
66-
_default_index,
67-
_values_from_object,
68-
_maybe_box_datetimelike,
69-
_dict_compat,
70-
standardize_mapping)
7165
from pandas.core.generic import NDFrame, _shared_docs
7266
from pandas.core.index import (Index, MultiIndex, _ensure_index,
7367
_ensure_index_from_sequences)
@@ -387,9 +381,9 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
387381
if isinstance(data[0], Series):
388382
index = _get_names_from_index(data)
389383
elif isinstance(data[0], Categorical):
390-
index = _default_index(len(data[0]))
384+
index = com._default_index(len(data[0]))
391385
else:
392-
index = _default_index(len(data))
386+
index = com._default_index(len(data))
393387

394388
mgr = _arrays_to_mgr(arrays, columns, index, columns,
395389
dtype=dtype)
@@ -466,7 +460,7 @@ def _init_dict(self, data, index, columns, dtype=None):
466460
else:
467461
keys = list(data.keys())
468462
if not isinstance(data, OrderedDict):
469-
keys = _try_sort(keys)
463+
keys = com._try_sort(keys)
470464
columns = data_names = Index(keys)
471465
arrays = [data[k] for k in keys]
472466

@@ -493,12 +487,12 @@ def _get_axes(N, K, index=index, columns=columns):
493487
# return axes or defaults
494488

495489
if index is None:
496-
index = _default_index(N)
490+
index = com._default_index(N)
497491
else:
498492
index = _ensure_index(index)
499493

500494
if columns is None:
501-
columns = _default_index(K)
495+
columns = com._default_index(K)
502496
else:
503497
columns = _ensure_index(columns)
504498
return index, columns
@@ -990,7 +984,7 @@ def to_dict(self, orient='dict', into=dict):
990984
"columns will be omitted.", UserWarning,
991985
stacklevel=2)
992986
# GH16122
993-
into_c = standardize_mapping(into)
987+
into_c = com.standardize_mapping(into)
994988
if orient.lower().startswith('d'):
995989
return into_c(
996990
(k, v.to_dict(into)) for k, v in compat.iteritems(self))
@@ -1000,13 +994,13 @@ def to_dict(self, orient='dict', into=dict):
1000994
return into_c((('index', self.index.tolist()),
1001995
('columns', self.columns.tolist()),
1002996
('data', lib.map_infer(self.values.ravel(),
1003-
_maybe_box_datetimelike)
997+
com._maybe_box_datetimelike)
1004998
.reshape(self.values.shape).tolist())))
1005999
elif orient.lower().startswith('s'):
1006-
return into_c((k, _maybe_box_datetimelike(v))
1000+
return into_c((k, com._maybe_box_datetimelike(v))
10071001
for k, v in compat.iteritems(self))
10081002
elif orient.lower().startswith('r'):
1009-
return [into_c((k, _maybe_box_datetimelike(v))
1003+
return [into_c((k, com._maybe_box_datetimelike(v))
10101004
for k, v in zip(self.columns, np.atleast_1d(row)))
10111005
for row in self.values]
10121006
elif orient.lower().startswith('i'):
@@ -1947,30 +1941,28 @@ def transpose(self, *args, **kwargs):
19471941

19481942
# legacy pickle formats
19491943
def _unpickle_frame_compat(self, state): # pragma: no cover
1950-
from pandas.core.common import _unpickle_array
19511944
if len(state) == 2: # pragma: no cover
19521945
series, idx = state
19531946
columns = sorted(series)
19541947
else:
19551948
series, cols, idx = state
1956-
columns = _unpickle_array(cols)
1949+
columns = com._unpickle_array(cols)
19571950

1958-
index = _unpickle_array(idx)
1951+
index = com._unpickle_array(idx)
19591952
self._data = self._init_dict(series, index, columns, None)
19601953

19611954
def _unpickle_matrix_compat(self, state): # pragma: no cover
1962-
from pandas.core.common import _unpickle_array
19631955
# old unpickling
19641956
(vals, idx, cols), object_state = state
19651957

1966-
index = _unpickle_array(idx)
1967-
dm = DataFrame(vals, index=index, columns=_unpickle_array(cols),
1958+
index = com._unpickle_array(idx)
1959+
dm = DataFrame(vals, index=index, columns=com._unpickle_array(cols),
19681960
copy=False)
19691961

19701962
if object_state is not None:
19711963
ovals, _, ocols = object_state
19721964
objects = DataFrame(ovals, index=index,
1973-
columns=_unpickle_array(ocols), copy=False)
1965+
columns=com._unpickle_array(ocols), copy=False)
19741966

19751967
dm = dm.join(objects)
19761968

@@ -2006,7 +1998,7 @@ def _get_value(self, index, col, takeable=False):
20061998

20071999
if takeable:
20082000
series = self._iget_item_cache(col)
2009-
return _maybe_box_datetimelike(series._values[index])
2001+
return com._maybe_box_datetimelike(series._values[index])
20102002

20112003
series = self._get_item_cache(col)
20122004
engine = self.index._engine
@@ -3371,7 +3363,7 @@ def _maybe_casted_values(index, labels=None):
33713363
values, mask, np.nan)
33723364
return values
33733365

3374-
new_index = _default_index(len(new_obj))
3366+
new_index = com._default_index(len(new_obj))
33753367
if level is not None:
33763368
if not isinstance(level, (tuple, list)):
33773369
level = [level]
@@ -6084,7 +6076,7 @@ def extract_index(data):
60846076
(lengths[0], len(index)))
60856077
raise ValueError(msg)
60866078
else:
6087-
index = _default_index(lengths[0])
6079+
index = com._default_index(lengths[0])
60886080

60896081
return _ensure_index(index)
60906082

@@ -6155,7 +6147,7 @@ def _to_arrays(data, columns, coerce_float=False, dtype=None):
61556147
dtype=dtype)
61566148
elif isinstance(data[0], Categorical):
61576149
if columns is None:
6158-
columns = _default_index(len(data))
6150+
columns = com._default_index(len(data))
61596151
return data, columns
61606152
elif (isinstance(data, (np.ndarray, Series, Index)) and
61616153
data.dtype.names is not None):
@@ -6179,7 +6171,7 @@ def _masked_rec_array_to_mgr(data, index, columns, dtype, copy):
61796171
if index is None:
61806172
index = _get_names_from_index(fdata)
61816173
if index is None:
6182-
index = _default_index(len(data))
6174+
index = com._default_index(len(data))
61836175
index = _ensure_index(index)
61846176

61856177
if columns is not None:
@@ -6239,14 +6231,14 @@ def _list_of_series_to_arrays(data, columns, coerce_float=False, dtype=None):
62396231
for s in data:
62406232
index = getattr(s, 'index', None)
62416233
if index is None:
6242-
index = _default_index(len(s))
6234+
index = com._default_index(len(s))
62436235

62446236
if id(index) in indexer_cache:
62456237
indexer = indexer_cache[id(index)]
62466238
else:
62476239
indexer = indexer_cache[id(index)] = index.get_indexer(columns)
62486240

6249-
values = _values_from_object(s)
6241+
values = com._values_from_object(s)
62506242
aligned_values.append(algorithms.take_1d(values, indexer))
62516243

62526244
values = np.vstack(aligned_values)
@@ -6276,7 +6268,7 @@ def _list_of_dict_to_arrays(data, columns, coerce_float=False, dtype=None):
62766268

62776269
def _convert_object_array(content, columns, coerce_float=False, dtype=None):
62786270
if columns is None:
6279-
columns = _default_index(len(content))
6271+
columns = com._default_index(len(content))
62806272
else:
62816273
if len(columns) != len(content): # pragma: no cover
62826274
# caller's responsibility to check for this...
@@ -6298,7 +6290,7 @@ def convert(arr):
62986290
def _get_names_from_index(data):
62996291
has_some_name = any(getattr(s, 'name', None) is not None for s in data)
63006292
if not has_some_name:
6301-
return _default_index(len(data))
6293+
return com._default_index(len(data))
63026294

63036295
index = lrange(len(data))
63046296
count = 0
@@ -6333,7 +6325,7 @@ def _homogenize(data, index, dtype=None):
63336325
oindex = index.astype('O')
63346326

63356327
if isinstance(index, (DatetimeIndex, TimedeltaIndex)):
6336-
v = _dict_compat(v)
6328+
v = com._dict_compat(v)
63376329
else:
63386330
v = dict(v)
63396331
v = lib.fast_multiget(v, oindex.values, default=np.nan)

0 commit comments

Comments
 (0)