Skip to content

Commit

Permalink
DOC: fix PR02 errors in docstring for pandas.Series.str.wrap (pandas-…
Browse files Browse the repository at this point in the history
…dev#57321)

* DOC: fix PR02 errors in docstring for pandas.Series.str.wrap

* change signature of wrap to include kwargs as explicit parameters

* fixing call to _str_wrap()

* Added all textwrap.TextWrapper parameters

* update width argument to remain positional by default
  • Loading branch information
jordan-d-murphy authored and pmhatre1 committed May 7, 2024
1 parent 0adf3cc commit ce358e6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
1 change: 0 additions & 1 deletion ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.Series.dt.ceil\
pandas.Series.dt.month_name\
pandas.Series.dt.day_name\
pandas.Series.str.wrap\
pandas.Series.cat.rename_categories\
pandas.Series.cat.reorder_categories\
pandas.Series.cat.add_categories\
Expand Down
67 changes: 63 additions & 4 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2232,26 +2232,66 @@ def removesuffix(self, suffix: str):
return self._wrap_result(result)

@forbid_nonstring_types(["bytes"])
def wrap(self, width: int, **kwargs):
def wrap(
self,
width: int,
expand_tabs: bool = True,
tabsize: int = 8,
replace_whitespace: bool = True,
drop_whitespace: bool = True,
initial_indent: str = "",
subsequent_indent: str = "",
fix_sentence_endings: bool = False,
break_long_words: bool = True,
break_on_hyphens: bool = True,
max_lines: int | None = None,
placeholder: str = " [...]",
):
r"""
Wrap strings in Series/Index at specified line width.
This method has the same keyword parameters and defaults as
:class:`textwrap.TextWrapper`.
:class:`textwrap.TextWrapper`.
Parameters
----------
width : int
width : int, optional
Maximum line width.
expand_tabs : bool, optional
If True, tab characters will be expanded to spaces (default: True).
tabsize : int, optional
If expand_tabs is true, then all tab characters in text will be
expanded to zero or more spaces, depending on the current column
and the given tab size (default: 8).
replace_whitespace : bool, optional
If True, each whitespace character (as defined by string.whitespace)
remaining after tab expansion will be replaced by a single space
(default: True).
drop_whitespace : bool, optional
If True, whitespace that, after wrapping, happens to end up at the
beginning or end of a line is dropped (default: True).
initial_indent : str, optional
String that will be prepended to the first line of wrapped output.
Counts towards the length of the first line. The empty string is
not indented (default: '').
subsequent_indent : str, optional
String that will be prepended to all lines of wrapped output except
the first. Counts towards the length of each line except the first
(default: '').
fix_sentence_endings : bool, optional
If true, TextWrapper attempts to detect sentence endings and ensure
that sentences are always separated by exactly two spaces. This is
generally desired for text in a monospaced font. However, the sentence
detection algorithm is imperfect: it assumes that a sentence ending
consists of a lowercase letter followed by one of '.', '!', or '?',
possibly followed by one of '"' or "'", followed by a space. One
problem with this algorithm is that it is unable to detect the
difference between “Dr.” in `[...] Dr. Frankenstein's monster [...]`
and “Spot.” in `[...] See Spot. See Spot run [...]`
Since the sentence detection algorithm relies on string.lowercase
for the definition of “lowercase letter”, and a convention of using
two spaces after a period to separate sentences on the same line,
it is specific to English-language texts (default: False).
break_long_words : bool, optional
If True, then words longer than width will be broken in order to ensure
that no lines are longer than width. If it is false, long words will
Expand All @@ -2262,6 +2302,12 @@ def wrap(self, width: int, **kwargs):
only whitespaces will be considered as potentially good places for line
breaks, but you need to set break_long_words to false if you want truly
insecable words (default: True).
max_lines : int, optional
If not None, then the output will contain at most max_lines lines, with
placeholder appearing at the end of the output (default: None).
placeholder : str, optional
String that will appear at the end of the output text if it has been
truncated (default: ' [...]').
Returns
-------
Expand All @@ -2287,7 +2333,20 @@ def wrap(self, width: int, **kwargs):
1 another line\nto be\nwrapped
dtype: object
"""
result = self._data.array._str_wrap(width, **kwargs)
result = self._data.array._str_wrap(
width=width,
expand_tabs=expand_tabs,
tabsize=tabsize,
replace_whitespace=replace_whitespace,
drop_whitespace=drop_whitespace,
initial_indent=initial_indent,
subsequent_indent=subsequent_indent,
fix_sentence_endings=fix_sentence_endings,
break_long_words=break_long_words,
break_on_hyphens=break_on_hyphens,
max_lines=max_lines,
placeholder=placeholder,
)
return self._wrap_result(result)

@forbid_nonstring_types(["bytes"])
Expand Down

0 comments on commit ce358e6

Please sign in to comment.