From 1870210ad4c9004f769f84142c6068b32e3ff460 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 30 Jan 2020 15:34:03 +0200 Subject: [PATCH 1/4] DOC: Fixed example section in pandas/core/dtypes/*.py --- pandas/core/dtypes/base.py | 10 +++++++--- pandas/core/dtypes/cast.py | 26 +++++++++++++------------- pandas/core/dtypes/common.py | 23 +++++++++-------------- pandas/core/dtypes/concat.py | 4 +++- pandas/core/dtypes/dtypes.py | 29 ++++++++++++++++++----------- pandas/core/dtypes/inference.py | 5 ++++- 6 files changed, 54 insertions(+), 43 deletions(-) diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index 1b4e7062b38e5..2aee397fbe739 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -1,4 +1,7 @@ -"""Extend pandas with custom array types""" +""" +Extend pandas with custom array types. +""" + from typing import Any, List, Optional, Tuple, Type import numpy as np @@ -231,8 +234,9 @@ def construct_from_string(cls, string: str): ... if match: ... return cls(**match.groupdict()) ... else: - ... raise TypeError(f"Cannot construct a '{cls.__name__}' from - ... " "'{string}'") + ... raise TypeError( + ... f"Cannot construct a '{cls.__name__}' from '{string}'" + ... ) """ if not isinstance(string, str): raise TypeError(f"Expects a string, got {type(string).__name__}") diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 52c569793e499..c89426e31fd1f 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1,4 +1,6 @@ -""" routings for casting """ +""" +Routings for casting. +""" from datetime import datetime, timedelta @@ -262,12 +264,12 @@ def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray, other): Examples -------- - >>> result, _ = maybe_upcast_putmask(np.arange(1,6), - np.array([False, True, False, True, True]), np.arange(21,23)) + >>> arr = np.arange(1, 6) + >>> mask = np.array([False, True, False, True, True]) + >>> result, _ = maybe_upcast_putmask(arr, mask, False) >>> result - array([1, 21, 3, 22, 21]) + array([1, 0, 3, 0, 0]) """ - if not isinstance(result, np.ndarray): raise ValueError("The result input must be a ndarray.") if not is_scalar(other): @@ -655,9 +657,8 @@ def infer_dtype_from_array(arr, pandas_dtype: bool = False): array(['1', '1'], dtype='>> infer_dtype_from_array([1, '1']) - (numpy.object_, [1, '1']) + (, [1, '1']) """ - if isinstance(arr, np.ndarray): return arr.dtype, arr @@ -702,7 +703,7 @@ def maybe_infer_dtype_type(element): >>> from collections import namedtuple >>> Foo = namedtuple("Foo", "dtype") >>> maybe_infer_dtype_type(Foo(np.dtype("i8"))) - numpy.int64 + dtype('int64') """ tipo = None if hasattr(element, "dtype"): @@ -1548,8 +1549,8 @@ def maybe_cast_to_integer_array(arr, dtype, copy: bool = False): Returns ------- - int_arr : ndarray - An array of integer or unsigned integer dtype + ndarray + Array of integer or unsigned integer dtype. Raises ------ @@ -1560,19 +1561,18 @@ def maybe_cast_to_integer_array(arr, dtype, copy: bool = False): -------- If you try to coerce negative values to unsigned integers, it raises: - >>> Series([-1], dtype="uint64") + >>> pd.Series([-1], dtype="uint64") Traceback (most recent call last): ... OverflowError: Trying to coerce negative values to unsigned integers Also, if you try to coerce float values to integers, it raises: - >>> Series([1, 2, 3.5], dtype="int64") + >>> pd.Series([1, 2, 3.5], dtype="int64") Traceback (most recent call last): ... ValueError: Trying to coerce float values to integers """ - try: if not hasattr(arr, "astype"): casted = np.array(arr, dtype=dtype, copy=copy) diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index f62f03be9b732..7508497da1fa2 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1,4 +1,7 @@ -""" common type operations """ +""" +Common type operations. +""" + from typing import Any, Callable, Union import warnings @@ -705,7 +708,7 @@ def is_dtype_equal(source, target) -> bool: False >>> is_dtype_equal(CategoricalDtype(), "category") True - >>> is_dtype_equal(DatetimeTZDtype(), "datetime64") + >>> is_dtype_equal(DatetimeTZDtype(tz="UTC"), "datetime64") False """ @@ -862,7 +865,7 @@ def is_signed_integer_dtype(arr_or_dtype) -> bool: True >>> is_signed_integer_dtype('Int8') True - >>> is_signed_dtype(pd.Int8Dtype) + >>> is_signed_integer_dtype(pd.Int8Dtype) True >>> is_signed_integer_dtype(np.datetime64) False @@ -1013,11 +1016,7 @@ def is_datetime64_any_dtype(arr_or_dtype) -> bool: False >>> is_datetime64_any_dtype(np.array([], dtype=np.datetime64)) True - >>> is_datetime64_any_dtype(pd.DatetimeIndex([1, 2, 3], - dtype=np.datetime64)) - True """ - if arr_or_dtype is None: return False return is_datetime64_dtype(arr_or_dtype) or is_datetime64tz_dtype(arr_or_dtype) @@ -1053,14 +1052,9 @@ def is_datetime64_ns_dtype(arr_or_dtype) -> bool: False >>> is_datetime64_ns_dtype(np.array([], dtype=np.datetime64)) # no unit False - >>> is_datetime64_ns_dtype(np.array([], - dtype="datetime64[ps]")) # wrong unit + >>> is_datetime64_ns_dtype(np.array([], dtype="datetime64[ps]")) # wrong unit False - >>> is_datetime64_ns_dtype(pd.DatetimeIndex([1, 2, 3], - dtype=np.datetime64)) # has 'ns' unit - True """ - if arr_or_dtype is None: return False try: @@ -1240,7 +1234,8 @@ def is_datetimelike_v_numeric(a, b): Examples -------- - >>> dt = np.datetime64(pd.datetime(2017, 1, 1)) + >>> from datetime import datetime + >>> dt = np.datetime64(datetime(2017, 1, 1)) >>> >>> is_datetimelike_v_numeric(1, 1) False diff --git a/pandas/core/dtypes/concat.py b/pandas/core/dtypes/concat.py index cd4b5af4588e5..fdc2eeb34b4ed 100644 --- a/pandas/core/dtypes/concat.py +++ b/pandas/core/dtypes/concat.py @@ -1,5 +1,5 @@ """ -Utility functions related to concat +Utility functions related to concat. """ import numpy as np @@ -261,6 +261,8 @@ def union_categoricals( >>> a = pd.Categorical(["a", "b"], ordered=True) >>> b = pd.Categorical(["a", "b", "c"], ordered=True) >>> union_categoricals([a, b]) + Traceback (most recent call last): + ... TypeError: to union ordered Categoricals, all categories must be the same New in version 0.20.0 diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 93522abc3a48f..f9c29c41dcf74 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -1,4 +1,7 @@ -""" define extension dtypes """ +""" +Define extension dtypes. +""" + import re from typing import Any, Dict, List, MutableMapping, Optional, Tuple, Type, Union, cast @@ -286,23 +289,27 @@ def _from_values_or_dtype( Examples -------- - >>> CategoricalDtype._from_values_or_dtype() + >>> pd.CategoricalDtype._from_values_or_dtype() CategoricalDtype(categories=None, ordered=None) - >>> CategoricalDtype._from_values_or_dtype(categories=['a', 'b'], - ... ordered=True) + >>> pd.CategoricalDtype._from_values_or_dtype( + ... categories=['a', 'b'], ordered=True + ... ) CategoricalDtype(categories=['a', 'b'], ordered=True) - >>> dtype1 = CategoricalDtype(['a', 'b'], ordered=True) - >>> dtype2 = CategoricalDtype(['x', 'y'], ordered=False) - >>> c = Categorical([0, 1], dtype=dtype1, fastpath=True) - >>> CategoricalDtype._from_values_or_dtype(c, ['x', 'y'], ordered=True, - ... dtype=dtype2) + >>> dtype1 = pd.CategoricalDtype(['a', 'b'], ordered=True) + >>> dtype2 = pd.CategoricalDtype(['x', 'y'], ordered=False) + >>> c = pd.Categorical([0, 1], dtype=dtype1, fastpath=True) + >>> pd.CategoricalDtype._from_values_or_dtype( + ... c, ['x', 'y'], ordered=True, dtype=dtype2 + ... ) + Traceback (most recent call last): + ... ValueError: Cannot specify `categories` or `ordered` together with `dtype`. The supplied dtype takes precedence over values' dtype: - >>> CategoricalDtype._from_values_or_dtype(c, dtype=dtype2) - CategoricalDtype(['x', 'y'], ordered=False) + >>> pd.CategoricalDtype._from_values_or_dtype(c, dtype=dtype2) + CategoricalDtype(categories=['x', 'y'], ordered=False) """ from pandas.core.dtypes.common import is_categorical diff --git a/pandas/core/dtypes/inference.py b/pandas/core/dtypes/inference.py index 37bca76802843..a9cd696633273 100644 --- a/pandas/core/dtypes/inference.py +++ b/pandas/core/dtypes/inference.py @@ -117,7 +117,8 @@ def is_file_like(obj) -> bool: Examples -------- - >>> buffer(StringIO("data")) + >>> import io + >>> buffer = io.StringIO("data") >>> is_file_like(buffer) True >>> is_file_like([1, 2, 3]) @@ -311,6 +312,7 @@ def is_named_tuple(obj) -> bool: Examples -------- + >>> from collections import namedtuple >>> Point = namedtuple("Point", ["x", "y"]) >>> p = Point(1, 2) >>> @@ -339,6 +341,7 @@ def is_hashable(obj) -> bool: Examples -------- + >>> import collections >>> a = ([],) >>> isinstance(a, collections.abc.Hashable) True From a5063086a9c4325c056652f3604fcaf15aaee9cf Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sat, 1 Feb 2020 12:25:20 +0200 Subject: [PATCH 2/4] Fixes for review REF: https://github.com/pandas-dev/pandas/pull/31451#discussion_r373501601 https://github.com/pandas-dev/pandas/pull/31451#discussion_r373502296 --- pandas/core/dtypes/common.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index a14eb7907c4ba..f8e14d1cbc9e9 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -997,7 +997,7 @@ def is_datetime64_any_dtype(arr_or_dtype) -> bool: Returns ------- - boolean + bool Whether or not the array or dtype is of the datetime64 dtype. Examples @@ -1014,7 +1014,9 @@ def is_datetime64_any_dtype(arr_or_dtype) -> bool: False >>> is_datetime64_any_dtype(np.array([1, 2])) False - >>> is_datetime64_any_dtype(np.array([], dtype=np.datetime64)) + >>> is_datetime64_any_dtype(np.array([], dtype="datetime64[ns]")) + True + >>> is_datetime64_any_dtype(pd.DatetimeIndex([1, 2, 3], dtype="datetime64[ns]")) True """ if arr_or_dtype is None: @@ -1033,7 +1035,7 @@ def is_datetime64_ns_dtype(arr_or_dtype) -> bool: Returns ------- - boolean + bool Whether or not the array or dtype is of the datetime64[ns] dtype. Examples @@ -1050,10 +1052,12 @@ def is_datetime64_ns_dtype(arr_or_dtype) -> bool: False >>> is_datetime64_ns_dtype(np.array([1, 2])) False - >>> is_datetime64_ns_dtype(np.array([], dtype=np.datetime64)) # no unit + >>> is_datetime64_ns_dtype(np.array([], dtype="datetime64")) # no unit False >>> is_datetime64_ns_dtype(np.array([], dtype="datetime64[ps]")) # wrong unit False + >>> is_datetime64_ns_dtype(pd.DatetimeIndex([1, 2, 3], dtype="datetime64[ns]")) + True """ if arr_or_dtype is None: return False From 4f1ace4a1f94714ffb754719e1d5c0eaa633a73c Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <50263213+MomIsBestFriend@users.noreply.github.com> Date: Fri, 7 Feb 2020 12:25:46 +0200 Subject: [PATCH 3/4] Update pandas/core/dtypes/cast.py Co-Authored-By: Joris Van den Bossche --- pandas/core/dtypes/cast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index c89426e31fd1f..b952d3220f647 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1,5 +1,5 @@ """ -Routings for casting. +Routines for casting. """ from datetime import datetime, timedelta From 2a330b42c98e53ede8d96ce3968306618946d0fa Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 7 Feb 2020 12:29:12 +0200 Subject: [PATCH 4/4] Added check in the CI as suggested REF: https://github.com/pandas-dev/pandas/pull/31451#pullrequestreview-352072757 --- ci/code_checks.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index b46989894ae12..fdc9fef5d7f77 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -305,6 +305,10 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then pandas/core/arrays/boolean.py RET=$(($RET + $?)) ; echo $MSG "DONE" + MSG='Doctests dtypes'; echo $MSG + pytest -q --doctest-modules pandas/core/dtypes/ + RET=$(($RET + $?)) ; echo $MSG "DONE" + MSG='Doctests arrays/boolean.py' ; echo $MSG pytest -q --doctest-modules pandas/core/arrays/boolean.py RET=$(($RET + $?)) ; echo $MSG "DONE"