Skip to content
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

ENH: #9847, adding a "same" and "expand" param to StringMethods.split() #9870

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ def str_pad(arr, width, side='left', fillchar=' '):

def str_split(arr, pat=None, n=None, return_type='series'):
"""
Deprecated: return_types 'series', 'index', 'frame' are now deprecated
Copy link
Member

Choose a reason for hiding this comment

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

Can you move this comment to the return_type explanation?

Copy link
Member

Choose a reason for hiding this comment

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

To explain: the first sentence is used in summary tables on the API pages, so this is not what we want to show there.

Split each string (a la re.split) in array by given pattern, propagating NA
values

Expand All @@ -632,9 +633,9 @@ def str_split(arr, pat=None, n=None, return_type='series'):
pat : string, default None
String or regular expression to split on. If None, splits on whitespace
n : int, default None (all)
return_type : {'series', 'index', 'frame'}, default 'series'
If frame, returns a DataFrame (elements are strings)
If series or index, returns the same type as the original object
return_type : {'same', 'expand'}, default 'series'
Copy link
Member

Choose a reason for hiding this comment

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

should be default 'same'

If expand, returns a DataFrame (elements are strings)
Copy link
Member

Choose a reason for hiding this comment

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

Can you add puntcuation? (so a . at the end of the line) -> when converted to html, the line breaks are not preserved as in raw text here.

Also, can you quote expand (like 'expand'), then it is clearer it is the value you give to the argument.

If series, index or same, returns the same type as the original object
Copy link
Member

Choose a reason for hiding this comment

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

I would only mention 'same' here (as you did for 'frame')

(elements are lists of strings).

Notes
Expand All @@ -649,11 +650,14 @@ def str_split(arr, pat=None, n=None, return_type='series'):
from pandas.core.frame import DataFrame
from pandas.core.index import Index

Copy link
Contributor

Choose a reason for hiding this comment

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

this is a minor point but it might be good idea to use a dict to organize the mapping from old to new arg names, at the beginning of the function, something like:

arg_map = {'series': 'same', 'index': 'same', 'frame': 'expand'}
if return_type in arg_map:
    warnings.warn("return_type='%s' is deprecated, please use '%s' instead" % (return_type, arg_map[return_type]), FutureWarning)
    return_type = arg_map[return_type]

this way the rest of the code doesn't have to contain references to the old names, making it easier to remove the deprecation stuff later.

Copy link
Author

Choose a reason for hiding this comment

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

Should this be updated to use expand=True/False or keep as same/expand?

if return_type not in ('series', 'index', 'frame'):
raise ValueError("return_type must be {'series', 'index', 'frame'}")
if return_type == 'frame' and isinstance(arr, Index):
if return_type not in ('series', 'index', 'frame', 'same', 'expand'):
raise ValueError("return_type must be {'series', 'index', 'frame', 'same', 'expand'}")
Copy link
Member

Choose a reason for hiding this comment

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

also, here I would only mention 'same' and 'expand'

if return_type in ('frame', 'expand') and isinstance(arr, Index):
raise ValueError("return_type='frame' is not supported for string "
"methods on Index")
if return_type in ('series', 'index', 'frame'):
warnings.warn(("'series', 'index' and 'frame' are deprecated. Please use 'same' or 'expand' instead"),
FutureWarning)
if pat is None:
if n is None or n == 0:
n = -1
Expand All @@ -668,7 +672,7 @@ def str_split(arr, pat=None, n=None, return_type='series'):
n = 0
regex = re.compile(pat)
f = lambda x: regex.split(x, maxsplit=n)
if return_type == 'frame':
if return_type in ('frame', 'expand'):
res = DataFrame((Series(x) for x in _na_map(f, arr)), index=arr.index)
else:
res = _na_map(f, arr)
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,9 +1220,12 @@ def test_str_attribute(self):
tm.assert_index_equal(idx.str.split(return_type='series'), expected)
# return_type 'index' is an alias for 'series'
tm.assert_index_equal(idx.str.split(return_type='index'), expected)
# return_type 'same' is an alias for 'series' and 'index'
tm.assert_index_equal(idx.str.split(return_type='same'), expected)
with self.assertRaisesRegexp(ValueError, 'not supported'):
idx.str.split(return_type='frame')

with self.assertRaisesRegexp(ValueError, 'not supported'):
idx.str.split(return_type='expand')
# test boolean case, should return np.array instead of boolean Index
idx = Index(['a1', 'a2', 'b1', 'b2'])
expected = np.array([True, True, False, False])
Expand Down