|
8 | 8 | import re
|
9 | 9 | import pandas.lib as lib
|
10 | 10 | import warnings
|
| 11 | +import textwrap |
11 | 12 |
|
12 | 13 |
|
13 | 14 | def _get_array_list(arr, others):
|
@@ -717,20 +718,63 @@ def str_rstrip(arr, to_strip=None):
|
717 | 718 | return _na_map(lambda x: x.rstrip(to_strip), arr)
|
718 | 719 |
|
719 | 720 |
|
720 |
| -def str_wrap(arr, width=80): |
| 721 | +def str_wrap(arr, width, **kwargs): |
721 | 722 | """
|
722 | 723 | Wrap long strings to be formatted in paragraphs
|
723 | 724 |
|
724 | 725 | Parameters
|
725 | 726 | ----------
|
| 727 | + Same keyword parameters and defaults as :class:`textwrap.TextWrapper` |
726 | 728 | width : int
|
727 | 729 | Maximum line-width
|
| 730 | + expand_tabs : bool, optional |
| 731 | + If true, tab characters will be expanded to spaces (default: True) |
| 732 | + replace_whitespace : bool, optional |
| 733 | + If true, each whitespace character (as defined by string.whitespace) remaining |
| 734 | + after tab expansion will be replaced by a single space (default: True) |
| 735 | + drop_whitespace : bool, optional |
| 736 | + If true, whitespace that, after wrapping, happens to end up at the beginning |
| 737 | + or end of a line is dropped (default: True) |
| 738 | + break_long_words : bool, optional |
| 739 | + If true, then words longer than width will be broken in order to ensure that |
| 740 | + no lines are longer than width. If it is false, long words will not be broken, |
| 741 | + and some lines may be longer than width. (default: True) |
| 742 | + break_on_hyphens : bool, optional |
| 743 | + If true, wrapping will occur preferably on whitespace and right after hyphens |
| 744 | + in compound words, as it is customary in English. If false, only whitespaces |
| 745 | + will be considered as potentially good places for line breaks, but you need |
| 746 | + to set break_long_words to false if you want truly insecable words. |
| 747 | + (default: True) |
728 | 748 |
|
729 | 749 | Returns
|
730 | 750 | -------
|
731 | 751 | wrapped : array
|
| 752 | +
|
| 753 | + Notes |
| 754 | + ----- |
| 755 | + Internally, this method uses a :class:`textwrap.TextWrapper` instance with default |
| 756 | + settings. To achieve behavior matching R's stringr library str_wrap function, use |
| 757 | + the arguments: |
| 758 | +
|
| 759 | + expand_tabs = False |
| 760 | + replace_whitespace = True |
| 761 | + drop_whitespace = True |
| 762 | + break_long_words = False |
| 763 | + break_on_hyphens = False |
| 764 | +
|
| 765 | + Examples |
| 766 | + -------- |
| 767 | +
|
| 768 | + >>> s = pd.Series(['line to be wrapped', 'another line to be wrapped']) |
| 769 | + >>> s.str.wrap(12) |
| 770 | + 0 line to be\nwrapped |
| 771 | + 1 another line\nto be\nwrapped |
732 | 772 | """
|
733 |
| - raise NotImplementedError |
| 773 | + kwargs['width'] = width |
| 774 | + |
| 775 | + tw = textwrap.TextWrapper(**kwargs) |
| 776 | + |
| 777 | + return _na_map(lambda s: '\n'.join(tw.wrap(s)), arr) |
734 | 778 |
|
735 | 779 |
|
736 | 780 | def str_get(arr, i):
|
@@ -955,6 +999,11 @@ def rstrip(self, to_strip=None):
|
955 | 999 | result = str_rstrip(self.series, to_strip)
|
956 | 1000 | return self._wrap_result(result)
|
957 | 1001 |
|
| 1002 | + @copy(str_wrap) |
| 1003 | + def wrap(self, width, **kwargs): |
| 1004 | + result = str_wrap(self.series, width, **kwargs) |
| 1005 | + return self._wrap_result(result) |
| 1006 | + |
958 | 1007 | @copy(str_get_dummies)
|
959 | 1008 | def get_dummies(self, sep='|'):
|
960 | 1009 | result = str_get_dummies(self.series, sep)
|
|
0 commit comments