Skip to content

Commit dbc149d

Browse files
committed
Move apply _shared_docs to functions and attach to methods with copy
Make str_normalize as func; rename copy--> copy_doc to avoid stdlib name
1 parent b77e103 commit dbc149d

File tree

1 file changed

+83
-74
lines changed

1 file changed

+83
-74
lines changed

pandas/core/strings.py

Lines changed: 83 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,33 @@ def str_find(arr, sub, start=0, end=None, side='left'):
939939

940940
return _na_map(f, arr, dtype=int)
941941

942+
_shared_docs['index'] = textwrap.dedent("""
943+
Return %(side)s indexes in each strings where the substring is
944+
fully contained between [start:end]. This is the same as
945+
``str.%(similar)s`` except instead of returning -1, it raises a ValueError
946+
when the substring is not found. Equivalent to standard ``str.%(method)s``.
942947
948+
Parameters
949+
----------
950+
sub : str
951+
Substring being searched
952+
start : int
953+
Left edge index
954+
end : int
955+
Right edge index
956+
957+
Returns
958+
-------
959+
found : Series/Index of objects
960+
961+
See Also
962+
--------
963+
%(also)s
964+
""")
965+
966+
@Appender(_shared_docs['index'] %
967+
dict(side='lowest', similar='find', method='index',
968+
also='rindex : Return highest indexes in each strings'))
943969
def str_index(arr, sub, start=0, end=None, side='left'):
944970
if not isinstance(sub, compat.string_types):
945971
msg = 'expected a string object, not {0}'
@@ -1125,6 +1151,18 @@ def f(x):
11251151
return _na_map(f, arr)
11261152

11271153

