diff --git a/pandas/core/categorical.py b/pandas/core/categorical.py index 7dfdc88dddbff..5b3e9e8a22b12 100644 --- a/pandas/core/categorical.py +++ b/pandas/core/categorical.py @@ -18,7 +18,7 @@ from pandas.core.common import (CategoricalDtype, ABCSeries, isnull, notnull, is_categorical_dtype, is_integer_dtype, is_object_dtype, _possibly_infer_to_datetimelike, get_dtype_kinds, - is_list_like, _is_sequence, + is_list_like, is_sequence, _ensure_platform_int, _ensure_object, _ensure_int64, _coerce_indexer_dtype, _values_from_object, take_1d) from pandas.util.terminal import get_terminal_size @@ -1477,7 +1477,7 @@ def _convert_to_list_like(list_like): return list_like if isinstance(list_like, list): return list_like - if (_is_sequence(list_like) or isinstance(list_like, tuple) + if (is_sequence(list_like) or isinstance(list_like, tuple) or isinstance(list_like, types.GeneratorType)): return list(list_like) elif np.isscalar(list_like): diff --git a/pandas/core/common.py b/pandas/core/common.py index 6aff67412d677..f7f944bb418e9 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -2504,7 +2504,7 @@ def is_list_like(arg): not isinstance(arg, compat.string_and_binary_types)) -def _is_sequence(x): +def is_sequence(x): try: iter(x) len(x) # it has a length @@ -2512,6 +2512,7 @@ def _is_sequence(x): except (TypeError, AttributeError): return False + def _get_callable_name(obj): # typical case has name if hasattr(obj, '__name__'): @@ -3093,7 +3094,7 @@ def as_escaped_unicode(thing, escape_chars=escape_chars): elif (isinstance(thing, dict) and _nest_lvl < get_option("display.pprint_nest_depth")): result = _pprint_dict(thing, _nest_lvl, quote_strings=True) - elif _is_sequence(thing) and _nest_lvl < \ + elif is_sequence(thing) and _nest_lvl < \ get_option("display.pprint_nest_depth"): result = _pprint_seq(thing, _nest_lvl, escape_chars=escape_chars, quote_strings=quote_strings) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a464b687209cb..7c7872cf7b6a5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -24,7 +24,7 @@ import numpy.ma as ma from pandas.core.common import (isnull, notnull, PandasError, _try_sort, - _default_index, _maybe_upcast, _is_sequence, + _default_index, _maybe_upcast, is_sequence, _infer_dtype_from_scalar, _values_from_object, is_list_like, _get_dtype, _maybe_box_datetimelike, is_categorical_dtype) @@ -2255,7 +2255,7 @@ def reindexer(value): elif isinstance(value, Categorical): value = value.copy() - elif (isinstance(value, Index) or _is_sequence(value)): + elif (isinstance(value, Index) or is_sequence(value)): from pandas.core.series import _sanitize_index value = _sanitize_index(value, self.index, copy=False) if not isinstance(value, (np.ndarray, Index)): @@ -2844,7 +2844,7 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False, '(rows)') if not isinstance(by, list): by = [by] - if com._is_sequence(ascending) and len(by) != len(ascending): + if com.is_sequence(ascending) and len(by) != len(ascending): raise ValueError('Length of ascending (%d) != length of by' ' (%d)' % (len(ascending), len(by))) if len(by) > 1: @@ -3694,7 +3694,7 @@ def _apply_standard(self, func, axis, ignore_failures=False, reduce=True): com.pprint_thing(k),) raise - if len(results) > 0 and _is_sequence(results[0]): + if len(results) > 0 and is_sequence(results[0]): if not isinstance(results[0], Series): index = res_columns else: diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 048e4af20d02f..c9322a9371309 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -541,7 +541,7 @@ def _align_series(self, indexer, ser): # we have a frame, with multiple indexers on both axes; and a # series, so need to broadcast (see GH5206) if (sum_aligners == self.ndim and - all([com._is_sequence(_) for _ in indexer])): + all([com.is_sequence(_) for _ in indexer])): ser = ser.reindex(obj.axes[0][indexer[0]], copy=True).values # single indexer @@ -555,7 +555,7 @@ def _align_series(self, indexer, ser): ax = obj.axes[i] # multiple aligners (or null slices) - if com._is_sequence(idx) or isinstance(idx, slice): + if com.is_sequence(idx) or isinstance(idx, slice): if single_aligner and _is_null_slice(idx): continue new_ix = ax[idx] @@ -625,7 +625,7 @@ def _align_frame(self, indexer, df): sindexers = [] for i, ix in enumerate(indexer): ax = self.obj.axes[i] - if com._is_sequence(ix) or isinstance(ix, slice): + if com.is_sequence(ix) or isinstance(ix, slice): if idx is None: idx = ax[ix].ravel() elif cols is None: diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index 0d13b6513b377..9465247056e27 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -25,7 +25,7 @@ def test_mut_exclusive(): def test_is_sequence(): - is_seq = com._is_sequence + is_seq = com.is_sequence assert(is_seq((1, 2))) assert(is_seq([1, 2])) assert(not is_seq("abcd")) diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 4acf4d63d9de7..6a71160a08ae9 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -24,7 +24,7 @@ from numpy.testing import assert_array_equal import pandas as pd -from pandas.core.common import _is_sequence, array_equivalent, is_list_like +from pandas.core.common import is_sequence, array_equivalent, is_list_like import pandas.core.index as index import pandas.core.series as series import pandas.core.frame as frame @@ -945,7 +945,7 @@ def makeCustomIndex(nentries, nlevels, prefix='#', names=False, ndupe_l=None, if ndupe_l is None: ndupe_l = [1] * nlevels - assert (_is_sequence(ndupe_l) and len(ndupe_l) <= nlevels) + assert (is_sequence(ndupe_l) and len(ndupe_l) <= nlevels) assert (names is None or names is False or names is True or len(names) is nlevels) assert idx_type is None or \