diff --git a/pandas/core/strings.py b/pandas/core/strings.py index b98fa106336fc..d6a67435aeb09 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -328,19 +328,54 @@ def str_contains(arr, pat, case=True, flags=0, na=np.nan, regex=True): def str_startswith(arr, pat, na=np.nan): """ - Return boolean Series/``array`` indicating whether each string in the - Series/Index starts with passed pattern. Equivalent to - :meth:`str.startswith`. + Test if the start of each string element matches a pattern. + + Equivalent to :meth:`str.startswith`. Parameters ---------- - pat : string - Character sequence - na : bool, default NaN + pat : str + Character sequence. Regular expressions are not accepted. + na : object, default NaN + Object shown if element tested is not a string. Returns ------- - startswith : Series/array of boolean values + Series or Index of bool + A Series of booleans indicating whether the given pattern matches + the start of each string element. + + See Also + -------- + str.startswith : Python standard library string method. + Series.str.endswith : Same as startswith, but tests the end of string. + Series.str.contains : Tests if string element contains a pattern. + + Examples + -------- + >>> s = pd.Series(['bat', 'Bear', 'cat', np.nan]) + >>> s + 0 bat + 1 Bear + 2 cat + 3 NaN + dtype: object + + >>> s.str.startswith('b') + 0 True + 1 False + 2 False + 3 NaN + dtype: object + + Specifying `na` to be `False` instead of `NaN`. + + >>> s.str.startswith('b', na=False) + 0 True + 1 False + 2 False + 3 False + dtype: bool """ f = lambda x: x.startswith(pat) return _na_map(f, arr, na, dtype=bool)