8000 API: str.cat will align on Series by h-vetinari · Pull Request #20347 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

API: str.cat will align on Series #20347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 2, 2018
Prev Previous commit
Next Next commit
Revert cat-output-for-cat-caller propsal
  • Loading branch information
h-vetinari committed May 2, 2018
commit 2143f190bf5c0a6cc1c5800bc52fc96bcbead156
4 changes: 1 addition & 3 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,7 @@ In v.0.23 `join` will default to None (meaning no alignment), but this default w
s.str.cat(t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too long examples here

s.str.cat(t, join='left', na_rep='-')

Furthermore:
- meth:`Series.str.cat` now works as well for ``CategoricalIndex`` as well (previously raised a ``ValueError``; see :issue:`20842`)
- If concatenating with something (i.e. `others is not None`) the resulting ``Series``/``Index`` will now remain categorical if the calling ``Series``/``Index`` is categorical (see :issue:`20843`)
Furthermore, meth:`Series.str.cat` now works for ``CategoricalIndex`` as well (previously raised a ``ValueError``; see :issue:`20842`).

.. _whatsnew_0230.enhancements.astype_category:

Expand Down
5 changes: 2 additions & 3 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2217,11 +2217,10 @@ def cat(self, others=None, sep=None, na_rep=None, join=None):
# str_cat discards index
res = str_cat(data, others=others, sep=sep, na_rep=na_rep)

dtype = 'category' if self._is_categorical else None
if isinstance(self._orig, Index):
res = Index(res, dtype=dtype)
res = Index(res)
else: # Series
res = Series(res, index=data.index, dtype=dtype)
res = Series(res, index=data.index)
return res

@copy(str_split)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ def test_str_accessor_api_for_categorical(self):

# str functions, which need special arguments
special_func_defs = [
('cat', (list("zyxw"),), {"sep": ","}),
('center', (10,), {}),
('contains', ("a",), {}),
('count', ("a",), {}),
Expand Down Expand Up @@ -643,12 +644,11 @@ def test_str_accessor_api_for_categorical(self):
]
_special_func_names = [f[0] for f in special_func_defs]

# * cat tested extensively with categorical data in test_strings.py
# * get, join: they need a individual elements of type lists, but
# we can't make a categorical with lists as individual categories.
# -> `s.str.split(" ").astype("category")` will error!
# * `translate` has different interfaces for py2 vs. py3
_ignore_names = ["cat", "get", "join", "translate"]
_ignore_names = ["get", "join", "translate"]

str_func_names = [f for f in dir(s.str) if not (
f.startswith("_") or
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def test_str_cat_categorical(self, series_or_index,
s = Series(s)
t = Index(['b', 'a', 'b', 'c'], dtype=dtype_target)

exp = Index(['ab', 'aa', 'bb', 'ac'], dtype=dtype_caller)
exp = Index(['ab', 'aa', 'bb', 'ac'])
if series_or_index == 'series':
exp = Series(exp)

Expand Down
0