@@ -1943,21 +1943,21 @@ def _get_series_list(self, others, ignore_index=False):
1943
1943
1944
1944
Parameters
1945
1945
----------
1946
- input : Series, DataFrame, np.ndarray, list-like or list-like of
1946
+ others : Series, DataFrame, np.ndarray, list-like or list-like of
1947
1947
objects that are either Series, np.ndarray (1-dim) or list-like
1948
1948
ignore_index : boolean, default False
1949
- Determines whether to forcefully align with index of the caller
1949
+ Determines whether to forcefully align others with index of caller
1950
1950
1951
1951
Returns
1952
1952
-------
1953
- tuple : (input transformed into list of Series,
1954
- Boolean whether FutureWarning should be raised)
1953
+ tuple : (others transformed into list of Series,
1954
+ boolean whether FutureWarning should be raised)
1955
1955
"""
1956
1956
1957
1957
# once str.cat defaults to alignment, this function can be simplified;
1958
1958
# will not need `ignore_index` and the second boolean output anymore
1959
1959
1960
- from pandas import Index , Series , DataFrame , isnull
1960
+ from pandas import Index , Series , DataFrame
1961
1961
1962
1962
# self._orig is either Series or Index
1963
1963
idx = self ._orig if isinstance (self ._orig , Index ) else self ._orig .index
@@ -1966,66 +1966,69 @@ def _get_series_list(self, others, ignore_index=False):
1966
1966
'list-like (either containing only strings or containing '
1967
1967
'only objects of type Series/Index/list-like/np.ndarray)' )
1968
1968
1969
+ # Generally speaking, all objects without an index inherit the index
1970
+ # `idx` of the calling Series/Index - i.e. must have matching length.
1971
+ # Objects with an index (i.e. Series/Index/DataFrame) keep their own
1972
+ # index, *unless* ignore_index is set to True.
1969
1973
if isinstance (others , Series ):
1970
- fu_wrn = not others .index .equals (idx )
1974
+ warn = not others .index .equals (idx )
1975
+ # only reconstruct Series when absolutely necessary
1971
1976
los = [Series (others .values , index = idx )
1972
- if ignore_index and fu_wrn else others ]
1973
- return (los , fu_wrn )
1977
+ if ignore_index and warn else others ]
1978
+ return (los , warn )
1974
1979
elif isinstance (others , Index ):
1975
- fu_wrn = not others .equals (idx )
1980
+ warn = not others .equals (idx )
1976
1981
los = [Series (others .values ,
1977
1982
index = (idx if ignore_index else others ))]
1978
- return (los , fu_wrn )
1983
+ return (los , warn )
1979
1984
elif isinstance (others , DataFrame ):
1980
- fu_wrn = not others .index .equals (idx )
1981
- if ignore_index and fu_wrn :
1985
+ warn = not others .index .equals (idx )
1986
+ if ignore_index and warn :
1982
1987
# without copy, this could change "others"
1983
1988
# that was passed to str.cat
1984
1989
others = others .copy ()
1985
1990
others .index = idx
1986
- return ([others [x ] for x in others ], fu_wrn )
1991
+ return ([others [x ] for x in others ], warn )
1987
1992
elif isinstance (others , np .ndarray ) and others .ndim == 2 :
1988
1993
others = DataFrame (others , index = idx )
1989
1994
return ([others [x ] for x in others ], False )
1990
1995
elif is_list_like (others ):
1991
1996
others = list (others ) # ensure iterators do not get read twice etc
1997
+
1998
+ # in case of list-like `others`, all elements must be
1999
+ # either one-dimensional list-likes or scalars
1992
2000
if all (is_list_like (x ) for x in others ):
1993
2001
los = []
1994
- fu_wrn = False
2002
+ warn = False
2003
+ # iterate through list and append list of series for each
2004
+ # element (which we check to be one-dimensional and non-nested)
1995
2005
while others :
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
2006
+ nxt = others .pop (0 ) # nxt is guaranteed list-like by above
1999
2007
if not isinstance (nxt , (DataFrame , Series ,
2000
2008
Index , np .ndarray )):
2009
+ # safety for non-persistent list-likes (e.g. iterators)
2010
+ # do not map indexed/typed objects; info needed below
2001
2011
nxt = list (nxt )
2002
2012
2003
- # known types without deep inspection
2013
+ # known types for which we can avoid deep inspection
2004
2014
no_deep = ((isinstance (nxt , np .ndarray ) and nxt .ndim == 1 )
2005
2015
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)
2016
+ # nested list-likes are forbidden:
2017
+ # -> elements of nxt must not be list-like
2009
2018
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 ))
2019
+ or all (not is_list_like (x ) for x in nxt ))
2020
+
2014
2021
# DataFrame is false positive of is_legal
2015
2022
# because "x in df" returns column names
2016
2023
if not is_legal or isinstance (nxt , DataFrame ):
2017
2024
raise TypeError (err_msg )
2018
2025
2019
- nxt , fwn = self ._get_series_list (nxt ,
2026
+ nxt , wnx = self ._get_series_list (nxt ,
2020
2027
ignore_index = ignore_index )
2021
2028
los = los + nxt
2022
- fu_wrn = fu_wrn or fwn
2023
- return (los , fu_wrn )
2024
- # test if there is a mix of list-like and non-list-like (e.g. str)
2025
- elif (any (is_list_like (x ) for x in others )
2026
- and any (not is_list_like (x ) for x in others )):
2027
- raise TypeError (err_msg )
2028
- else : # all elements in others are _not_ list-like
2029
+ warn = warn or wnx
2030
+ return (los , warn )
2031
+ elif all (not is_list_like (x ) for x in others ):
2029
2032
return ([Series (others , index = idx )], False )
2030
2033
raise TypeError (err_msg )
2031
2034
@@ -2187,8 +2190,8 @@ def cat(self, others=None, sep=None, na_rep=None, join=None):
2187
2190
2188
2191
try :
2189
2192
# turn anything in "others" into lists of Series
2190
- others , fu_wrn = self ._get_series_list (others ,
2191
- ignore_index = (join is None ))
2193
+ others , warn = self ._get_series_list (others ,
2194
+ ignore_index = (join is None ))
2192
2195
except ValueError : # do not catch TypeError raised by _get_series_list
2193
2196
if join is None :
2194
2197
raise ValueError ('All arrays must be same length, except '
@@ -2199,7 +2202,7 @@ def cat(self, others=None, sep=None, na_rep=None, join=None):
2199
2202
'must all be of the same length as the '
2200
2203
'calling Series/Index.' )
2201
2204
2202
- if join is None and fu_wrn :
2205
+ if join is None and warn :
2203
2206
warnings .warn ("A future version of pandas will perform index "
2204
2207
"alignment when `others` is a Series/Index/"
2205
2208
"DataFrame (or a list-like containing one). To "
0 commit comments