diff --git a/pandas/core/strings.py b/pandas/core/strings.py index cac9e84412cfa..a24b213c4e8e0 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -111,6 +111,16 @@ def g(x): else: return lib.map_infer(arr, f) +def str_title(arr): + """ + Convert strings to titlecased version + + Returns + ------- + titled : array + """ + return _na_map(lambda x: x.title(), arr) + def str_count(arr, pat, flags=0): """ @@ -744,3 +754,4 @@ def rstrip(self, to_strip=None): len = _noarg_wrapper(str_len) lower = _noarg_wrapper(str_lower) upper = _noarg_wrapper(str_upper) + title = _noarg_wrapper(str_title) diff --git a/pandas/tests/test_strings.py b/pandas/tests/test_strings.py index 910bfda73abe5..a75ea8dc8259e 100644 --- a/pandas/tests/test_strings.py +++ b/pandas/tests/test_strings.py @@ -184,6 +184,28 @@ def test_endswith(self): result = values.str.endswith('foo', na=False) tm.assert_series_equal(result, exp.fillna(False).astype(bool)) + def test_title(self): + values = Series(["FOO", "BAR", NA, "Blah", "blurg"]) + + result = values.str.title() + exp = Series(["Foo", "Bar", NA, "Blah", "Blurg"]) + tm.assert_series_equal(result, exp) + + # mixed + mixed = Series(["FOO", NA, "bar", True, datetime.today(), + "blah", None, 1, 2.]) + mixed = mixed.str.title() + exp = Series(["Foo", NA, "Bar", NA, NA, "Blah", NA, NA, NA]) + tm.assert_almost_equal(mixed, exp) + + # unicode + values = Series([u"FOO", NA, u"bar", u"Blurg"]) + + results = values.str.title() + exp = Series([u"Foo", NA, u"Bar", u"Blurg"]) + + tm.assert_series_equal(results, exp) + def test_lower_upper(self): values = Series(['om', NA, 'nom', 'nom'])