API: str.cat will align on Series by h-vetinari · Pull Request #20347 · pandas-dev/pandas (original) (raw)

@h-vetinari, I'll merge this shortly. I'm opening a followup issue.

On Wed, May 2, 2018 at 6:03 AM, Jeff Reback ***@***.***> wrote: ***@***.**** requested changes on this pull request. the checking code needs some work ------------------------------ In pandas/core/strings.py <#20347 (comment)>: > + if ignore_index and fu_wrn else others] + return (los, fu_wrn) + elif isinstance(others, Index): + fu_wrn = not others.equals(idx) + los = [Series(others.values, + index=(idx if ignore_index else others))] + return (los, fu_wrn) + elif isinstance(others, DataFrame): + fu_wrn = not others.index.equals(idx) + if ignore_index and fu_wrn: + # without copy, this could change "others" + # that was passed to str.cat + others = others.copy() + others.index = idx + return ([others[x] for x in others], fu_wrn) + elif isinstance(others, np.ndarray) and others.ndim == 2: this is wrong i don’t think we can align a ndarray at all like this let’s can ndarray a that are > 1 dim ------------------------------ In pandas/core/strings.py <#20347 (comment)>: > + or (not is_list_like(x) and isnull(x)) + or x is None) + for x in nxt)) + # DataFrame is false positive of is_legal + # because "x in df" returns column names + if not is_legal or isinstance(nxt, DataFrame): + raise TypeError(err_msg) + + nxt, fwn = self._get_series_list(nxt, + ignore_index=ignore_index) + los = los + nxt + fu_wrn = fu_wrn or fwn + return (los, fu_wrn) + # test if there is a mix of list-like and non-list-like (e.g. str) + elif (any(is_list_like(x) for x in others) + and any(not is_list_like(x) for x in others)): you can make this simpler by just checking for all is not list like (eg strings) anything else will fail thru to the TypeError ------------------------------ In pandas/core/strings.py <#20347 (comment)>: > + elif isinstance(others, DataFrame): + fu_wrn = not others.index.equals(idx) + if ignore_index and fu_wrn: + # without copy, this could change "others" + # that was passed to str.cat + others = others.copy() + others.index = idx + return ([others[x] for x in others], fu_wrn) + elif isinstance(others, np.ndarray) and others.ndim == 2: + others = DataFrame(others, index=idx) + return ([others[x] for x in others], False) + elif is_list_like(others): + others = list(others) # ensure iterators do not get read twice etc + if all(is_list_like(x) for x in others): + los = [] + fu_wrn = False can u name this parameter just warn ------------------------------ In pandas/core/strings.py <#20347 (comment)>: > + # without copy, this could change "others" + # that was passed to str.cat + others = others.copy() + others.index = idx + return ([others[x] for x in others], fu_wrn) + elif isinstance(others, np.ndarray) and others.ndim == 2: + others = DataFrame(others, index=idx) + return ([others[x] for x in others], False) + elif is_list_like(others): + others = list(others) # ensure iterators do not get read twice etc + if all(is_list_like(x) for x in others): + los = [] + fu_wrn = False + while others: + nxt = others.pop(0) # list-like as per check above + # safety for iterators and other non-persistent list-likes this whole section needs some work it’s way too hard to read and follow ------------------------------ In pandas/core/strings.py <#20347 (comment)>: > + # safety for iterators and other non-persistent list-likes + # do not map indexed/typed objects; would lose information + if not isinstance(nxt, (DataFrame, Series, + Index, np.ndarray)): + nxt = list(nxt) + + # known types without deep inspection + no_deep = ((isinstance(nxt, np.ndarray) and nxt.ndim == 1) + or isinstance(nxt, (Series, Index))) + # Nested list-likes are forbidden - elements of nxt must be + # strings/NaN/None. Need to robustify NaN-check against + # x in nxt being list-like (otherwise ambiguous boolean) + is_legal = ((no_deep and nxt.dtype == object) + or all((isinstance(x, compat.string_types) + or (not is_list_like(x) and isnull(x)) + or x is None) isnull already checks for None only 1d objects are valid here (or all scalars) do this check up front — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#20347 (review)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ABQHIi5osRhzmq7QhxBxozIbONWCN5Yiks5tuZKYgaJpZM4SqkE_> .