Skip to content

Commit 72b71c2

Browse files
jseaboldwesm
authored andcommitted
ENH: Add title string method
1 parent 600943e commit 72b71c2

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

pandas/core/strings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ def g(x):
111111
else:
112112
return lib.map_infer(arr, f)
113113

114+
def str_title(arr):
115+
"""
116+
Convert strings to titlecased version
117+
118+
Returns
119+
-------
120+
titled : array
121+
"""
122+
return _na_map(lambda x: x.title(), arr)
123+
114124

115125
def str_count(arr, pat, flags=0):
116126
"""
@@ -744,3 +754,4 @@ def rstrip(self, to_strip=None):
744754
len = _noarg_wrapper(str_len)
745755
lower = _noarg_wrapper(str_lower)
746756
upper = _noarg_wrapper(str_upper)
757+
title = _noarg_wrapper(str_title)

pandas/tests/test_strings.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,28 @@ def test_endswith(self):
184184
result = values.str.endswith('foo', na=False)
185185
tm.assert_series_equal(result, exp.fillna(False).astype(bool))
186186

187+
def test_title(self):
188+
values = Series(["FOO", "BAR", NA, "Blah", "blurg"])
189+
190+
result = values.str.title()
191+
exp = Series(["Foo", "Bar", NA, "Blah", "Blurg"])
192+
tm.assert_series_equal(result, exp)
193+
194+
# mixed
195+
mixed = Series(["FOO", NA, "bar", True, datetime.today(),
196+
"blah", None, 1, 2.])
197+
mixed = mixed.str.title()
198+
exp = Series(["Foo", NA, "Bar", NA, NA, "Blah", NA, NA, NA])
199+
tm.assert_almost_equal(mixed, exp)
200+
201+
# unicode
202+
values = Series([u"FOO", NA, u"bar", u"Blurg"])
203+
204+
results = values.str.title()
205+
exp = Series([u"Foo", NA, u"Bar", u"Blurg"])
206+
207+
tm.assert_series_equal(results, exp)
208+
187209
def test_lower_upper(self):
188210
values = Series(['om', NA, 'nom', 'nom'])
189211

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy