Skip to content

Commit

Permalink
[ArrowStringArray] TYP: use future annotations more (pandas-dev#41601)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjayhawkins authored and JulianWgs committed Jul 3, 2021
1 parent 483ee44 commit ab52b08
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
24 changes: 11 additions & 13 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from __future__ import annotations

import codecs
from functools import wraps
import re
from typing import (
TYPE_CHECKING,
Dict,
Hashable,
List,
Optional,
Pattern,
Union,
)
import warnings

Expand Down Expand Up @@ -43,7 +41,7 @@
if TYPE_CHECKING:
from pandas import Index

_shared_docs: Dict[str, str] = {}
_shared_docs: dict[str, str] = {}
_cpython_optimized_encoders = (
"utf-8",
"utf8",
Expand Down Expand Up @@ -325,7 +323,7 @@ def cons_row(x):
else:
index = self._orig.index
# This is a mess.
dtype: Optional[str]
dtype: str | None
if self._is_string and returns_string:
dtype = self._orig.dtype
else:
Expand Down Expand Up @@ -391,7 +389,7 @@ def _get_series_list(self, others):
or (isinstance(x, np.ndarray) and x.ndim == 1)
for x in others
):
los: List[Series] = []
los: list[Series] = []
while others: # iterate through list and append each element
los = los + self._get_series_list(others.pop(0))
return los
Expand Down Expand Up @@ -2292,7 +2290,7 @@ def findall(self, pat, flags=0):
@forbid_nonstring_types(["bytes"])
def extract(
self, pat: str, flags: int = 0, expand: bool = True
) -> Union[FrameOrSeriesUnion, "Index"]:
) -> FrameOrSeriesUnion | Index:
r"""
Extract capture groups in the regex `pat` as columns in a DataFrame.
Expand Down Expand Up @@ -2733,7 +2731,7 @@ def len(self):
# boolean:
# isalpha, isnumeric isalnum isdigit isdecimal isspace islower isupper istitle
# _doc_args holds dict of strings to use in substituting casemethod docs
_doc_args: Dict[str, Dict[str, str]] = {}
_doc_args: dict[str, dict[str, str]] = {}
_doc_args["lower"] = {"type": "lowercase", "method": "lower", "version": ""}
_doc_args["upper"] = {"type": "uppercase", "method": "upper", "version": ""}
_doc_args["title"] = {"type": "titlecase", "method": "title", "version": ""}
Expand Down Expand Up @@ -2971,7 +2969,7 @@ def casefold(self):
)


def cat_safe(list_of_columns: List, sep: str):
def cat_safe(list_of_columns: list, sep: str):
"""
Auxiliary function for :meth:`str.cat`.
Expand Down Expand Up @@ -3007,7 +3005,7 @@ def cat_safe(list_of_columns: List, sep: str):
return result


def cat_core(list_of_columns: List, sep: str):
def cat_core(list_of_columns: list, sep: str):
"""
Auxiliary function for :meth:`str.cat`
Expand Down Expand Up @@ -3053,7 +3051,7 @@ def _get_single_group_name(regex: Pattern) -> Hashable:
return None


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

result_index: Optional["Index"]
result_index: Index | None
if isinstance(obj, ABCSeries):
result_index = obj.index
else:
Expand Down
9 changes: 4 additions & 5 deletions pandas/core/strings/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import annotations

import abc
from typing import (
Pattern,
Union,
)
from typing import Pattern

import numpy as np

Expand Down Expand Up @@ -68,7 +67,7 @@ def _str_match(
@abc.abstractmethod
def _str_fullmatch(
self,
pat: Union[str, Pattern],
pat: str | Pattern,
case: bool = True,
flags: int = 0,
na: Scalar = np.nan,
Expand Down
15 changes: 6 additions & 9 deletions pandas/core/strings/object_array.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from __future__ import annotations

import re
import textwrap
from typing import (
Optional,
Pattern,
Set,
Union,
)
from typing import Pattern
import unicodedata

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

def _str_map(self, f, na_value=None, dtype: Optional[Dtype] = None):
def _str_map(self, f, na_value=None, dtype: Dtype | None = None):
"""
Map a callable over valid element of the array.
Expand Down Expand Up @@ -198,7 +195,7 @@ def _str_match(

def _str_fullmatch(
self,
pat: Union[str, Pattern],
pat: str | Pattern,
case: bool = True,
flags: int = 0,
na: Scalar = None,
Expand Down Expand Up @@ -339,7 +336,7 @@ def _str_get_dummies(self, sep="|"):
except TypeError:
arr = sep + arr.astype(str) + sep

tags: Set[str] = set()
tags: set[str] = set()
for ts in Series(arr).str.split(sep):
tags.update(ts)
tags2 = sorted(tags - {""})
Expand Down

0 comments on commit ab52b08

Please sign in to comment.