Skip to content

Commit 5fed9c2

Browse files
authored
first part of v2.0 changes (#632)
* first part of v2.0 changes * combine np.int types. Add tests
1 parent 636dac5 commit 5fed9c2

File tree

4 files changed

+102
-19
lines changed

4 files changed

+102
-19
lines changed

pandas-stubs/core/arrays/sparse/accessor.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
from pandas import Series
12
from pandas.core.accessor import PandasDelegate
23

34
class BaseAccessor:
45
def __init__(self, data=...) -> None: ...
56

67
class SparseAccessor(BaseAccessor, PandasDelegate):
78
@classmethod
8-
def from_coo(cls, A, dense_index: bool = ...): ...
9+
def from_coo(cls, A, dense_index: bool = ...) -> Series: ...
910
def to_coo(self, row_levels=..., column_levels=..., sort_labels: bool = ...): ...
1011
def to_dense(self): ...
1112

pandas-stubs/core/indexes/base.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Index(IndexOpsMixin, PandasObject):
7373
def __new__(
7474
cls,
7575
data: Iterable,
76-
dtype: Literal["int"] | type_t[int] | type_t[np.int_],
76+
dtype: Literal["int"] | type_t[int] | type_t[np.integer],
7777
copy: bool = ...,
7878
name=...,
7979
tupleize_cols: bool = ...,
@@ -83,7 +83,10 @@ class Index(IndexOpsMixin, PandasObject):
8383
def __new__(
8484
cls,
8585
data: Iterable,
86-
dtype: Literal["float"] | type_t[float] | type_t[np.float_],
86+
dtype: Literal["float"]
87+
| type_t[float]
88+
| type_t[np.float32]
89+
| type_t[np.float64],
8790
copy: bool = ...,
8891
name=...,
8992
tupleize_cols: bool = ...,

tests/test_indexes.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525

2626
if TYPE_CHECKING:
2727
from pandas.core.indexes.base import (
28+
_ComplexIndexType,
2829
_FloatIndexType,
2930
_IntIndexType,
3031
)
3132
else:
3233
from pandas.core.indexes.base import (
34+
Index as _ComplexIndexType,
3335
Index as _FloatIndexType,
3436
Index as _IntIndexType,
3537
)
@@ -855,3 +857,80 @@ def test_multiindex_dtypes():
855857
# GH-597
856858
mi = pd.MultiIndex.from_tuples([(1, 2.0), (2, 3.0)], names=["foo", "bar"])
857859
check(assert_type(mi.dtypes, "pd.Series[Dtype]"), pd.Series)
860+
861+
862+
def test_index_constructors():
863+
# See if we can pick up the different index types in 2.0
864+
# Eventually should be using a generic index
865+
ilist = [1, 2, 3]
866+
check(
867+
assert_type(pd.Index(ilist, dtype="int"), _IntIndexType), pd.Index, np.integer
868+
)
869+
check(assert_type(pd.Index(ilist, dtype=int), _IntIndexType), pd.Index, np.integer)
870+
check(assert_type(pd.Index(ilist, dtype=np.int8), _IntIndexType), pd.Index, np.int8)
871+
check(
872+
assert_type(pd.Index(ilist, dtype=np.int16), _IntIndexType), pd.Index, np.int16
873+
)
874+
check(
875+
assert_type(pd.Index(ilist, dtype=np.int32), _IntIndexType), pd.Index, np.int32
876+
)
877+
check(
878+
assert_type(pd.Index(ilist, dtype=np.int64), _IntIndexType), pd.Index, np.int64
879+
)
880+
check(
881+
assert_type(pd.Index(ilist, dtype=np.uint8), _IntIndexType), pd.Index, np.uint8
882+
)
883+
check(
884+
assert_type(pd.Index(ilist, dtype=np.uint16), _IntIndexType),
885+
pd.Index,
886+
np.uint16,
887+
)
888+
check(
889+
assert_type(pd.Index(ilist, dtype=np.uint32), _IntIndexType),
890+
pd.Index,
891+
np.uint32,
892+
)
893+
check(
894+
assert_type(pd.Index(ilist, dtype=np.uint64), _IntIndexType),
895+
pd.Index,
896+
np.uint64,
897+
)
898+
899+
flist = [1.1, 2.2, 3.3]
900+
check(
901+
assert_type(pd.Index(flist, dtype="float"), _FloatIndexType),
902+
pd.Index,
903+
np.float64,
904+
)
905+
check(
906+
assert_type(pd.Index(flist, dtype=float), _FloatIndexType), pd.Index, np.float64
907+
)
908+
check(
909+
assert_type(pd.Index(flist, dtype=np.float32), _FloatIndexType),
910+
pd.Index,
911+
np.float32,
912+
)
913+
check(
914+
assert_type(pd.Index(flist, dtype=np.float64), _FloatIndexType),
915+
pd.Index,
916+
np.float64,
917+
)
918+
919+
clist = [1 + 1j, 2 + 2j, 3 + 4j]
920+
check(
921+
assert_type(pd.Index(clist, dtype="complex"), _ComplexIndexType),
922+
pd.Index,
923+
complex,
924+
)
925+
check(
926+
assert_type(pd.Index(clist, dtype=complex), _ComplexIndexType),
927+
pd.Index,
928+
complex,
929+
)
930+
931+
if TYPE_CHECKING_INVALID_USAGE:
932+
# This should be detected by the type checker, but for it to work,
933+
# we need to change the last overload of __new__ in core/indexes/base.pyi
934+
# to specify all the possible dtype options. For right now, we will leave the
935+
# test here as a reminder that we would like this to be seen as incorrect usage.
936+
pd.Index(flist, dtype=np.float16)

tests/test_timefuncs.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -452,29 +452,29 @@ def test_datetimeindex_accessors() -> None:
452452
check(assert_type(i0.date, np.ndarray), np.ndarray, dt.date)
453453
check(assert_type(i0.time, np.ndarray), np.ndarray, dt.time)
454454
check(assert_type(i0.timetz, np.ndarray), np.ndarray, dt.time)
455-
check(assert_type(i0.year, "_IntIndexType"), _IntIndexType, np.integer)
456-
check(assert_type(i0.month, "_IntIndexType"), _IntIndexType, np.integer)
457-
check(assert_type(i0.day, "_IntIndexType"), _IntIndexType, np.integer)
458-
check(assert_type(i0.hour, "_IntIndexType"), _IntIndexType, np.integer)
459-
check(assert_type(i0.minute, "_IntIndexType"), _IntIndexType, np.integer)
460-
check(assert_type(i0.second, "_IntIndexType"), _IntIndexType, np.integer)
461-
check(assert_type(i0.microsecond, "_IntIndexType"), _IntIndexType, np.integer)
462-
check(assert_type(i0.nanosecond, "_IntIndexType"), _IntIndexType, np.integer)
463-
check(assert_type(i0.dayofweek, "_IntIndexType"), _IntIndexType, np.integer)
464-
check(assert_type(i0.day_of_week, "_IntIndexType"), _IntIndexType, np.integer)
465-
check(assert_type(i0.weekday, "_IntIndexType"), _IntIndexType, np.integer)
466-
check(assert_type(i0.dayofyear, "_IntIndexType"), _IntIndexType, np.integer)
467-
check(assert_type(i0.day_of_year, "_IntIndexType"), _IntIndexType, np.integer)
468-
check(assert_type(i0.quarter, "_IntIndexType"), _IntIndexType, np.integer)
455+
check(assert_type(i0.year, "_IntIndexType"), _IntIndexType, np.int32)
456+
check(assert_type(i0.month, "_IntIndexType"), _IntIndexType, np.int32)
457+
check(assert_type(i0.day, "_IntIndexType"), _IntIndexType, np.int32)
458+
check(assert_type(i0.hour, "_IntIndexType"), _IntIndexType, np.int32)
459+
check(assert_type(i0.minute, "_IntIndexType"), _IntIndexType, np.int32)
460+
check(assert_type(i0.second, "_IntIndexType"), _IntIndexType, np.int32)
461+
check(assert_type(i0.microsecond, "_IntIndexType"), _IntIndexType, np.int32)
462+
check(assert_type(i0.nanosecond, "_IntIndexType"), _IntIndexType, np.int32)
463+
check(assert_type(i0.dayofweek, "_IntIndexType"), _IntIndexType, np.int32)
464+
check(assert_type(i0.day_of_week, "_IntIndexType"), _IntIndexType, np.int32)
465+
check(assert_type(i0.weekday, "_IntIndexType"), _IntIndexType, np.int32)
466+
check(assert_type(i0.dayofyear, "_IntIndexType"), _IntIndexType, np.int32)
467+
check(assert_type(i0.day_of_year, "_IntIndexType"), _IntIndexType, np.int32)
468+
check(assert_type(i0.quarter, "_IntIndexType"), _IntIndexType, np.int32)
469469
check(assert_type(i0.is_month_start, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
470470
check(assert_type(i0.is_month_end, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
471471
check(assert_type(i0.is_quarter_start, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
472472
check(assert_type(i0.is_quarter_end, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
473473
check(assert_type(i0.is_year_start, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
474474
check(assert_type(i0.is_year_end, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
475475
check(assert_type(i0.is_leap_year, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
476-
check(assert_type(i0.daysinmonth, "_IntIndexType"), _IntIndexType, np.integer)
477-
check(assert_type(i0.days_in_month, "_IntIndexType"), _IntIndexType, np.integer)
476+
check(assert_type(i0.daysinmonth, "_IntIndexType"), _IntIndexType, np.int32)
477+
check(assert_type(i0.days_in_month, "_IntIndexType"), _IntIndexType, np.int32)
478478
check(assert_type(i0.tz, Optional[dt.tzinfo]), type(None))
479479
check(assert_type(i0.freq, Optional[BaseOffset]), BaseOffset)
480480
check(assert_type(i0.isocalendar(), pd.DataFrame), pd.DataFrame)

0 commit comments

Comments
 (0)