Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas-stubs/core/arrays/sparse/accessor.pyi
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
from pandas import Series
from pandas.core.accessor import PandasDelegate

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

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): ...

Expand Down
7 changes: 5 additions & 2 deletions pandas-stubs/core/indexes/base.pyi
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.integer],
copy: bool = ...,
name=...,
tupleize_cols: bool = ...,
Expand All@@ -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 = ...,
Expand Down
79 changes: 79 additions & 0 deletions tests/test_indexes.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -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,
)
Expand DownExpand Up@@ -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)
32 changes: 16 additions & 16 deletions tests/test_timefuncs.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -452,29 +452,29 @@ 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_)
check(assert_type(i0.is_quarter_end, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
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)
Expand Down