1154+
_shared_docs['str_strip'] = textwrap.dedent("""
1155+
Strip whitespace (including newlines) from each string in the
1156+
Series/Index from %(side)s. Equivalent to :meth:`str.%(method)s`.
1157+
1158+
Returns
1159+
-------
1160+
stripped : Series/Index of objects
1161+
""")
1162+
1163+
1164+
@Appender(_shared_docs['str_strip'] % dict(side='left and right sides',
1165+
method='strip'))
11281166
def str_strip(arr, to_strip=None, side='both'):
11291167
"""
11301168
Strip whitespace (including newlines) from each string in the
@@ -1317,6 +1355,27 @@ def str_encode(arr, encoding, errors="strict"):
13171355
return _na_map(f, arr)
13181356

13191357

1358+
def str_normalize(arr, form):
1359+
"""
1360+
Return the Unicode normal form for the strings in the Series/Index.
1361+
For more information on the forms, see the
1362+
:func:`unicodedata.normalize`.
1363+
1364+
Parameters
1365+
----------
1366+
form : {'NFC', 'NFKC', 'NFD', 'NFKD'}
1367+
Unicode form
1368+
1369+
Returns
1370+
-------
1371+
normalized : Series/Index of objects
1372+
"""
1373+
import unicodedata
1374+
f = lambda x: unicodedata.normalize(form, compat.u_safe(x))
1375+
result = _na_map(f, arr)
1376+
return result
1377+
1378+
13201379
def _noarg_wrapper(f, docstring=None, **kargs):
13211380
def wrapper(self):
13221381
result = _na_map(f, self._data, **kargs)
@@ -1353,7 +1412,7 @@ def wrapper3(self, pat, na=np.nan):
13531412
return wrapper
13541413

13551414

1356-
def copy(source):
1415+
def copy_doc(source):
13571416
"Copy a docstring from another source function (if present)"
13581417

13591418
def do_copy(target):
@@ -1466,18 +1525,18 @@ def cons_row(x):
14661525
cons = self._orig._constructor
14671526
return cons(result, name=name, index=index)
14681527

1469-
@copy(str_cat)
1528+
@copy_doc(str_cat)
14701529
def cat(self, others=None, sep=None, na_rep=None):
14711530
data = self._orig if self._is_categorical else self._data
14721531
result = str_cat(data, others=others, sep=sep, na_rep=na_rep)
14731532
return self._wrap_result(result, use_codes=(not self._is_categorical))
14741533

1475-
@copy(str_split)
1534+
@copy_doc(str_split)
14761535
def split(self, pat=None, n=-1, expand=False):
14771536
result = str_split(self._data, pat, n=n)
14781537
return self._wrap_result(result, expand=expand)
14791538

1480-
@copy(str_rsplit)
1539+
@copy_doc(str_rsplit)
14811540
def rsplit(self, pat=None, n=-1, expand=False):
14821541
result = str_rsplit(self._data, pat, n=n)
14831542
return self._wrap_result(result, expand=expand)
@@ -1548,40 +1607,40 @@ def rpartition(self, pat=' ', expand=True):
15481607
result = _na_map(f, self._data)
15491608
return self._wrap_result(result, expand=expand)
15501609

1551-
@copy(str_get)
1610+
@copy_doc(str_get)
15521611
def get(self, i):
15531612
result = str_get(self._data, i)
15541613
return self._wrap_result(result)
15551614

1556-
@copy(str_join)
1615+
@copy_doc(str_join)
15571616
def join(self, sep):
15581617
result = str_join(self._data, sep)
15591618
return self._wrap_result(result)
15601619

1561-
@copy(str_contains)
1620+
@copy_doc(str_contains)
15621621
def contains(self, pat, case=True, flags=0, na=np.nan, regex=True):
15631622
result = str_contains(self._data, pat, case=case, flags=flags, na=na,
15641623
regex=regex)
15651624
return self._wrap_result(result)
15661625

1567-
@copy(str_match)
1626+
@copy_doc(str_match)
15681627
def match(self, pat, case=True, flags=0, na=np.nan, as_indexer=None):
15691628
result = str_match(self._data, pat, case=case, flags=flags, na=na,
15701629
as_indexer=as_indexer)
15711630
return self._wrap_result(result)
15721631

1573-
@copy(str_replace)
1632+
@copy_doc(str_replace)
15741633
def replace(self, pat, repl, n=-1, case=None, flags=0):
15751634
result = str_replace(self._data, pat, repl, n=n, case=case,
15761635
flags=flags)
15771636
return self._wrap_result(result)
15781637

1579-
@copy(str_repeat)
1638+
@copy_doc(str_repeat)
15801639
def repeat(self, repeats):
15811640
result = str_repeat(self._data, repeats)
15821641
return self._wrap_result(result)
15831642

1584-
@copy(str_pad)
1643+
@copy_doc(str_pad)
15851644
def pad(self, width, side='left', fillchar=' '):
15861645
result = str_pad(self._data, width, side=side, fillchar=fillchar)
15871646
return self._wrap_result(result)
@@ -1634,37 +1693,27 @@ def zfill(self, width):
16341693
result = str_pad(self._data, width, side='left', fillchar='0')
16351694
return self._wrap_result(result)
16361695

1637-
@copy(str_slice)
1696+
@copy_doc(str_slice)
16381697
def slice(self, start=None, stop=None, step=None):
16391698
result = str_slice(self._data, start, stop, step)
16401699
return self._wrap_result(result)
16411700

1642-
@copy(str_slice_replace)
1701+
@copy_doc(str_slice_replace)
16431702
def slice_replace(self, start=None, stop=None, repl=None):
16441703
result = str_slice_replace(self._data, start, stop, repl)
16451704
return self._wrap_result(result)
16461705

1647-
@copy(str_decode)
1706+
@copy_doc(str_decode)
16481707
def decode(self, encoding, errors="strict"):
16491708
result = str_decode(self._data, encoding, errors)
16501709
return self._wrap_result(result)
16511710

1652-
@copy(str_encode)
1711+
@copy_doc(str_encode)
16531712
def encode(self, encoding, errors="strict"):
16541713
result = str_encode(self._data, encoding, errors)
16551714
return self._wrap_result(result)
16561715

1657-
_shared_docs['str_strip'] = ("""
1658-
Strip whitespace (including newlines) from each string in the
1659-
Series/Index from %(side)s. Equivalent to :meth:`str.%(method)s`.
1660-
1661-
Returns
1662-
-------
1663-
stripped : Series/Index of objects
1664-
""")
1665-
1666-
@Appender(_shared_docs['str_strip'] % dict(side='left and right sides',
1667-
method='strip'))
1716+
@copy_doc(str_strip)
16681717
def strip(self, to_strip=None):
16691718
result = str_strip(self._data, to_strip, side='both')
16701719
return self._wrap_result(result)
@@ -1681,12 +1730,12 @@ def rstrip(self, to_strip=None):
16811730
result = str_strip(self._data, to_strip, side='right')
16821731
return self._wrap_result(result)
16831732

1684-
@copy(str_wrap)
1733+
@copy_doc(str_wrap)
16851734
def wrap(self, width, **kwargs):
16861735
result = str_wrap(self._data, width, **kwargs)
16871736
return self._wrap_result(result)
16881737

1689-
@copy(str_get_dummies)
1738+
@copy_doc(str_get_dummies)
16901739
def get_dummies(self, sep='|'):
16911740
# we need to cast to Series of strings as only that has all
16921741
# methods available for making the dummies...
@@ -1695,7 +1744,7 @@ def get_dummies(self, sep='|'):
16951744
return self._wrap_result(result, use_codes=(not self._is_categorical),
16961745
name=name, expand=True)
16971746

1698-
@copy(str_translate)
1747+
@copy_doc(str_translate)
16991748
def translate(self, table, deletechars=None):
17001749
result = str_translate(self._data, table, deletechars)
17011750
return self._wrap_result(result)
@@ -1705,11 +1754,11 @@ def translate(self, table, deletechars=None):
17051754
endswith = _pat_wrapper(str_endswith, na=True)
17061755
findall = _pat_wrapper(str_findall, flags=True)
17071756

1708-
@copy(str_extract)
1757+
@copy_doc(str_extract)
17091758
def extract(self, pat, flags=0, expand=None):
17101759
return str_extract(self, pat, flags=flags, expand=expand)
17111760

1712-
@copy(str_extractall)
1761+
@copy_doc(str_extractall)
17131762
def extractall(self, pat, flags=0):
17141763
return str_extractall(self._orig, pat, flags=flags)
17151764

@@ -1750,52 +1799,12 @@ def rfind(self, sub, start=0, end=None):
17501799
result = str_find(self._data, sub, start=start, end=end, side='right')
17511800
return self._wrap_result(result)
17521801

1802+
@copy_doc(str_normalize)
17531803
def normalize(self, form):
1754-
"""Return the Unicode normal form for the strings in the Series/Index.
1755-
For more information on the forms, see the
1756-
:func:`unicodedata.normalize`.
1757-
1758-
Parameters
1759-
----------
1760-
form : {'NFC', 'NFKC', 'NFD', 'NFKD'}
1761-
Unicode form
1762-
1763-
Returns
1764-
-------
1765-
normalized : Series/Index of objects
1766-
"""
1767-
import unicodedata
1768-
f = lambda x: unicodedata.normalize(form, compat.u_safe(x))
1769-
result = _na_map(f, self._data)
1804+
result = str_normalize(self._data, form)
17701805
return self._wrap_result(result)
17711806

1772-
_shared_docs['index'] = ("""
1773-
Return %(side)s indexes in each strings where the substring is
1774-
fully contained between [start:end]. This is the same as
1775-
``str.%(similar)s`` except instead of returning -1, it raises a ValueError
1776-
when the substring is not found. Equivalent to standard ``str.%(method)s``.
1777-
1778-
Parameters
1779-
----------
1780-
sub : str
1781-
Substring being searched
1782-
start : int
1783-
Left edge index
1784-
end : int
1785-
Right edge index
1786-
1787-
Returns
1788-
-------
1789-
found : Series/Index of objects
1790-
1791-
See Also
1792-
--------
1793-
%(also)s
1794-
""")
1795-
1796-
@Appender(_shared_docs['index'] %
1797-
dict(side='lowest', similar='find', method='index',
1798-
also='rindex : Return highest indexes in each strings'))
1807+
@copy_doc(str_index)
17991808
def index(self, sub, start=0, end=None):
18001809
result = str_index(self._data, sub, start=start, end=end, side='left')
18011810
return self._wrap_result(result)

0 commit comments

Comments
 (0)