From f3102bbb0e2328b7afd84749ad64a8b833b1b336 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 29 Jan 2020 11:16:41 -0800 Subject: [PATCH] ENH: implement pd._typing.DtypeObj --- pandas/_typing.py | 1 + pandas/core/dtypes/common.py | 10 +++++----- pandas/core/dtypes/missing.py | 4 +++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 445eff9e19e47..ef1045a93b325 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -29,6 +29,7 @@ ArrayLike = TypeVar("ArrayLike", "ExtensionArray", np.ndarray) DatetimeLikeScalar = TypeVar("DatetimeLikeScalar", "Period", "Timestamp", "Timedelta") Dtype = Union[str, np.dtype, "ExtensionDtype"] +DtypeObj = Union[np.dtype, "ExtensionDtype"] FilePathOrBuffer = Union[str, Path, IO[AnyStr]] FrameOrSeries = TypeVar("FrameOrSeries", bound="NDFrame") diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 3f4ebc88c1c8a..52e1aa27bb25f 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -48,7 +48,7 @@ is_string_like, ) -from pandas._typing import ArrayLike +from pandas._typing import ArrayLike, DtypeObj _POSSIBLY_CAST_DTYPES = { np.dtype(t).name @@ -1834,7 +1834,7 @@ def _is_dtype(arr_or_dtype, condition) -> bool: return condition(dtype) -def _get_dtype(arr_or_dtype): +def _get_dtype(arr_or_dtype) -> DtypeObj: """ Get the dtype instance associated with an array or dtype object. @@ -2004,7 +2004,7 @@ def _validate_date_like_dtype(dtype) -> None: raise ValueError(msg.format(name=dtype.name, type=dtype.type.__name__)) -def pandas_dtype(dtype): +def pandas_dtype(dtype) -> DtypeObj: """ Convert input into a pandas only dtype object or a numpy dtype object. @@ -2037,7 +2037,7 @@ def pandas_dtype(dtype): npdtype = np.dtype(dtype) except SyntaxError: # np.dtype uses `eval` which can raise SyntaxError - raise TypeError("data type '{}' not understood".format(dtype)) + raise TypeError("data type '{dtype}' not understood".format(dtype=dtype)) # Any invalid dtype (such as pd.Timestamp) should raise an error. # np.dtype(invalid_type).kind = 0 for such objects. However, this will @@ -2049,6 +2049,6 @@ def pandas_dtype(dtype): # here and `dtype` is an array return npdtype elif npdtype.kind == "O": - raise TypeError("dtype '{}' not understood".format(dtype)) + raise TypeError("dtype '{dtype}' not understood".format(dtype=dtype)) return npdtype diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 322011eb8e263..b8eee5bb7fae3 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -9,6 +9,8 @@ import pandas._libs.missing as libmissing from pandas._libs.tslibs import NaT, iNaT +from pandas._typing import DtypeObj + from .common import ( _NS_DTYPE, _TD_DTYPE, @@ -572,7 +574,7 @@ def remove_na_arraylike(arr): return arr[notna(lib.values_from_object(arr))] -def is_valid_nat_for_dtype(obj, dtype): +def is_valid_nat_for_dtype(obj, dtype: DtypeObj) -> bool: """ isna check that excludes incompatible dtypes