
    Wh(<                    T    d dl mZ d dlmZmZ d dlmZ d dlmZ  G d dee         Z	y)    )annotations)AnyGeneric)is_narwhals_series)SeriesTc                      e Zd ZddZddZddZddd	 	 	 	 	 	 	 	 	 ddZdd	 	 	 	 	 	 	 dd	ZdddZddZ	d dZ
ddd!dZdd"dZd#dZd$d%dZd$d%dZddZddZdd&dZdd&dZd'dZy
)(SeriesStringNamespacec                    || _         y N)_narwhals_series)selfseriess     M/var/www/html/jupyter_env/lib/python3.12/site-packages/narwhals/series_str.py__init__zSeriesStringNamespace.__init__
   s
     &    c                    | j                   j                  | j                   j                  j                  j	                               S )a  Return the length of each string as the number of characters.

        Examples:
            >>> import polars as pl
            >>> import narwhals as nw
            >>> s_native = pl.Series(["foo", "345", None])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.len_chars().to_native()  # doctest: +NORMALIZE_WHITESPACE
            shape: (3,)
            Series: '' [u32]
            [
                    3
                    3
                    null
            ]
        )r   _with_compliant_compliant_seriesstr	len_charsr   s    r   r   zSeriesStringNamespace.len_chars   s;    " $$44!!3377AAC
 	
r   c                4    t        |      r|j                  S |S r   )r   r   )r   args     r   _extract_compliantz(SeriesStringNamespace._extract_compliant"   s    (:3(?s$$HSHr   F   literalnc                   | j                   j                  | j                   j                  j                  j	                  || j                  |      ||            S )a  Replace first matching regex/literal substring with a new string value.

        Arguments:
            pattern: A valid regular expression pattern.
            value: String that will replace the matched substring.
            literal: Treat `pattern` as a literal string.
            n: Number of matches to replace.

        Examples:
            >>> import pandas as pd
            >>> import narwhals as nw
            >>> s_native = pd.Series(["123abc", "abc abc123"])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.replace("abc", "").to_native()
            0        123
            1     abc123
            dtype: object
        r   )r   r   r   r   replacer   )r   patternvaluer   r   s        r   r    zSeriesStringNamespace.replace%   sW    * $$44!!3377??007A @ 
 	
r   r   c                   | j                   j                  | j                   j                  j                  j	                  || j                  |      |            S )ar  Replace all matching regex/literal substring with a new string value.

        Arguments:
            pattern: A valid regular expression pattern.
            value: String that will replace the matched substring.
            literal: Treat `pattern` as a literal string.

        Examples:
            >>> import pandas as pd
            >>> import narwhals as nw
            >>> s_native = pd.Series(["123abc", "abc abc123"])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.replace_all("abc", "").to_native()
            0     123
            1     123
            dtype: object
        r#   )r   r   r   r   replace_allr   )r   r!   r"   r   s       r   r%   z!SeriesStringNamespace.replace_all@   sU    ( $$44!!3377CC007 D 
 	
r   Nc                    | j                   j                  | j                   j                  j                  j	                  |            S )a  Remove leading and trailing characters.

        Arguments:
            characters: The set of characters to be removed. All combinations of this set of characters will be stripped from the start and end of the string. If set to None (default), all leading and trailing whitespace is removed instead.

        Examples:
            >>> import polars as pl
            >>> import narwhals as nw
            >>> s_native = pl.Series(["apple", "\nmango"])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.strip_chars().to_native()  # doctest: +NORMALIZE_WHITESPACE
            shape: (2,)
            Series: '' [str]
            [
                    "apple"
                    "mango"
            ]
        )r   r   r   r   strip_chars)r   
characterss     r   r'   z!SeriesStringNamespace.strip_charsZ   s=    & $$44!!3377CCJO
 	
r   c                    | j                   j                  | j                   j                  j                  j	                  |            S )a  Check if string values start with a substring.

        Arguments:
            prefix: prefix substring

        Examples:
            >>> import pandas as pd
            >>> import narwhals as nw
            >>> s_native = pd.Series(["apple", "mango", None])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.starts_with("app").to_native()
            0     True
            1    False
            2     None
            dtype: object
        )r   r   r   r   starts_with)r   prefixs     r   r*   z!SeriesStringNamespace.starts_withq   s=    " $$44!!3377CCFK
 	
r   c                    | j                   j                  | j                   j                  j                  j	                  |            S )a  Check if string values end with a substring.

        Arguments:
            suffix: suffix substring

        Examples:
            >>> import pandas as pd
            >>> import narwhals as nw
            >>> s_native = pd.Series(["apple", "mango", None])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.ends_with("ngo").to_native()
            0    False
            1     True
            2     None
            dtype: object
        )r   r   r   r   	ends_with)r   suffixs     r   r-   zSeriesStringNamespace.ends_with   s=    " $$44!!3377AA&I
 	
