Skip to content

Commit 58a8e42

Browse files
committed
Avoid deep inspection for known types in _get_series_list
1 parent fee1c54 commit 58a8e42

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

doc/source/text.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ All one-dimensional list-likes can be arbitrarily combined in a list-like contai
311311
312312
s
313313
u
314-
s.str.cat([u, pd.Index(u.values), ['A', 'B', 'C', 'D']], na_rep='-')
314+
s.str.cat([u, pd.Index(u.values), ['A', 'B', 'C', 'D'], map(int, u.index)], na_rep='-')
315315
316316
All elements must match in length to the calling ``Series`` (or ``Index``), except those having an index if ``join`` is not None:
317317

pandas/core/strings.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -1979,8 +1979,8 @@ def _get_series_list(self, others, ignore_index=False):
19791979
elif isinstance(others, DataFrame):
19801980
fut_warn = not others.index.equals(idx)
19811981
if ignore_index and fut_warn:
1982-
# without copy, this could change (the corresponding list
1983-
# element of) "others" that was passed to str.cat
1982+
# without copy, this could change "others"
1983+
# that was passed to str.cat
19841984
others = others.copy()
19851985
others.index = idx
19861986
return ([others[x] for x in others], fut_warn)
@@ -1993,22 +1993,27 @@ def _get_series_list(self, others, ignore_index=False):
19931993
los = []
19941994
fut_warn = False
19951995
while others:
1996-
nxt = others.pop(0)
1997-
# safety for iterators etc.; nxt is list-like as per above
1998-
# do not map indexed objects, which would lose their index
1999-
if not isinstance(nxt, (DataFrame, Series, Index)):
1996+
nxt = others.pop(0) # list-like as per check above
1997+
# safety for iterators and other non-persistent list-likes
1998+
# do not map indexed/typed objects; would lose information
1999+
if not isinstance(nxt, (DataFrame, Series,
2000+
Index, np.ndarray)):
20002001
nxt = list(nxt)
20012002

2002-
# Nested list-likes are forbidden - content of nxt must be
2003-
# strings/NaN/None. Need to robustify check against
2004-
# x in nxt being list-like (otherwise ambiguous boolean).
2005-
is_legal = all((isinstance(x, compat.string_types)
2006-
or (not is_list_like(x) and isnull(x))
2007-
or x is None)
2008-
for x in nxt)
2003+
# known types without deep inspection
2004+
no_deep = ((isinstance(nxt, np.ndarray) and nxt.ndim == 1)
2005+
or isinstance(nxt, (Series, Index)))
2006+
# Nested list-likes are forbidden - elements of nxt must be
2007+
# strings/NaN/None. Need to robustify NaN-check against
2008+
# x in nxt being list-like (otherwise ambiguous boolean)
2009+
is_legal = ((no_deep and nxt.dtype == object)
2010+
or all((isinstance(x, compat.string_types)
2011+
or (not is_list_like(x) and isnull(x))
2012+
or x is None)
2013+
for x in nxt))
20092014
# DataFrame is false positive of is_legal
20102015
# because "x in df" returns column names
2011-
if isinstance(nxt, DataFrame) or not is_legal:
2016+
if not is_legal or isinstance(nxt, DataFrame):
20122017
raise TypeError(err_msg)
20132018

20142019
tmp = self._get_series_list(nxt, ignore_index=ignore_index)

pandas/tests/test_strings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import pandas.core.strings as strings
2020

2121

22-
def assert_series_or_index_equal(left, right, expect_warn=False):
22+
def assert_series_or_index_equal(left, right):
2323
if isinstance(left, Series):
2424
assert_series_equal(left, right)
2525
else: # Index

0 commit comments

Comments
 (0)