From 933ec41ce754912539ac26f7bbcb3ba086303671 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Tue, 4 Apr 2023 23:06:15 -0400 Subject: [PATCH 1/2] first part of v2.0 changes --- pandas-stubs/core/arrays/sparse/accessor.pyi | 3 +- pandas-stubs/core/indexes/base.pyi | 7 +++-- tests/test_timefuncs.py | 32 ++++++++++---------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/pandas-stubs/core/arrays/sparse/accessor.pyi b/pandas-stubs/core/arrays/sparse/accessor.pyi index 4c091d1d8..85f1f9a57 100644 --- a/pandas-stubs/core/arrays/sparse/accessor.pyi +++ b/pandas-stubs/core/arrays/sparse/accessor.pyi @@ -1,3 +1,4 @@ +from pandas import Series from pandas.core.accessor import PandasDelegate class BaseAccessor: @@ -5,7 +6,7 @@ class BaseAccessor: class SparseAccessor(BaseAccessor, PandasDelegate): @classmethod - def from_coo(cls, A, dense_index: bool = ...): ... + def from_coo(cls, A, dense_index: bool = ...) -> Series: ... def to_coo(self, row_levels=..., column_levels=..., sort_labels: bool = ...): ... def to_dense(self): ... diff --git a/pandas-stubs/core/indexes/base.pyi b/pandas-stubs/core/indexes/base.pyi index 0bee43b16..8d843c567 100644 --- a/pandas-stubs/core/indexes/base.pyi +++ b/pandas-stubs/core/indexes/base.pyi @@ -73,7 +73,7 @@ class Index(IndexOpsMixin, PandasObject): def __new__( cls, data: Iterable, - dtype: Literal["int"] | type_t[int] | type_t[np.int_], + dtype: Literal["int"] | type_t[int] | type_t[np.int_] | type_t[np.uint], copy: bool = ..., name=..., tupleize_cols: bool = ..., @@ -83,7 +83,10 @@ class Index(IndexOpsMixin, PandasObject): def __new__( cls, data: Iterable, - dtype: Literal["float"] | type_t[float] | type_t[np.float_], + dtype: Literal["float"] + | type_t[float] + | type_t[np.float32] + | type_t[np.float64], copy: bool = ..., name=..., tupleize_cols: bool = ..., diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index a171cb29b..ec5d165e1 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -452,20 +452,20 @@ def test_datetimeindex_accessors() -> None: check(assert_type(i0.date, np.ndarray), np.ndarray, dt.date) check(assert_type(i0.time, np.ndarray), np.ndarray, dt.time) check(assert_type(i0.timetz, np.ndarray), np.ndarray, dt.time) - check(assert_type(i0.year, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.month, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.day, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.hour, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.minute, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.second, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.microsecond, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.nanosecond, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.dayofweek, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.day_of_week, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.weekday, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.dayofyear, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.day_of_year, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.quarter, "_IntIndexType"), _IntIndexType, np.integer) + check(assert_type(i0.year, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.month, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.day, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.hour, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.minute, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.second, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.microsecond, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.nanosecond, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.dayofweek, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.day_of_week, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.weekday, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.dayofyear, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.day_of_year, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.quarter, "_IntIndexType"), _IntIndexType, np.int32) check(assert_type(i0.is_month_start, npt.NDArray[np.bool_]), np.ndarray, np.bool_) check(assert_type(i0.is_month_end, npt.NDArray[np.bool_]), np.ndarray, np.bool_) check(assert_type(i0.is_quarter_start, npt.NDArray[np.bool_]), np.ndarray, np.bool_) @@ -473,8 +473,8 @@ def test_datetimeindex_accessors() -> None: check(assert_type(i0.is_year_start, npt.NDArray[np.bool_]), np.ndarray, np.bool_) check(assert_type(i0.is_year_end, npt.NDArray[np.bool_]), np.ndarray, np.bool_) check(assert_type(i0.is_leap_year, npt.NDArray[np.bool_]), np.ndarray, np.bool_) - check(assert_type(i0.daysinmonth, "_IntIndexType"), _IntIndexType, np.integer) - check(assert_type(i0.days_in_month, "_IntIndexType"), _IntIndexType, np.integer) + check(assert_type(i0.daysinmonth, "_IntIndexType"), _IntIndexType, np.int32) + check(assert_type(i0.days_in_month, "_IntIndexType"), _IntIndexType, np.int32) check(assert_type(i0.tz, Optional[dt.tzinfo]), type(None)) check(assert_type(i0.freq, Optional[BaseOffset]), BaseOffset) check(assert_type(i0.isocalendar(), pd.DataFrame), pd.DataFrame) From bd3418dfb5b73b9d9db2445a325fdd3e7b7eb32e Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Thu, 6 Apr 2023 18:48:45 -0400 Subject: [PATCH 2/2] combine np.int types. Add tests --- pandas-stubs/core/indexes/base.pyi | 2 +- tests/test_indexes.py | 79 ++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/pandas-stubs/core/indexes/base.pyi b/pandas-stubs/core/indexes/base.pyi index 8d843c567..fa27a5235 100644 --- a/pandas-stubs/core/indexes/base.pyi +++ b/pandas-stubs/core/indexes/base.pyi @@ -73,7 +73,7 @@ class Index(IndexOpsMixin, PandasObject): def __new__( cls, data: Iterable, - dtype: Literal["int"] | type_t[int] | type_t[np.int_] | type_t[np.uint], + dtype: Literal["int"] | type_t[int] | type_t[np.integer], copy: bool = ..., name=..., tupleize_cols: bool = ..., diff --git a/tests/test_indexes.py b/tests/test_indexes.py index d3d0a9c00..2c337a35d 100644 --- a/tests/test_indexes.py +++ b/tests/test_indexes.py @@ -25,11 +25,13 @@ if TYPE_CHECKING: from pandas.core.indexes.base import ( + _ComplexIndexType, _FloatIndexType, _IntIndexType, ) else: from pandas.core.indexes.base import ( + Index as _ComplexIndexType, Index as _FloatIndexType, Index as _IntIndexType, ) @@ -855,3 +857,80 @@ def test_multiindex_dtypes(): # GH-597 mi = pd.MultiIndex.from_tuples([(1, 2.0), (2, 3.0)], names=["foo", "bar"]) check(assert_type(mi.dtypes, "pd.Series[Dtype]"), pd.Series) + + +def test_index_constructors(): + # See if we can pick up the different index types in 2.0 + # Eventually should be using a generic index + ilist = [1, 2, 3] + check( + assert_type(pd.Index(ilist, dtype="int"), _IntIndexType), pd.Index, np.integer + ) + check(assert_type(pd.Index(ilist, dtype=int), _IntIndexType), pd.Index, np.integer) + check(assert_type(pd.Index(ilist, dtype=np.int8), _IntIndexType), pd.Index, np.int8) + check( + assert_type(pd.Index(ilist, dtype=np.int16), _IntIndexType), pd.Index, np.int16 + ) + check( + assert_type(pd.Index(ilist, dtype=np.int32), _IntIndexType), pd.Index, np.int32 + ) + check( + assert_type(pd.Index(ilist, dtype=np.int64), _IntIndexType), pd.Index, np.int64 + ) + check( + assert_type(pd.Index(ilist, dtype=np.uint8), _IntIndexType), pd.Index, np.uint8 + ) + check( + assert_type(pd.Index(ilist, dtype=np.uint16), _IntIndexType), + pd.Index, + np.uint16, + ) + check( + assert_type(pd.Index(ilist, dtype=np.uint32), _IntIndexType), + pd.Index, + np.uint32, + ) + check( + assert_type(pd.Index(ilist, dtype=np.uint64), _IntIndexType), + pd.Index, + np.uint64, + ) + + flist = [1.1, 2.2, 3.3] + check( + assert_type(pd.Index(flist, dtype="float"), _FloatIndexType), + pd.Index, + np.float64, + ) + check( + assert_type(pd.Index(flist, dtype=float), _FloatIndexType), pd.Index, np.float64 + ) + check( + assert_type(pd.Index(flist, dtype=np.float32), _FloatIndexType), + pd.Index, + np.float32, + ) + check( + assert_type(pd.Index(flist, dtype=np.float64), _FloatIndexType), + pd.Index, + np.float64, + ) + + clist = [1 + 1j, 2 + 2j, 3 + 4j] + check( + assert_type(pd.Index(clist, dtype="complex"), _ComplexIndexType), + pd.Index, + complex, + ) + check( + assert_type(pd.Index(clist, dtype=complex), _ComplexIndexType), + pd.Index, + complex, + ) + + if TYPE_CHECKING_INVALID_USAGE: + # This should be detected by the type checker, but for it to work, + # we need to change the last overload of __new__ in core/indexes/base.pyi + # to specify all the possible dtype options. For right now, we will leave the + # test here as a reminder that we would like this to be seen as incorrect usage. + pd.Index(flist, dtype=np.float16)