r   c                   | j                   j                  | j                   j                  j                  j	                  ||            S )a,  Check if string contains a substring that matches a pattern.

        Arguments:
            pattern: A Character sequence or valid regular expression pattern.
            literal: If True, treats the pattern as a literal string.
                     If False, assumes the pattern is a regular expression.

        Examples:
            >>> import pyarrow as pa
            >>> import narwhals as nw
            >>> s_native = pa.chunked_array([["cat", "dog", "rabbit and parrot"]])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.contains("cat|parrot").to_native()  # doctest: +ELLIPSIS
            <pyarrow.lib.ChunkedArray object at ...>
            [
              [
                true,
                false,
                true
              ]
            ]
        r#   )r   r   r   r   contains)r   r!   r   s      r   r0   zSeriesStringNamespace.contains   sC    . $$44!!3377@@RY@Z
 	
r   c                    | j                   j                  | j                   j                  j                  j	                  ||            S )a  Create subslices of the string values of a Series.

        Arguments:
            offset: Start index. Negative indexing is supported.
            length: Length of the slice. If set to `None` (default), the slice is taken to the
                end of the string.

        Examples:
            >>> import pandas as pd
            >>> import narwhals as nw
            >>> s_native = pd.Series(["pear", None, "papaya"])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.slice(4, 3).to_native()  # doctest: +NORMALIZE_WHITESPACE
            0
            1    None
            2      ya
            dtype: object
        offsetlengthr   r   r   r   slice)r   r3   r4   s      r   r6   zSeriesStringNamespace.slice   sG    & $$44!!3377==f > 
 	
r   c                    | j                   j                  | j                   j                  j                  j	                  |            S )a9  Split the string values of a Series by a substring.

        Arguments:
            by: Substring to split by.

        Examples:
            >>> import polars as pl
            >>> import narwhals as nw
            >>> s_native = pl.Series(["foo bar", "foo_bar"])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.split("_").to_native()  # doctest: +NORMALIZE_WHITESPACE
            shape: (2,)
            Series: '' [list[str]]
            [
                    ["foo bar"]
                    ["foo", "bar"]
            ]
        )by)r   r   r   r   split)r   r8   s     r   r9   zSeriesStringNamespace.split   s@    & $$44!!3377===D
 	
r   c                    | j                   j                  | j                   j                  j                  j	                  d|            S )a  Take the first n elements of each string.

        Arguments:
            n: Number of elements to take. Negative indexing is supported (see note (1.))

        Notes:
            1. When the `n` input is negative, `head` returns characters up to the n-th from the end of the string.
            For example, if `n = -3`, then all characters except the last three are returned.
            2. If the length of the string has fewer than `n` characters, the full string is returned.

        Examples:
            >>> import pyarrow as pa
            >>> import narwhals as nw
            >>> s_native = pa.chunked_array([["taata", "taatatata", "zukkyun"]])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.head().to_native()  # doctest: +ELLIPSIS
            <pyarrow.lib.ChunkedArray object at ...>
            [
              [
                "taata",
                "taata",
                "zukky"
              ]
            ]
        r   r2   r5   r   r   s     r   headzSeriesStringNamespace.head   sB    4 $$44!!3377==Qq=Q
 	
r   c                    | j                   j                  | j                   j                  j                  j	                  | d            S )a  Take the last n elements of each string.

        Arguments:
            n: Number of elements to take. Negative indexing is supported (see note (1.))

        Notes:
            1. When the `n` input is negative, `tail` returns characters starting from the n-th from the beginning of
            the string. For example, if `n = -3`, then all characters except the first three are returned.
            2. If the length of the string has fewer than `n` characters, the full string is returned.

        Examples:
            >>> import pyarrow as pa
            >>> import narwhals as nw
            >>> s_native = pa.chunked_array([["taata", "taatatata", "zukkyun"]])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.tail().to_native()  # doctest: +ELLIPSIS
            <pyarrow.lib.ChunkedArray object at ...>
            [
              [
                "taata",
                "atata",
                "kkyun"
              ]
            ]
        Nr2   r5   r;   s     r   tailzSeriesStringNamespace.tail  sE    4 $$44!!3377==aRPT=U
 	
r   c                    | j                   j                  | j                   j                  j                  j	                               S )u~  Transform string to uppercase variant.

        Notes:
            The PyArrow backend will convert 'ß' to 'ẞ' instead of 'SS'.
            For more info see: https://github.com/apache/arrow/issues/34599
            There may be other unicode-edge-case-related variations across implementations.

        Examples:
            >>> import pandas as pd
            >>> import narwhals as nw
            >>> s_native = pd.Series(["apple", None])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.to_uppercase().to_native()
            0    APPLE
            1     None
            dtype: object
        )r   r   r   r   to_uppercaser   s    r   r@   z"SeriesStringNamespace.to_uppercase"  s;    $ $$44!!3377DDF
 	
