From 039035e8776c5141af5592fe763ec5a2edcd8565 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Sat, 2 Jan 2021 00:19:47 +0100 Subject: [PATCH 1/2] CLN: add typing for dtype arg in directories core/indexes and core/strings (GH38808) --- pandas/core/indexes/category.py | 10 ++++++++-- pandas/core/indexes/datetimes.py | 4 ++-- pandas/core/indexes/interval.py | 20 +++++++++++++++----- pandas/core/indexes/numeric.py | 4 ++-- pandas/core/indexes/period.py | 6 +++--- pandas/core/indexes/range.py | 16 ++++++++++++---- pandas/core/strings/object_array.py | 6 +++--- 7 files changed, 45 insertions(+), 21 deletions(-) diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 9062667298d7c..24920e9b5faa7 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -7,7 +7,7 @@ from pandas._libs import index as libindex from pandas._libs.lib import no_default -from pandas._typing import ArrayLike, Label +from pandas._typing import ArrayLike, Dtype, Label from pandas.util._decorators import Appender, doc from pandas.core.dtypes.common import ( @@ -180,7 +180,13 @@ def _engine_type(self): # Constructors def __new__( - cls, data=None, categories=None, ordered=None, dtype=None, copy=False, name=None + cls, + data=None, + categories=None, + ordered=None, + dtype: Optional[Dtype] = None, + copy=False, + name=None, ): name = maybe_extract_name(name, data, cls) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index d176b6a5d8e6d..f82ee27aef534 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -14,7 +14,7 @@ to_offset, ) from pandas._libs.tslibs.offsets import prefix_mapping -from pandas._typing import DtypeObj +from pandas._typing import Dtype, DtypeObj from pandas.errors import InvalidIndexError from pandas.util._decorators import cache_readonly, doc @@ -289,7 +289,7 @@ def __new__( ambiguous="raise", dayfirst=False, yearfirst=False, - dtype=None, + dtype: Optional[Dtype] = None, copy=False, name=None, ): diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 054b21d2857ff..aabc3e741641f 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -11,7 +11,7 @@ from pandas._libs import lib from pandas._libs.interval import Interval, IntervalMixin, IntervalTree from pandas._libs.tslibs import BaseOffset, Timedelta, Timestamp, to_offset -from pandas._typing import DtypeObj, Label +from pandas._typing import Dtype, DtypeObj, Label from pandas.errors import InvalidIndexError from pandas.util._decorators import Appender, cache_readonly from pandas.util._exceptions import rewrite_exception @@ -192,7 +192,7 @@ def __new__( cls, data, closed=None, - dtype=None, + dtype: Optional[Dtype] = None, copy: bool = False, name=None, verify_integrity: bool = True, @@ -249,7 +249,12 @@ def _simple_new(cls, array: IntervalArray, name: Label = None): } ) def from_breaks( - cls, breaks, closed: str = "right", name=None, copy: bool = False, dtype=None + cls, + breaks, + closed: str = "right", + name=None, + copy: bool = False, + dtype: Optional[Dtype] = None, ): with rewrite_exception("IntervalArray", cls.__name__): array = IntervalArray.from_breaks( @@ -281,7 +286,7 @@ def from_arrays( closed: str = "right", name=None, copy: bool = False, - dtype=None, + dtype: Optional[Dtype] = None, ): with rewrite_exception("IntervalArray", cls.__name__): array = IntervalArray.from_arrays( @@ -307,7 +312,12 @@ def from_arrays( } ) def from_tuples( - cls, data, closed: str = "right", name=None, copy: bool = False, dtype=None + cls, + data, + closed: str = "right", + name=None, + copy: bool = False, + dtype: Optional[Dtype] = None, ): with rewrite_exception("IntervalArray", cls.__name__): arr = IntervalArray.from_tuples(data, closed=closed, copy=copy, dtype=dtype) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 2c2888e1c6f72..59793d1a63813 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -1,4 +1,4 @@ -from typing import Any +from typing import Any, Optional import warnings import numpy as np @@ -45,7 +45,7 @@ class NumericIndex(Index): _is_numeric_dtype = True _can_hold_strings = False - def __new__(cls, data=None, dtype=None, copy=False, name=None): + def __new__(cls, data=None, dtype: Optional[Dtype] = None, copy=False, name=None): name = maybe_extract_name(name, data, cls) subarr = cls._ensure_array(data, dtype, copy) diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 7746d7e617f8b..7762198246603 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -1,5 +1,5 @@ from datetime import datetime, timedelta -from typing import Any +from typing import Any, Optional import warnings import numpy as np @@ -7,7 +7,7 @@ from pandas._libs import index as libindex, lib from pandas._libs.tslibs import BaseOffset, Period, Resolution, Tick from pandas._libs.tslibs.parsing import DateParseError, parse_time_string -from pandas._typing import DtypeObj +from pandas._typing import Dtype, DtypeObj from pandas.errors import InvalidIndexError from pandas.util._decorators import cache_readonly, doc @@ -190,7 +190,7 @@ def __new__( data=None, ordinal=None, freq=None, - dtype=None, + dtype: Optional[Dtype] = None, copy=False, name=None, **fields, diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 029c4a30a6b22..4256ad93695e9 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -8,7 +8,7 @@ from pandas._libs import index as libindex from pandas._libs.lib import no_default -from pandas._typing import Label +from pandas._typing import Dtype, Label from pandas.compat.numpy import function as nv from pandas.util._decorators import cache_readonly, doc @@ -83,7 +83,13 @@ class RangeIndex(Int64Index): # Constructors def __new__( - cls, start=None, stop=None, step=None, dtype=None, copy=False, name=None + cls, + start=None, + stop=None, + step=None, + dtype: Optional[Dtype] = None, + copy=False, + name=None, ): cls._validate_dtype(dtype) @@ -113,7 +119,9 @@ def __new__( return cls._simple_new(rng, name=name) @classmethod - def from_range(cls, data: range, name=None, dtype=None) -> "RangeIndex": + def from_range( + cls, data: range, name=None, dtype: Optional[Dtype] = None + ) -> "RangeIndex": """ Create RangeIndex from a range object. @@ -405,7 +413,7 @@ def _shallow_copy(self, values=None, name: Label = no_default): return result @doc(Int64Index.copy) - def copy(self, name=None, deep=False, dtype=None, names=None): + def copy(self, name=None, deep=False, dtype: Optional[Dtype] = None, names=None): name = self._validate_names(name=name, names=names, deep=deep)[0] new_index = self._shallow_copy(name=name) diff --git a/pandas/core/strings/object_array.py b/pandas/core/strings/object_array.py index a29d84edd3a77..0196cc86becf5 100644 --- a/pandas/core/strings/object_array.py +++ b/pandas/core/strings/object_array.py @@ -1,6 +1,6 @@ import re import textwrap -from typing import Pattern, Set, Union, cast +from typing import Optional, Pattern, Set, Union, cast import unicodedata import warnings @@ -9,7 +9,7 @@ import pandas._libs.lib as lib import pandas._libs.missing as libmissing import pandas._libs.ops as libops -from pandas._typing import Scalar +from pandas._typing import NpDtype, Scalar from pandas.core.dtypes.common import is_re, is_scalar from pandas.core.dtypes.missing import isna @@ -28,7 +28,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=None): + def _str_map(self, f, na_value=None, dtype: Optional[NpDtype] = None): """ Map a callable over valid element of the array. From 64ced2b4bafc0fdbd468ac2daafe14f0d8ec4a0b Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Sun, 3 Jan 2021 22:37:23 +0100 Subject: [PATCH 2/2] Address comments --- pandas/core/strings/object_array.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/strings/object_array.py b/pandas/core/strings/object_array.py index 0196cc86becf5..471f1e521b991 100644 --- a/pandas/core/strings/object_array.py +++ b/pandas/core/strings/object_array.py @@ -9,7 +9,7 @@ import pandas._libs.lib as lib import pandas._libs.missing as libmissing import pandas._libs.ops as libops -from pandas._typing import NpDtype, Scalar +from pandas._typing import Dtype, Scalar from pandas.core.dtypes.common import is_re, is_scalar from pandas.core.dtypes.missing import isna @@ -28,7 +28,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[NpDtype] = None): + def _str_map(self, f, na_value=None, dtype: Optional[Dtype] = None): """ Map a callable over valid element of the array.