Test if the end of each string element matches a pattern. Equivalent to :meth:`str.endswith`. Parameters ---------- pat : str or tuple[str, ...] Character sequence or tuple of strings. Regular expressions are not accepted. na
(
self, pat: str | tuple[str, ...], na: Scalar | lib.NoDefault = lib.no_default
)
| 3175 | |
| 3176 | @forbid_nonstring_types(["bytes"]) |
| 3177 | def endswith( |
| 3178 | self, pat: str | tuple[str, ...], na: Scalar | lib.NoDefault = lib.no_default |
| 3179 | ) -> Series | Index: |
| 3180 | """ |
| 3181 | Test if the end of each string element matches a pattern. |
| 3182 | |
| 3183 | Equivalent to :meth:`str.endswith`. |
| 3184 | |
| 3185 | Parameters |
| 3186 | ---------- |
| 3187 | pat : str or tuple[str, ...] |
| 3188 | Character sequence or tuple of strings. Regular expressions are not |
| 3189 | accepted. |
| 3190 | na : scalar, optional |
| 3191 | Object shown if element tested is not a string. The default depends |
| 3192 | on dtype of the array. For the ``"str"`` dtype, ``False`` is used. |
| 3193 | For object dtype, ``numpy.nan`` is used. For the nullable |
| 3194 | ``StringDtype``, ``pandas.NA`` is used. |
| 3195 | |
| 3196 | Returns |
| 3197 | ------- |
| 3198 | Series or Index of bool |
| 3199 | A Series of booleans indicating whether the given pattern matches |
| 3200 | the end of each string element. |
| 3201 | |
| 3202 | See Also |
| 3203 | -------- |
| 3204 | str.endswith : Python standard library string method. |
| 3205 | Series.str.startswith : Same as endswith, but tests the start of string. |
| 3206 | Series.str.contains : Tests if string element contains a pattern. |
| 3207 | |
| 3208 | Examples |
| 3209 | -------- |
| 3210 | >>> s = pd.Series(["bat", "bear", "caT", np.nan]) |
| 3211 | >>> s |
| 3212 | 0 bat |
| 3213 | 1 bear |
| 3214 | 2 caT |
| 3215 | 3 NaN |
| 3216 | dtype: str |
| 3217 | |
| 3218 | >>> s.str.endswith("t") |
| 3219 | 0 True |
| 3220 | 1 False |
| 3221 | 2 False |
| 3222 | 3 False |
| 3223 | dtype: bool |
| 3224 | |
| 3225 | >>> s.str.endswith(("t", "T")) |
| 3226 | 0 True |
| 3227 | 1 False |
| 3228 | 2 True |
| 3229 | 3 False |
| 3230 | dtype: bool |
| 3231 | """ |
| 3232 | if not isinstance(pat, (str, tuple)): |
| 3233 | msg = f"expected a string or tuple, not {type(pat).__name__}" |
| 3234 | raise TypeError(msg) |