r   c                    | j                   j                  | j                   j                  j                  j	                               S )az  Transform string to lowercase variant.

        Examples:
            >>> import pandas as pd
            >>> import narwhals as nw
            >>> s_native = pd.Series(["APPLE", None])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.to_lowercase().to_native()
            0    apple
            1     None
            dtype: object
        )r   r   r   r   to_lowercaser   s    r   rB   z"SeriesStringNamespace.to_lowercase8  s;     $$44!!3377DDF
 	
r   c                    | j                   j                  | j                   j                  j                  j	                  |            S )u1  Parse Series with strings to a Series with Datetime dtype.

        Notes:
            - pandas defaults to nanosecond time unit, Polars to microsecond.
              Prior to pandas 2.0, nanoseconds were the only time unit supported
              in pandas, with no ability to set any other one. The ability to
              set the time unit in pandas, if the version permits, will arrive.
            - timezone-aware strings are all converted to and parsed as UTC.

        Warning:
            As different backends auto-infer format in different ways, if `format=None`
            there is no guarantee that the result will be equal.

        Arguments:
            format: Format to use for conversion. If set to None (default), the format is
                inferred from the data.

        Examples:
            >>> import polars as pl
            >>> import narwhals as nw
            >>> s_native = pl.Series(["2020-01-01", "2020-01-02"])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.to_datetime(
            ...     format="%Y-%m-%d"
            ... ).to_native()  # doctest: +NORMALIZE_WHITESPACE
            shape: (2,)
            Series: '' [datetime[μs]]
            [
                    2020-01-01 00:00:00
                    2020-01-02 00:00:00
            ]
        format)r   r   r   r   to_datetimer   rE   s     r   rF   z!SeriesStringNamespace.to_datetimeI  sA    B $$44!!3377CC6CR
 	
r   c                    | j                   j                  | j                   j                  j                  j	                  |            S )aD  Convert to date dtype.

        Warning:
            As different backends auto-infer format in different ways, if `format=None`
            there is no guarantee that the result will be equal.

        Arguments:
            format: Format to use for conversion. If set to None (default), the format is
                inferred from the data.

        Examples:
            >>> import pyarrow as pa
            >>> import narwhals as nw
            >>> s_native = pa.chunked_array([["2020-01-01", "2020-01-02"]])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.to_date(format="%Y-%m-%d").to_native()  # doctest: +ELLIPSIS
            <pyarrow.lib.ChunkedArray object at ...>
            [
              [
                2020-01-01,
                2020-01-02
              ]
            ]
        rD   )r   r   r   r   to_daterG   s     r   rI   zSeriesStringNamespace.to_daten  s@    2 $$44!!3377??v?N
 	
r   c                    | j                   j                  | j                   j                  j                  j	                  |            S )aM  Pad strings with zeros on the left.

        Arguments:
            width: The target width of the string. If the string is shorter than this width, it will be padded with zeros on the left.

        Examples:
            >>> import pandas as pd
            >>> import narwhals as nw
            >>> s_native = pd.Series(["+1", "-23", "456", "123456"])
            >>> s = nw.from_native(s_native, series_only=True)
            >>> s.str.zfill(5).to_native()
            0     +0001
            1     -0023
            2     00456
            3    123456
            dtype: object
        )r   r   r   r   zfill)r   widths     r   rK   zSeriesStringNamespace.zfill  s=    $ $$44!!3377==eD
 	
r   )r   r   returnNone)rM   r   )r   r   rM   r   )
r!   r   r"   str | SeriesTr   boolr   intrM   r   )r!   r   r"   rO   r   rP   rM   r   r   )r(   
str | NonerM   r   )r+   r   rM   r   )r.   r   rM   r   )r!   r   r   rP   rM   r   )r3   rQ   r4   z
int | NonerM   r   )r8   r   rM   r   )   )r   rQ   rM   r   )rE   rR   rM   r   )rL   rQ   rM   r   )__name__
__module____qualname__r   r   r   r    r%   r'   r*   r-   r0   r6   r9   r<   r>   r@   rB   rF   rI   rK    r   r   r	   r	   	   s    '
*I FKUV

#0
>B
OR
	
8 FK

#0
>B
	
4
.
*
* 9> 
6
2
.
<
<
,
"#
J
:
r   r	   N)

__future__r   typingr   r   narwhals.dependenciesr   narwhals.typingr   r	   rW   r   r   <module>r\      s$    "  4 #V
GG, V
r   