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" diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index eddf46ee362d6..618a35886a905 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( diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 0719b8ce6010b..6120bc92adbfc 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1,4 +1,6 @@ -""" routings for casting """ +""" +Routines for casting. +""" from datetime import date, datetime, timedelta @@ -269,12 +271,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): @@ -662,9 +664,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 @@ -709,7 +710,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"): @@ -1555,8 +1556,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 ------ @@ -1567,19 +1568,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 eb9b880cd10d9..f8e14d1cbc9e9 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 @@ -994,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 @@ -1011,13 +1014,11 @@ 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=np.datetime64)) + >>> is_datetime64_any_dtype(pd.DatetimeIndex([1, 2, 3], dtype="datetime64[ns]")) True """ - if arr_or_dtype is None: return False return is_datetime64_dtype(arr_or_dtype) or is_datetime64tz_dtype(arr_or_dtype) @@ -1034,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 @@ -1051,16 +1052,13 @@ 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 + >>> 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 + >>> is_datetime64_ns_dtype(pd.DatetimeIndex([1, 2, 3], dtype="datetime64[ns]")) True """ - if arr_or_dtype is None: return False try: @@ -1240,7 +1238,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 d00b46700981c..8aaebe89871b6 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