1
+ # pyright: strict
1
2
from collections .abc import (
2
3
Callable ,
3
4
Sequence ,
@@ -12,6 +13,7 @@ from typing import (
12
13
)
13
14
14
15
import numpy as np
16
+ import numpy .typing as npt
15
17
import pandas as pd
16
18
from pandas import (
17
19
DataFrame ,
@@ -21,23 +23,36 @@ from pandas import (
21
23
)
22
24
from pandas .core .base import NoNewAttributesMixin
23
25
26
+ from pandas ._libs .tslibs .nattype import NaTType
24
27
from pandas ._typing import (
25
28
JoinHow ,
29
+ Scalar ,
26
30
T ,
27
31
np_ndarray_bool ,
28
32
)
29
33
30
- # The _TS type is what is used for the result of str.split with expand=True
31
- _TS = TypeVar ("_TS" , bound = DataFrame | MultiIndex )
32
- # The _TS2 type is what is used for the result of str.split with expand=False
33
- _TS2 = TypeVar ("_TS2" , bound = Series [list [str ]] | Index [list [str ]])
34
- # The _TM type is what is used for the result of str.match
35
- _TM = TypeVar ("_TM" , bound = Series [bool ] | np_ndarray_bool )
34
+ # Used for the result of str.split with expand=True
35
+ _T_EXPANDING = TypeVar ("_T_EXPANDING" , bound = DataFrame | MultiIndex )
36
+ # Used for the result of str.split with expand=False
37
+ _T_LIST_STR = TypeVar ("_T_LIST_STR" , bound = Series [list [str ]] | Index [list [str ]])
38
+ # Used for the result of str.match
39
+ _T_BOOL = TypeVar ("_T_BOOL" , bound = Series [bool ] | np_ndarray_bool )
40
+ # Used for the result of str.index / str.find
41
+ _T_INT = TypeVar ("_T_INT" , bound = Series [int ] | Index [int ])
42
+ # Used for the result of str.encode
43
+ _T_BYTES = TypeVar ("_T_BYTES" , bound = Series [bytes ] | Index [bytes ])
44
+ # Used for the result of str.decode
45
+ _T_STR = TypeVar ("_T_STR" , bound = Series [str ] | Index [str ])
46
+ # Used for the result of str.partition
47
+ _T_OBJECT = TypeVar ("_T_OBJECT" , bound = Series [type [object ]] | Index [type [object ]])
36
48
37
- class StringMethods (NoNewAttributesMixin , Generic [T , _TS , _TM , _TS2 ]):
49
+ class StringMethods (
50
+ NoNewAttributesMixin ,
51
+ Generic [T , _T_EXPANDING , _T_BOOL , _T_LIST_STR , _T_INT , _T_BYTES , _T_STR , _T_OBJECT ],
52
+ ):
38
53
def __init__ (self , data : T ) -> None : ...
39
- def __getitem__ (self , key : slice | int ) -> T : ...
40
- def __iter__ (self ) -> T : ...
54
+ def __getitem__ (self , key : slice | int ) -> _T_STR : ...
55
+ def __iter__ (self ) -> _T_STR : ...
41
56
@overload
42
57
def cat (
43
58
self ,
@@ -58,15 +73,17 @@ class StringMethods(NoNewAttributesMixin, Generic[T, _TS, _TM, _TS2]):
58
73
@overload
59
74
def cat (
60
75
self ,
61
- others : Series | pd .Index | pd .DataFrame | np .ndarray | list [Any ],
76
+ others : (
77
+ Series [str ] | Index [str ] | pd .DataFrame | npt .NDArray [np .str_ ] | list [str ]
78
+ ),
62
79
sep : str = ...,
63
80
na_rep : str | None = ...,
64
81
join : JoinHow = ...,
65
- ) -> T : ...
82
+ ) -> _T_STR : ...
66
83
@overload
67
84
def split (
68
85
self , pat : str = ..., * , n : int = ..., expand : Literal [True ], regex : bool = ...
69
- ) -> _TS : ...
86
+ ) -> _T_EXPANDING : ...
70
87
@overload
71
88
def split (
72
89
self ,
@@ -75,77 +92,79 @@ class StringMethods(NoNewAttributesMixin, Generic[T, _TS, _TM, _TS2]):
75
92
n : int = ...,
76
93
expand : Literal [False ] = ...,
77
94
regex : bool = ...,
78
- ) -> _TS2 : ...
95
+ ) -> _T_LIST_STR : ...
79
96
@overload
80
- def rsplit (self , pat : str = ..., * , n : int = ..., expand : Literal [True ]) -> _TS : ...
97
+ def rsplit (
98
+ self , pat : str = ..., * , n : int = ..., expand : Literal [True ]
99
+ ) -> _T_EXPANDING : ...
81
100
@overload
82
101
def rsplit (
83
102
self , pat : str = ..., * , n : int = ..., expand : Literal [False ] = ...
84
- ) -> _TS2 : ...
103
+ ) -> _T_LIST_STR : ...
85
104
@overload
86
- def partition (self , sep : str = ...) -> pd . DataFrame : ...
105
+ def partition (self , sep : str = ...) -> _T_EXPANDING : ...
87
106
@overload
88
- def partition (self , * , expand : Literal [True ]) -> pd . DataFrame : ...
107
+ def partition (self , * , expand : Literal [True ]) -> _T_EXPANDING : ...
89
108
@overload
90
- def partition (self , sep : str , expand : Literal [True ]) -> pd . DataFrame : ...
109
+ def partition (self , sep : str , expand : Literal [True ]) -> _T_EXPANDING : ...
91
110
@overload
92
- def partition (self , sep : str , expand : Literal [False ]) -> T : ...
111
+ def partition (self , sep : str , expand : Literal [False ]) -> _T_OBJECT : ...
93
112
@overload
94
- def partition (self , * , expand : Literal [False ]) -> T : ...
113
+ def partition (self , * , expand : Literal [False ]) -> _T_OBJECT : ...
95
114
@overload
96
- def rpartition (self , sep : str = ...) -> pd . DataFrame : ...
115
+ def rpartition (self , sep : str = ...) -> _T_EXPANDING : ...
97
116
@overload
98
- def rpartition (self , * , expand : Literal [True ]) -> pd . DataFrame : ...
117
+ def rpartition (self , * , expand : Literal [True ]) -> _T_EXPANDING : ...
99
118
@overload
100
- def rpartition (self , sep : str , expand : Literal [True ]) -> pd . DataFrame : ...
119
+ def rpartition (self , sep : str , expand : Literal [True ]) -> _T_EXPANDING : ...
101
120
@overload
102
- def rpartition (self , sep : str , expand : Literal [False ]) -> T : ...
121
+ def rpartition (self , sep : str , expand : Literal [False ]) -> _T_OBJECT : ...
103
122
@overload
104
- def rpartition (self , * , expand : Literal [False ]) -> T : ...
105
- def get (self , i : int ) -> T : ...
106
- def join (self , sep : str ) -> T : ...
123
+ def rpartition (self , * , expand : Literal [False ]) -> _T_OBJECT : ...
124
+ def get (self , i : int ) -> _T_STR : ...
125
+ def join (self , sep : str ) -> _T_STR : ...
107
126
def contains (
108
127
self ,
109
- pat : str | re .Pattern ,
128
+ pat : str | re .Pattern [ str ] ,
110
129
case : bool = ...,
111
130
flags : int = ...,
112
- na = ...,
131
+ na : Scalar | NaTType | None = ...,
113
132
regex : bool = ...,
114
- ) -> Series [ bool ] : ...
133
+ ) -> _T_BOOL : ...
115
134
def match (
116
135
self , pat : str , case : bool = ..., flags : int = ..., na : Any = ...
117
- ) -> _TM : ...
136
+ ) -> _T_BOOL : ...
118
137
def replace (
119
138
self ,
120
139
pat : str ,
121
- repl : str | Callable [[re .Match ], str ],
140
+ repl : str | Callable [[re .Match [ str ] ], str ],
122
141
n : int = ...,
123
142
case : bool | None = ...,
124
143
flags : int = ...,
125
144
regex : bool = ...,
126
- ) -> T : ...
127
- def repeat (self , repeats : int | Sequence [int ]) -> T : ...
145
+ ) -> _T_STR : ...
146
+ def repeat (self , repeats : int | Sequence [int ]) -> _T_STR : ...
128
147
def pad (
129
148
self ,
130
149
width : int ,
131
150
side : Literal ["left" , "right" , "both" ] = ...,
132
151
fillchar : str = ...,
133
- ) -> T : ...
134
- def center (self , width : int , fillchar : str = ...) -> T : ...
135
- def ljust (self , width : int , fillchar : str = ...) -> T : ...
136
- def rjust (self , width : int , fillchar : str = ...) -> T : ...
137
- def zfill (self , width : int ) -> T : ...
152
+ ) -> _T_STR : ...
153
+ def center (self , width : int , fillchar : str = ...) -> _T_STR : ...
154
+ def ljust (self , width : int , fillchar : str = ...) -> _T_STR : ...
155
+ def rjust (self , width : int , fillchar : str = ...) -> _T_STR : ...
156
+ def zfill (self , width : int ) -> _T_STR : ...
138
157
def slice (
139
158
self , start : int | None = ..., stop : int | None = ..., step : int | None = ...
140
159
) -> T : ...
141
160
def slice_replace (
142
161
self , start : int | None = ..., stop : int | None = ..., repl : str | None = ...
143
- ) -> T : ...
144
- def decode (self , encoding : str , errors : str = ...) -> T : ...
145
- def encode (self , encoding : str , errors : str = ...) -> T : ...
146
- def strip (self , to_strip : str | None = ...) -> T : ...
147
- def lstrip (self , to_strip : str | None = ...) -> T : ...
148
- def rstrip (self , to_strip : str | None = ...) -> T : ...
162
+ ) -> _T_STR : ...
163
+ def decode (self , encoding : str , errors : str = ...) -> _T_STR : ...
164
+ def encode (self , encoding : str , errors : str = ...) -> _T_BYTES : ...
165
+ def strip (self , to_strip : str | None = ...) -> _T_STR : ...
166
+ def lstrip (self , to_strip : str | None = ...) -> _T_STR : ...
167
+ def rstrip (self , to_strip : str | None = ...) -> _T_STR : ...
149
168
def wrap (
150
169
self ,
151
170
width : int ,
@@ -154,45 +173,47 @@ class StringMethods(NoNewAttributesMixin, Generic[T, _TS, _TM, _TS2]):
154
173
drop_whitespace : bool | None = ...,
155
174
break_long_words : bool | None = ...,
156
175
break_on_hyphens : bool | None = ...,
157
- ) -> T : ...
158
- def get_dummies (self , sep : str = ...) -> pd . DataFrame : ...
159
- def translate (self , table : dict [int , int | str | None ] | None ) -> T : ...
160
- def count (self , pat : str , flags : int = ...) -> Series [ int ] : ...
161
- def startswith (self , pat : str | tuple [str , ...], na : Any = ...) -> Series [ bool ] : ...
162
- def endswith (self , pat : str | tuple [str , ...], na : Any = ...) -> Series [ bool ] : ...
163
- def findall (self , pat : str , flags : int = ...) -> Series : ...
176
+ ) -> _T_STR : ...
177
+ def get_dummies (self , sep : str = ...) -> _T_EXPANDING : ...
178
+ def translate (self , table : dict [int , int | str | None ] | None ) -> _T_STR : ...
179
+ def count (self , pat : str , flags : int = ...) -> _T_INT : ...
180
+ def startswith (self , pat : str | tuple [str , ...], na : Any = ...) -> _T_BOOL : ...
181
+ def endswith (self , pat : str | tuple [str , ...], na : Any = ...) -> _T_BOOL : ...
182
+ def findall (self , pat : str , flags : int = ...) -> _T_LIST_STR : ...
164
183
@overload
165
184
def extract (
166
185
self , pat : str , flags : int = ..., * , expand : Literal [True ] = ...
167
186
) -> pd .DataFrame : ...
168
187
@overload
169
- def extract (self , pat : str , flags : int , expand : Literal [False ]) -> T : ...
188
+ def extract (self , pat : str , flags : int , expand : Literal [False ]) -> _T_OBJECT : ...
170
189
@overload
171
- def extract (self , pat : str , flags : int = ..., * , expand : Literal [False ]) -> T : ...
190
+ def extract (
191
+ self , pat : str , flags : int = ..., * , expand : Literal [False ]
192
+ ) -> _T_OBJECT : ...
172
193
def extractall (self , pat : str , flags : int = ...) -> pd .DataFrame : ...
173
- def find (self , sub : str , start : int = ..., end : int | None = ...) -> T : ...
174
- def rfind (self , sub : str , start : int = ..., end : int | None = ...) -> T : ...
175
- def normalize (self , form : Literal ["NFC" , "NFKC" , "NFD" , "NFKD" ]) -> T : ...
176
- def index (self , sub : str , start : int = ..., end : int | None = ...) -> T : ...
177
- def rindex (self , sub : str , start : int = ..., end : int | None = ...) -> T : ...
178
- def len (self ) -> Series [ int ] : ...
179
- def lower (self ) -> T : ...
180
- def upper (self ) -> T : ...
181
- def title (self ) -> T : ...
182
- def capitalize (self ) -> T : ...
183
- def swapcase (self ) -> T : ...
184
- def casefold (self ) -> T : ...
185
- def isalnum (self ) -> Series [ bool ] : ...
186
- def isalpha (self ) -> Series [ bool ] : ...
187
- def isdigit (self ) -> Series [ bool ] : ...
188
- def isspace (self ) -> Series [ bool ] : ...
189
- def islower (self ) -> Series [ bool ] : ...
190
- def isupper (self ) -> Series [ bool ] : ...
191
- def istitle (self ) -> Series [ bool ] : ...
192
- def isnumeric (self ) -> Series [ bool ] : ...
193
- def isdecimal (self ) -> Series [ bool ] : ...
194
+ def find (self , sub : str , start : int = ..., end : int | None = ...) -> _T_INT : ...
195
+ def rfind (self , sub : str , start : int = ..., end : int | None = ...) -> _T_INT : ...
196
+ def normalize (self , form : Literal ["NFC" , "NFKC" , "NFD" , "NFKD" ]) -> _T_STR : ...
197
+ def index (self , sub : str , start : int = ..., end : int | None = ...) -> _T_INT : ...
198
+ def rindex (self , sub : str , start : int = ..., end : int | None = ...) -> _T_INT : ...
199
+ def len (self ) -> _T_INT : ...
200
+ def lower (self ) -> _T_STR : ...
201
+ def upper (self ) -> _T_STR : ...
202
+ def title (self ) -> _T_STR : ...
203
+ def capitalize (self ) -> _T_STR : ...
204
+ def swapcase (self ) -> _T_STR : ...
205
+ def casefold (self ) -> _T_STR : ...
206
+ def isalnum (self ) -> _T_BOOL : ...
207
+ def isalpha (self ) -> _T_BOOL : ...
208
+ def isdigit (self ) -> _T_BOOL : ...
209
+ def isspace (self ) -> _T_BOOL : ...
210
+ def islower (self ) -> _T_BOOL : ...
211
+ def isupper (self ) -> _T_BOOL : ...
212
+ def istitle (self ) -> _T_BOOL : ...
213
+ def isnumeric (self ) -> _T_BOOL : ...
214
+ def isdecimal (self ) -> _T_BOOL : ...
194
215
def fullmatch (
195
216
self , pat : str , case : bool = ..., flags : int = ..., na : Any = ...
196
- ) -> Series [ bool ] : ...
197
- def removeprefix (self , prefix : str ) -> T : ...
198
- def removesuffix (self , suffix : str ) -> T : ...
217
+ ) -> _T_BOOL : ...
218
+ def removeprefix (self , prefix : str ) -> _T_STR : ...
219
+ def removesuffix (self , suffix : str ) -> _T_STR : ...
0 commit comments