From 54f2c52f533cd9f3750f7d04404df7d30874f587 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 31 Jan 2020 12:33:48 -0800 Subject: [PATCH 01/15] TYP: Arraylike --- pandas/core/arrays/base.py | 6 +++--- pandas/core/base.py | 6 +++--- pandas/core/indexes/base.py | 8 ++++---- pandas/core/ops/__init__.py | 21 +++++++-------------- pandas/core/ops/array_ops.py | 15 +++++---------- pandas/core/ops/dispatch.py | 10 +++++----- pandas/io/pytables.py | 6 +++--- 7 files changed, 30 insertions(+), 42 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index c3c91cea43f6b..283d13c0204d7 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -451,7 +451,7 @@ def isna(self) -> ArrayLike: Returns ------- - na_values : Union[np.ndarray, ExtensionArray] + na_values : ArrayLike In most cases, this should return a NumPy ndarray. For exceptional cases like ``SparseArray``, where returning an ndarray would be expensive, an ExtensionArray may be @@ -922,7 +922,7 @@ def copy(self) -> ABCExtensionArray: """ raise AbstractMethodError(self) - def view(self, dtype=None) -> Union[ABCExtensionArray, np.ndarray]: + def view(self, dtype=None) -> ArrayLike: """ Return a view on the array. @@ -1175,7 +1175,7 @@ def _create_method(cls, op, coerce_to_dtype=True): Returns ------- - Callable[[Any, Any], Union[ndarray, ExtensionArray]] + Callable[[Any, Any], ArrayLike] A method that can be bound to a class. When used, the method receives the two arguments, one of which is the instance of this class, and should return an ExtensionArray or an ndarray. diff --git a/pandas/core/base.py b/pandas/core/base.py index 9fe1af776dd2b..c59193d826d7e 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -4,12 +4,12 @@ import builtins import textwrap -from typing import Dict, FrozenSet, List, Optional, Union +from typing import Dict, FrozenSet, List, Optional import numpy as np import pandas._libs.lib as lib -from pandas._typing import T +from pandas._typing import ArrayLike, T from pandas.compat import PYPY from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError @@ -606,7 +606,7 @@ class IndexOpsMixin: ) @property - def _values(self) -> Union[ExtensionArray, np.ndarray]: + def _values(self) -> ArrayLike: # must be defined here as a property for mypy raise AbstractMethodError(self) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 22ba317e78e63..16e64e9ee3e3d 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1,7 +1,7 @@ from datetime import datetime import operator from textwrap import dedent -from typing import Any, FrozenSet, Hashable, Optional, Union +from typing import Any, FrozenSet, Hashable, Optional import warnings import numpy as np @@ -12,7 +12,7 @@ from pandas._libs.tslibs import OutOfBoundsDatetime, Timestamp from pandas._libs.tslibs.period import IncompatibleFrequency from pandas._libs.tslibs.timezones import tz_compare -from pandas._typing import Label +from pandas._typing import ArrayLike, Label from pandas.compat import set_function_name from pandas.compat.numpy import function as nv from pandas.util._decorators import Appender, Substitution, cache_readonly @@ -243,7 +243,7 @@ def _outer_indexer(self, left, right): return libjoin.outer_join_indexer(left, right) _typ = "index" - _data: Union[ExtensionArray, np.ndarray] + _data: ArrayLike _id = None _name: Label = None # MultiIndex.levels previously allowed setting the index name. We @@ -3891,7 +3891,7 @@ def array(self) -> ExtensionArray: return array @property - def _values(self) -> Union[ExtensionArray, np.ndarray]: + def _values(self) -> ArrayLike: """ The best array representation. diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 6d2253c5dc87d..b803b02d3da74 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -5,22 +5,17 @@ """ import datetime import operator -from typing import Optional, Set, Tuple, Union +from typing import TYPE_CHECKING, Optional, Set, Tuple import numpy as np from pandas._libs import Timedelta, Timestamp, lib from pandas._libs.ops_dispatch import maybe_dispatch_ufunc_to_dunder_op # noqa:F401 -from pandas._typing import Level +from pandas._typing import ArrayLike, Level from pandas.util._decorators import Appender from pandas.core.dtypes.common import is_list_like, is_timedelta64_dtype -from pandas.core.dtypes.generic import ( - ABCDataFrame, - ABCExtensionArray, - ABCIndexClass, - ABCSeries, -) +from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries from pandas.core.dtypes.missing import isna from pandas.core.construction import extract_array @@ -61,6 +56,9 @@ rxor, ) +if TYPE_CHECKING: + from pandas import Index, Series # noqa: F401 + # ----------------------------------------------------------------------------- # constants ARITHMETIC_BINOPS: Set[str] = { @@ -449,12 +447,7 @@ def _align_method_SERIES(left, right, align_asobject=False): return left, right -def _construct_result( - left: ABCSeries, - result: Union[np.ndarray, ABCExtensionArray], - index: ABCIndexClass, - name, -): +def _construct_result(left: "Series", result: ArrayLike, index: "Index", name): """ Construct an appropriately-labelled Series from the result of an op. diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index c393b8028113b..7208281b333f0 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -4,11 +4,12 @@ """ from functools import partial import operator -from typing import Any, Optional, Union +from typing import Any, Optional import numpy as np from pandas._libs import Timestamp, lib, ops as libops +from pandas._typing import ArrayLike from pandas.core.dtypes.cast import ( construct_1d_object_array_from_listlike, @@ -155,9 +156,7 @@ def na_arithmetic_op(left, right, op, str_rep: str): return missing.dispatch_fill_zeros(op, left, right, result) -def arithmetic_op( - left: Union[np.ndarray, ABCExtensionArray], right: Any, op, str_rep: str -): +def arithmetic_op(left: ArrayLike, right: Any, op, str_rep: str): """ Evaluate an arithmetic operation `+`, `-`, `*`, `/`, `//`, `%`, `**`, ... @@ -200,9 +199,7 @@ def arithmetic_op( return res_values -def comparison_op( - left: Union[np.ndarray, ABCExtensionArray], right: Any, op -) -> Union[np.ndarray, ABCExtensionArray]: +def comparison_op(left: ArrayLike, right: Any, op) -> ArrayLike: """ Evaluate a comparison operation `=`, `!=`, `>=`, `>`, `<=`, or `<`. @@ -303,9 +300,7 @@ def na_logical_op(x: np.ndarray, y, op): return result.reshape(x.shape) -def logical_op( - left: Union[np.ndarray, ABCExtensionArray], right: Any, op -) -> Union[np.ndarray, ABCExtensionArray]: +def logical_op(left: ArrayLike, right: Any, op) -> ArrayLike: """ Evaluate a logical operation `|`, `&`, or `^`. diff --git a/pandas/core/ops/dispatch.py b/pandas/core/ops/dispatch.py index 61a3032c7a02c..5c34cb20be266 100644 --- a/pandas/core/ops/dispatch.py +++ b/pandas/core/ops/dispatch.py @@ -1,10 +1,12 @@ """ Functions for defining unary operations. """ -from typing import Any, Union +from typing import Any import numpy as np +from pandas._typing import ArrayLike + from pandas.core.dtypes.common import ( is_datetime64_dtype, is_extension_array_dtype, @@ -13,7 +15,7 @@ is_scalar, is_timedelta64_dtype, ) -from pandas.core.dtypes.generic import ABCExtensionArray, ABCSeries +from pandas.core.dtypes.generic import ABCSeries from pandas.core.construction import array @@ -93,9 +95,7 @@ def should_series_dispatch(left, right, op): return False -def dispatch_to_extension_op( - op, left: Union[ABCExtensionArray, np.ndarray], right: Any, -): +def dispatch_to_extension_op(op, left: ArrayLike, right: Any): """ Assume that left or right is a Series backed by an ExtensionArray, apply the operator defined by op. diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index c1e12887b0150..5229bfca7a84f 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -2216,7 +2216,7 @@ def __eq__(self, other: Any) -> bool: for a in ["name", "cname", "dtype", "pos"] ) - def set_data(self, data: Union[np.ndarray, ABCExtensionArray]): + def set_data(self, data: ArrayLike): assert data is not None assert self.dtype is None @@ -2231,7 +2231,7 @@ def take_data(self): return self.data @classmethod - def _get_atom(cls, values: Union[np.ndarray, ABCExtensionArray]) -> "Col": + def _get_atom(cls, values: ArrayLike) -> "Col": """ Get an appropriately typed and shaped pytables.Col object for values. """ @@ -4977,7 +4977,7 @@ def _dtype_to_kind(dtype_str: str) -> str: return kind -def _get_data_and_dtype_name(data: Union[np.ndarray, ABCExtensionArray]): +def _get_data_and_dtype_name(data: ArrayLike): """ Convert the passed data into a storable form and a dtype string. """ From 60567d28e129eabc292750b2b9f2dc1197095b8c Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 2 Feb 2020 16:01:48 +0000 Subject: [PATCH 02/15] mypy fixup --- pandas/core/arrays/base.py | 8 ++++++++ pandas/core/base.py | 11 +++++------ pandas/core/dtypes/base.py | 7 +++++++ pandas/core/indexes/base.py | 2 +- pandas/io/pytables.py | 3 +++ 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 283d13c0204d7..32b09543b9b9c 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -70,6 +70,7 @@ class ExtensionArray: nbytes ndim shape + size Methods ------- @@ -344,6 +345,13 @@ def __len__(self) -> int: """ raise AbstractMethodError(self) + @property + def size(self) -> int: + """ + The number of elements in this array. + """ + raise AbstractMethodError(self) + def __iter__(self): """ Iterate over elements of the array. diff --git a/pandas/core/base.py b/pandas/core/base.py index c59193d826d7e..bde37dc3a68f7 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -4,7 +4,7 @@ import builtins import textwrap -from typing import Dict, FrozenSet, List, Optional +from typing import Dict, FrozenSet, Generic, List, Optional import numpy as np @@ -594,7 +594,7 @@ def _shallow_copy(self, obj, **kwargs): return self._constructor(obj, **kwargs) -class IndexOpsMixin: +class IndexOpsMixin(Generic[ArrayLike]): """ Common ops mixin to support a unified interface / docs for Series / Index """ @@ -1154,10 +1154,9 @@ def _map_values(self, mapper, na_action=None): values = self.astype(object) values = getattr(values, "values", values) if na_action == "ignore": - - def map_f(values, f): - return lib.map_infer_mask(values, f, isna(values).view(np.uint8)) - + map_f = lambda values, f: lib.map_infer_mask( + values, f, isna(values).view(np.uint8) + ) else: map_f = lib.map_infer diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index eddf46ee362d6..3b12a390b8ca7 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -145,6 +145,13 @@ def type(self) -> Type: """ raise AbstractMethodError(self) + @property + def itemsize(self) -> int: + """ + The element size of this data-type object. + """ + raise AbstractMethodError(self) + @property def kind(self) -> str: """ diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2dc363ec9e6ab..a58325e028c28 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -175,7 +175,7 @@ def _new_Index(cls, d): return cls.__new__(cls, **d) -class Index(IndexOpsMixin, PandasObject): +class Index(IndexOpsMixin[ArrayLike], PandasObject): """ Immutable ndarray implementing an ordered, sliceable set. The basic object storing axis labels for all pandas objects. diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 5229bfca7a84f..8de970a63d32b 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -18,6 +18,7 @@ Tuple, Type, Union, + cast, ) import warnings @@ -2245,6 +2246,7 @@ def _get_atom(cls, values: ArrayLike) -> "Col": shape = (1, values.size) if is_categorical_dtype(dtype): + values = cast(Categorical, values) codes = values.codes atom = cls.get_atom_data(shape, kind=codes.dtype.name) elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype): @@ -4982,6 +4984,7 @@ def _get_data_and_dtype_name(data: ArrayLike): Convert the passed data into a storable form and a dtype string. """ if is_categorical_dtype(data.dtype): + data = cast(Categorical, data) data = data.codes # For datetime64tz we need to drop the TZ in tests TODO: why? From f01f4b1de576229cb6e0a6739d0bc642132e9ec3 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Thu, 19 Mar 2020 18:07:20 +0000 Subject: [PATCH 03/15] mypy/isort fixup --- pandas/core/arrays/base.py | 11 ++--------- pandas/core/base.py | 1 - pandas/io/pytables.py | 12 +----------- 3 files changed, 3 insertions(+), 21 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index edeb71d5b264a..2ccd0ad104a95 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -346,13 +346,6 @@ def __len__(self) -> int: """ raise AbstractMethodError(self) - @property - def size(self) -> int: - """ - The number of elements in this array. - """ - raise AbstractMethodError(self) - def __iter__(self): """ Iterate over elements of the array. @@ -469,7 +462,7 @@ def isna(self) -> ArrayLike: Returns ------- - na_values : ArrayLike + na_values : np.ndarray or ExtensionArray In most cases, this should return a NumPy ndarray. For exceptional cases like ``SparseArray``, where returning an ndarray would be expensive, an ExtensionArray may be @@ -1178,7 +1171,7 @@ def _create_method(cls, op, coerce_to_dtype=True): Returns ------- - Callable[[Any, Any], ArrayLike] + Callable[[Any, Any], Union[ndarray, ExtensionArray]] A method that can be bound to a class. When used, the method receives the two arguments, one of which is the instance of this class, and should return an ExtensionArray or an ndarray. diff --git a/pandas/core/base.py b/pandas/core/base.py index 976c39c329ef7..336a6bd52d929 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -36,7 +36,6 @@ from pandas.core.construction import create_series_with_explicit_dtype import pandas.core.nanops as nanops - _shared_docs: Dict[str, str] = dict() _indexops_doc_kwargs = dict( klass="IndexOpsMixin", diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 5635680dfc460..fbccb6e339f80 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -8,17 +8,7 @@ import itertools import os import re -from typing import ( - TYPE_CHECKING, - Any, - Dict, - List, - Optional, - Tuple, - Type, - Union, - cast, -) +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union, cast import warnings import numpy as np From 5f5231d6e5ff122dacaa79a1688ddd4b0f6b5782 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 10 Apr 2020 12:27:31 +0100 Subject: [PATCH 04/15] mypy fixup --- pandas/core/base.py | 5 ++++- pandas/core/indexes/base.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 52a5fa8ac2d1f..2a9b11847f000 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1142,7 +1142,10 @@ def _map_values(self, mapper, na_action=None): values = self._values if na_action is not None: raise NotImplementedError - map_f = lambda values, f: values.map(f) + + def map_f(values, f): + return values.map(f) + else: values = self.astype(object) values = getattr(values, "values", values) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 16edcfba511ab..303336c507ee9 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2,7 +2,7 @@ from datetime import datetime import operator from textwrap import dedent -from typing import TYPE_CHECKING, Any, FrozenSet, Hashable +from typing import TYPE_CHECKING, Any, FrozenSet, Hashable, Union import warnings import numpy as np From 42a178f1166a4dce4c7d554278c2cd16613ce90f Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 10 Apr 2020 12:45:09 +0100 Subject: [PATCH 05/15] revert add itemsize to EA interface --- pandas/core/dtypes/base.py | 7 ------- pandas/io/pytables.py | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index d15a6e0d14844..a4f0ccc2016c0 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -150,13 +150,6 @@ def type(self) -> Type: """ raise AbstractMethodError(self) - @property - def itemsize(self) -> int: - """ - The element size of this data-type object. - """ - raise AbstractMethodError(self) - @property def kind(self) -> str: """ diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 0f7a62c8763e8..c8d5f7b8d353b 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -2212,7 +2212,7 @@ def take_data(self): return self.data @classmethod - def _get_atom(cls, values: ArrayLike) -> "Col": + def _get_atom(cls, values: np.ndarray) -> "Col": """ Get an appropriately typed and shaped pytables.Col object for values. """ From a6cc70c900611aea8c77a300aa996846f773ab07 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 10 Apr 2020 12:57:23 +0100 Subject: [PATCH 06/15] revert changes to pytables --- pandas/io/pytables.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index c8d5f7b8d353b..a22251b29da54 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -8,7 +8,7 @@ import itertools import os import re -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union, cast +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union import warnings import numpy as np @@ -2212,7 +2212,7 @@ def take_data(self): return self.data @classmethod - def _get_atom(cls, values: np.ndarray) -> "Col": + def _get_atom(cls, values: Union[np.ndarray, ABCExtensionArray]) -> "Col": """ Get an appropriately typed and shaped pytables.Col object for values. """ @@ -2225,7 +2225,6 @@ def _get_atom(cls, values: np.ndarray) -> "Col": shape = (1, values.size) if is_categorical_dtype(dtype): - values = cast(Categorical, values) codes = values.codes atom = cls.get_atom_data(shape, kind=codes.dtype.name) elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype): From f7fca6ee028ae8c3c87582b9fc82e385c0bd2ee5 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 11 Apr 2020 10:19:03 +0100 Subject: [PATCH 07/15] unsed classes following merge upsteam --- pandas/core/ops/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index b0ec2996c8e2f..433ec7f06adb1 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -56,7 +56,7 @@ ) if TYPE_CHECKING: - from pandas import DataFrame, Index, Series # noqa: F401 + from pandas import DataFrame # noqa: F401 # ----------------------------------------------------------------------------- # constants From c987bc22184914b05d7596461bc7fe96b1c195fb Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 11 Apr 2020 10:20:05 +0100 Subject: [PATCH 08/15] whitespace --- pandas/core/ops/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 433ec7f06adb1..c14c4a311d66c 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -56,7 +56,7 @@ ) if TYPE_CHECKING: - from pandas import DataFrame # noqa: F401 + from pandas import DataFrame # noqa:F401 # ----------------------------------------------------------------------------- # constants From 384ac323c47019314be75518b80caed9b50497f2 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 11 Apr 2020 10:21:44 +0100 Subject: [PATCH 09/15] troubleshoot test failure --- pandas/core/base.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 2a9b11847f000..52a5fa8ac2d1f 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1142,10 +1142,7 @@ def _map_values(self, mapper, na_action=None): values = self._values if na_action is not None: raise NotImplementedError - - def map_f(values, f): - return values.map(f) - + map_f = lambda values, f: values.map(f) else: values = self.astype(object) values = getattr(values, "values", values) From 6984eaf76bbebb7cd2bc5d3007b9ea22d568c263 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 11 Apr 2020 10:54:33 +0100 Subject: [PATCH 10/15] restore replace lambda --- pandas/core/base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 52a5fa8ac2d1f..2a9b11847f000 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1142,7 +1142,10 @@ def _map_values(self, mapper, na_action=None): values = self._values if na_action is not None: raise NotImplementedError - map_f = lambda values, f: values.map(f) + + def map_f(values, f): + return values.map(f) + else: values = self.astype(object) values = getattr(values, "values", values) From c0772a29d4a463eb6c4fb63142062476fb94a2d2 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 11 Apr 2020 11:05:27 +0100 Subject: [PATCH 11/15] AssertionError -> TypeError --- pandas/core/indexes/period.py | 5 ++++- pandas/tests/indexes/period/test_constructors.py | 14 ++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 1f565828ec7a5..456d4c917f28f 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -230,7 +230,10 @@ def _simple_new(cls, values: PeriodArray, name: Label = None): Values that can be converted to a PeriodArray without inference or coercion. """ - assert isinstance(values, PeriodArray), type(values) + if not isinstance(values, PeriodArray): + raise TypeError( + f"_simple_new expects PeriodArray, got {type(values).__name__}" + ) result = object.__new__(cls) result._data = values diff --git a/pandas/tests/indexes/period/test_constructors.py b/pandas/tests/indexes/period/test_constructors.py index 4ec7ef64e2272..df41fcf9d951a 100644 --- a/pandas/tests/indexes/period/test_constructors.py +++ b/pandas/tests/indexes/period/test_constructors.py @@ -339,15 +339,21 @@ def test_constructor_simple_new(self): def test_constructor_simple_new_empty(self): # GH13079 idx = PeriodIndex([], freq="M", name="p") - with pytest.raises(AssertionError, match=""): + with pytest.raises( + TypeError, match="_simple_new expects PeriodArray, got PeriodIndex" + ): idx._simple_new(idx, name="p") result = idx._simple_new(idx._data, name="p") tm.assert_index_equal(result, idx) - @pytest.mark.parametrize("floats", [[1.1, 2.1], np.array([1.1, 2.1])]) - def test_constructor_floats(self, floats): - with pytest.raises(AssertionError, match=" Date: Sat, 11 Apr 2020 11:45:46 +0100 Subject: [PATCH 12/15] more test changes --- pandas/tests/indexes/period/test_constructors.py | 4 +++- pandas/tests/indexes/period/test_period.py | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexes/period/test_constructors.py b/pandas/tests/indexes/period/test_constructors.py index df41fcf9d951a..61236669cd7ee 100644 --- a/pandas/tests/indexes/period/test_constructors.py +++ b/pandas/tests/indexes/period/test_constructors.py @@ -321,7 +321,9 @@ def test_constructor_mixed(self): def test_constructor_simple_new(self): idx = period_range("2007-01", name="p", periods=2, freq="M") - with pytest.raises(AssertionError, match=""): + with pytest.raises( + TypeError, match="_simple_new expects PeriodArray, got PeriodIndex" + ): idx._simple_new(idx, name="p") result = idx._simple_new(idx._data, name="p") diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index 0ce10fb8779a1..2fef28cc34582 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -92,12 +92,16 @@ def test_shallow_copy_empty(self): def test_shallow_copy_disallow_i8(self): # GH-24391 pi = period_range("2018-01-01", periods=3, freq="2D") - with pytest.raises(AssertionError, match="ndarray"): + with pytest.raises( + TypeError, match="_simple_new expects PeriodArray, got ndarray" + ): pi._shallow_copy(pi.asi8) def test_shallow_copy_requires_disallow_period_index(self): pi = period_range("2018-01-01", periods=3, freq="2D") - with pytest.raises(AssertionError, match="PeriodIndex"): + with pytest.raises( + TypeError, match="_simple_new expects PeriodArray, got PeriodIndex" + ): pi._shallow_copy(pi) def test_view_asi8(self): From 0b66334ebeaf3954b013f35f19b735ef6bde1e6b Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 22 Apr 2020 13:10:36 +0100 Subject: [PATCH 13/15] revert changes to 'debugging' asserts that were tested --- pandas/core/indexes/period.py | 5 +---- .../tests/indexes/period/test_constructors.py | 18 +++++------------- pandas/tests/indexes/period/test_period.py | 8 ++------ 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 70f35c2f06568..56cb9e29761a6 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -228,10 +228,7 @@ def _simple_new(cls, values: PeriodArray, name: Label = None): Values that can be converted to a PeriodArray without inference or coercion. """ - if not isinstance(values, PeriodArray): - raise TypeError( - f"_simple_new expects PeriodArray, got {type(values).__name__}" - ) + assert isinstance(values, PeriodArray), type(values) result = object.__new__(cls) result._data = values diff --git a/pandas/tests/indexes/period/test_constructors.py b/pandas/tests/indexes/period/test_constructors.py index 61236669cd7ee..4ec7ef64e2272 100644 --- a/pandas/tests/indexes/period/test_constructors.py +++ b/pandas/tests/indexes/period/test_constructors.py @@ -321,9 +321,7 @@ def test_constructor_mixed(self): def test_constructor_simple_new(self): idx = period_range("2007-01", name="p", periods=2, freq="M") - with pytest.raises( - TypeError, match="_simple_new expects PeriodArray, got PeriodIndex" - ): + with pytest.raises(AssertionError, match=""): idx._simple_new(idx, name="p") result = idx._simple_new(idx._data, name="p") @@ -341,21 +339,15 @@ def test_constructor_simple_new(self): def test_constructor_simple_new_empty(self): # GH13079 idx = PeriodIndex([], freq="M", name="p") - with pytest.raises( - TypeError, match="_simple_new expects PeriodArray, got PeriodIndex" - ): + with pytest.raises(AssertionError, match=""): idx._simple_new(idx, name="p") result = idx._simple_new(idx._data, name="p") tm.assert_index_equal(result, idx) - @pytest.mark.parametrize( - "floats,box", [([1.1, 2.1], "list"), (np.array([1.1, 2.1]), "ndarray")] - ) - def test_constructor_floats(self, floats, box): - with pytest.raises( - TypeError, match=f"_simple_new expects PeriodArray, got {box}" - ): + @pytest.mark.parametrize("floats", [[1.1, 2.1], np.array([1.1, 2.1])]) + def test_constructor_floats(self, floats): + with pytest.raises(AssertionError, match=" Date: Wed, 22 Apr 2020 14:35:47 +0100 Subject: [PATCH 14/15] fixup tests --- pandas/tests/indexes/period/test_constructors.py | 4 ++-- pandas/tests/series/methods/test_argsort.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexes/period/test_constructors.py b/pandas/tests/indexes/period/test_constructors.py index 4ec7ef64e2272..56383826db164 100644 --- a/pandas/tests/indexes/period/test_constructors.py +++ b/pandas/tests/indexes/period/test_constructors.py @@ -321,7 +321,7 @@ def test_constructor_mixed(self): def test_constructor_simple_new(self): idx = period_range("2007-01", name="p", periods=2, freq="M") - with pytest.raises(AssertionError, match=""): + with pytest.raises(AssertionError, match="PeriodIndex"): idx._simple_new(idx, name="p") result = idx._simple_new(idx._data, name="p") @@ -339,7 +339,7 @@ def test_constructor_simple_new(self): def test_constructor_simple_new_empty(self): # GH13079 idx = PeriodIndex([], freq="M", name="p") - with pytest.raises(AssertionError, match=""): + with pytest.raises(AssertionError, match="PeriodIndex"): idx._simple_new(idx, name="p") result = idx._simple_new(idx._data, name="p") diff --git a/pandas/tests/series/methods/test_argsort.py b/pandas/tests/series/methods/test_argsort.py index 4353eb4c8cd64..53e5dbd87e508 100644 --- a/pandas/tests/series/methods/test_argsort.py +++ b/pandas/tests/series/methods/test_argsort.py @@ -53,7 +53,7 @@ def test_argsort_stable(self): tm.assert_series_equal(qindexer.astype(np.intp), Series(qexpected)) msg = ( r"ndarray Expected type , " - r"found instead" + r"found ()? instead" ) with pytest.raises(AssertionError, match=msg): tm.assert_numpy_array_equal(qindexer, mindexer) From 3d6f7a6a2ac2c3d0f53958507315ea5297381ee8 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Mon, 27 Apr 2020 10:16:08 +0100 Subject: [PATCH 15/15] fix doc failures --- pandas/core/arrays/base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index a9bc10ff5ed3c..4435d8b7c88ee 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -49,7 +49,6 @@ class ExtensionArray: nbytes ndim shape - size Methods -------