Skip to content

Commit 427ff79

Browse files
simonjayhawkinsTLouf
authored andcommitted
[ArrowStringArray] TYP: use future annotations more (pandas-dev#41601)
1 parent 9873fc9 commit 427ff79

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

pandas/core/strings/accessor.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1+
from __future__ import annotations
2+
13
import codecs
24
from functools import wraps
35
import re
46
from typing import (
57
TYPE_CHECKING,
6-
Dict,
78
Hashable,
8-
List,
9-
Optional,
109
Pattern,
11-
Union,
1210
)
1311
import warnings
1412

@@ -43,7 +41,7 @@
4341
if TYPE_CHECKING:
4442
from pandas import Index
4543

46-
_shared_docs: Dict[str, str] = {}
44+
_shared_docs: dict[str, str] = {}
4745
_cpython_optimized_encoders = (
4846
"utf-8",
4947
"utf8",
@@ -325,7 +323,7 @@ def cons_row(x):
325323
else:
326324
index = self._orig.index
327325
# This is a mess.
328-
dtype: Optional[str]
326+
dtype: str | None
329327
if self._is_string and returns_string:
330328
dtype = self._orig.dtype
331329
else:
@@ -391,7 +389,7 @@ def _get_series_list(self, others):
391389
or (isinstance(x, np.ndarray) and x.ndim == 1)
392390
for x in others
393391
):
394-
los: List[Series] = []
392+
los: list[Series] = []
395393
while others: # iterate through list and append each element
396394
los = los + self._get_series_list(others.pop(0))
397395
return los
@@ -2292,7 +2290,7 @@ def findall(self, pat, flags=0):
22922290
@forbid_nonstring_types(["bytes"])
22932291
def extract(
22942292
self, pat: str, flags: int = 0, expand: bool = True
2295-
) -> Union[FrameOrSeriesUnion, "Index"]:
2293+
) -> FrameOrSeriesUnion | Index:
22962294
r"""
22972295
Extract capture groups in the regex `pat` as columns in a DataFrame.
22982296
@@ -2733,7 +2731,7 @@ def len(self):
27332731
# boolean:
27342732
# isalpha, isnumeric isalnum isdigit isdecimal isspace islower isupper istitle
27352733
# _doc_args holds dict of strings to use in substituting casemethod docs
2736-
_doc_args: Dict[str, Dict[str, str]] = {}
2734+
_doc_args: dict[str, dict[str, str]] = {}
27372735
_doc_args["lower"] = {"type": "lowercase", "method": "lower", "version": ""}
27382736
_doc_args["upper"] = {"type": "uppercase", "method": "upper", "version": ""}
27392737
_doc_args["title"] = {"type": "titlecase", "method": "title", "version": ""}
@@ -2971,7 +2969,7 @@ def casefold(self):
29712969
)
29722970

29732971

2974-
def cat_safe(list_of_columns: List, sep: str):
2972+
def cat_safe(list_of_columns: list, sep: str):
29752973
"""
29762974
Auxiliary function for :meth:`str.cat`.
29772975
@@ -3007,7 +3005,7 @@ def cat_safe(list_of_columns: List, sep: str):
30073005
return result
30083006

30093007

3010-
def cat_core(list_of_columns: List, sep: str):
3008+
def cat_core(list_of_columns: list, sep: str):
30113009
"""
30123010
Auxiliary function for :meth:`str.cat`
30133011
@@ -3053,7 +3051,7 @@ def _get_single_group_name(regex: Pattern) -> Hashable:
30533051
return None
30543052

30553053

3056-
def _get_group_names(regex: Pattern) -> List[Hashable]:
3054+
def _get_group_names(regex: Pattern) -> list[Hashable]:
30573055
"""
30583056
Get named groups from compiled regex.
30593057
@@ -3119,7 +3117,7 @@ def str_extract(accessor: StringMethods, pat: str, flags: int = 0, expand: bool
31193117
else:
31203118
result_list = _str_extract(obj.array, pat, flags=flags, expand=returns_df)
31213119

3122-
result_index: Optional["Index"]
3120+
result_index: Index | None
31233121
if isinstance(obj, ABCSeries):
31243122
result_index = obj.index
31253123
else:

pandas/core/strings/base.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
from __future__ import annotations
2+
13
import abc
2-
from typing import (
3-
Pattern,
4-
Union,
5-
)
4+
from typing import Pattern
65

76
import numpy as np
87

@@ -68,7 +67,7 @@ def _str_match(
6867
@abc.abstractmethod
6968
def _str_fullmatch(
7069
self,
71-
pat: Union[str, Pattern],
70+
pat: str | Pattern,
7271
case: bool = True,
7372
flags: int = 0,
7473
na: Scalar = np.nan,

pandas/core/strings/object_array.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1+
from __future__ import annotations
2+
13
import re
24
import textwrap
3-
from typing import (
4-
Optional,
5-
Pattern,
6-
Set,
7-
Union,
8-
)
5+
from typing import Pattern
96
import unicodedata
107

118
import numpy as np
@@ -38,7 +35,7 @@ def __len__(self):
3835
# For typing, _str_map relies on the object being sized.
3936
raise NotImplementedError
4037

41-
def _str_map(self, f, na_value=None, dtype: Optional[Dtype] = None):
38+
def _str_map(self, f, na_value=None, dtype: Dtype | None = None):
4239
"""
4340
Map a callable over valid element of the array.
4441
@@ -198,7 +195,7 @@ def _str_match(
198195

199196
def _str_fullmatch(
200197
self,
201-
pat: Union[str, Pattern],
198+
pat: str | Pattern,
202199
case: bool = True,
203200
flags: int = 0,
204201
na: Scalar = None,
@@ -339,7 +336,7 @@ def _str_get_dummies(self, sep="|"):
339336
except TypeError:
340337
arr = sep + arr.astype(str) + sep
341338

342-
tags: Set[str] = set()
339+
tags: set[str] = set()
343340
for ts in Series(arr).str.split(sep):
344341
tags.update(ts)
345342
tags2 = sorted(tags - {""})

0 commit comments

Comments
 (0)