Skip to content

Commit bc18e49

Browse files
committed
asi8
1 parent a61b439 commit bc18e49

File tree

10 files changed

+115
-4
lines changed

10 files changed

+115
-4
lines changed

pandas-stubs/_libs/interval.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class _LengthDescriptor:
3838
def __get__(
3939
self, instance: Interval[_OrderableTimesT], owner: Any
4040
) -> Timedelta: ...
41+
@overload
4142
def __get__(self, instance: IntervalMixin, owner: Any) -> np_ndarray: ...
4243

4344
@type_check_only
@@ -48,6 +49,7 @@ class _MidDescriptor:
4849
def __get__(
4950
self, instance: Interval[_OrderableTimesT], owner: Any
5051
) -> _OrderableTimesT: ...
52+
@overload
5153
def __get__(self, instance: IntervalMixin, owner: Any) -> np_ndarray: ...
5254

5355
class IntervalMixin:

pandas-stubs/_typing.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,8 @@ ShapeT = TypeVar("ShapeT", bound=tuple[int, ...], default=tuple[Any, ...])
858858
np_ndarray: TypeAlias = np.ndarray[ShapeT, np.dtype[GenericT]]
859859
# Numpy arrays with known shape (Do not use as argument types, only as return types)
860860
np_1darray: TypeAlias = np.ndarray[tuple[int], np.dtype[GenericT]]
861+
np_1darray_int64: TypeAlias = np_1darray[np.int64]
862+
861863
np_2darray: TypeAlias = np.ndarray[tuple[int, int], np.dtype[GenericT]]
862864

863865
DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic])

pandas-stubs/core/arrays/datetimelike.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ from pandas._typing import (
2525
TimeNonexistent,
2626
TimeUnit,
2727
np_1darray,
28-
np_ndarray_int64,
28+
np_1darray_int64,
2929
)
3030

3131
DTScalarOrNaT: TypeAlias = DatetimeLikeScalar | NaTType
@@ -65,7 +65,7 @@ class DatetimeLikeArrayMixin(ExtensionOpsMixin, ExtensionArray):
6565
def ravel(self, *args: Any, **kwargs: Any): ...
6666
def __iter__(self): ...
6767
@property
68-
def asi8(self) -> np_ndarray_int64: ...
68+
def asi8(self) -> np_1darray_int64: ...
6969
@property
7070
def nbytes(self): ...
7171
def __array__(

pandas-stubs/core/construction.pyi

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,42 @@
11
from collections.abc import Sequence
2+
from datetime import (
3+
datetime,
4+
timedelta,
5+
)
26
from typing import overload
37

48
import numpy as np
59
from pandas.core.arrays.base import ExtensionArray
10+
from pandas.core.arrays.datetimes import DatetimeArray
611
from pandas.core.arrays.integer import IntegerArray
12+
from pandas.core.arrays.timedeltas import TimedeltaArray
713

814
from pandas._libs.missing import NAType
15+
from pandas._typing import (
16+
IntDtypeArg,
17+
TimedeltaDtypeArg,
18+
TimestampDtypeArg,
19+
UIntDtypeArg,
20+
)
921

1022
from pandas.core.dtypes.dtypes import ExtensionDtype
1123

24+
@overload
25+
def array( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
26+
data: Sequence[timedelta | NAType],
27+
dtype: TimedeltaDtypeArg | None = None,
28+
copy: bool = True,
29+
) -> TimedeltaArray: ...
30+
@overload
31+
def array( # type: ignore[overload-overlap]
32+
data: Sequence[datetime | NAType],
33+
dtype: TimestampDtypeArg | None = None,
34+
copy: bool = True,
35+
) -> DatetimeArray: ...
1236
@overload
1337
def array(
14-
data: Sequence[int] | Sequence[int | NAType],
15-
dtype: str | np.dtype | ExtensionDtype | None = None,
38+
data: Sequence[int | NAType],
39+
dtype: IntDtypeArg | UIntDtypeArg | None = None,
1640
copy: bool = True,
1741
) -> IntegerArray: ...
1842
@overload

pandas-stubs/core/indexes/datetimelike.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ from pandas._typing import (
1313
AxisIndex,
1414
GenericT_co,
1515
TimeUnit,
16+
np_1darray_int64,
1617
np_ndarray_complex,
1718
)
1819

@@ -62,3 +63,5 @@ class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin[S1, GenericT_co]):
6263
@property
6364
def unit(self) -> TimeUnit: ...
6465
def as_unit(self, unit: TimeUnit) -> Self: ...
66+
@property
67+
def asi8(self) -> np_1darray_int64: ...

tests/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
UIntDtypeArg as UIntDtypeArg,
4646
VoidDtypeArg as VoidDtypeArg,
4747
np_1darray as np_1darray,
48+
np_1darray_int64 as np_1darray_int64,
4849
np_2darray as np_2darray,
4950
np_ndarray as np_ndarray,
5051
np_ndarray_bool as np_ndarray_bool,
@@ -55,10 +56,12 @@
5556
_S = TypeVar("_S", bound=tuple[int, ...])
5657
# Separately define here so pytest works
5758
np_1darray: TypeAlias = np.ndarray[tuple[int], np.dtype[_G]]
59+
np_1darray_int64: TypeAlias = np_1darray[np.int64]
5860
np_2darray: TypeAlias = np.ndarray[tuple[int, int], np.dtype[_G]]
5961
np_ndarray: TypeAlias = np.ndarray[_S, np.dtype[_G]]
6062
np_ndarray_bool: TypeAlias = npt.NDArray[np.bool_]
6163
np_ndarray_int: TypeAlias = npt.NDArray[np.signedinteger]
64+
np_ndarray_int64: TypeAlias = npt.NDArray[np.int64]
6265

6366
TYPE_CHECKING_INVALID_USAGE: Final = TYPE_CHECKING
6467
WINDOWS = os.name == "nt" or "cygwin" in platform.system().lower()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from __future__ import annotations
2+
3+
from datetime import datetime
4+
5+
import numpy as np
6+
import pandas as pd
7+
from pandas.core.arrays.datetimes import DatetimeArray
8+
import pytest
9+
from typing_extensions import assert_type
10+
11+
from tests import (
12+
check,
13+
np_1darray_int64,
14+
)
15+
16+
17+
@pytest.fixture
18+
def arr() -> DatetimeArray:
19+
a = pd.array([datetime(2025, 11, 7), datetime(2025, 11, 8)])
20+
return check(assert_type(a, DatetimeArray), DatetimeArray, pd.Timestamp)
21+
22+
23+
def test_timedelta_index_properties(arr: DatetimeArray) -> None:
24+
check(assert_type(arr.asi8, np_1darray_int64), np_1darray_int64, np.integer)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from __future__ import annotations
2+
3+
from datetime import timedelta
4+
5+
import numpy as np
6+
import pandas as pd
7+
from pandas.core.arrays.timedeltas import TimedeltaArray
8+
import pytest
9+
from typing_extensions import assert_type
10+
11+
from tests import (
12+
check,
13+
np_1darray_int64,
14+
)
15+
16+
17+
@pytest.fixture
18+
def arr() -> TimedeltaArray:
19+
a = pd.array([timedelta(seconds=1), timedelta(seconds=2)])
20+
return check(assert_type(a, TimedeltaArray), TimedeltaArray, pd.Timedelta)
21+
22+
23+
def test_timedelta_index_properties(arr: TimedeltaArray) -> None:
24+
check(assert_type(arr.asi8, np_1darray_int64), np_1darray_int64, np.integer)

tests/indexes/test_datetime_index.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from tests import (
1212
check,
1313
np_1darray,
14+
np_1darray_int64,
1415
)
1516

1617

@@ -130,3 +131,8 @@ def test_datetimeindex_snap() -> None:
130131
),
131132
pd.DatetimeIndex,
132133
)
134+
135+
136+
def test_datetimeindex_properties() -> None:
137+
dti = pd.date_range("2023-01-01", "2023-02-01")
138+
check(assert_type(dti.asi8, np_1darray_int64), np_1darray_int64, np.integer)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from __future__ import annotations
2+
3+
import numpy as np
4+
import pandas as pd
5+
import pytest
6+
from typing_extensions import (
7+
assert_type,
8+
)
9+
10+
from tests import (
11+
check,
12+
np_1darray_int64,
13+
)
14+
15+
16+
@pytest.fixture
17+
def tdi() -> pd.TimedeltaIndex:
18+
idx = pd.timedelta_range("1 days", periods=3, freq="D")
19+
return check(assert_type(idx, pd.TimedeltaIndex), pd.TimedeltaIndex, pd.Timedelta)
20+
21+
22+
def test_timedelta_index_properties(tdi: pd.TimedeltaIndex) -> None:
23+
check(assert_type(tdi.asi8, np_1darray_int64), np_1darray_int64, np.integer)

0 commit comments

Comments
 (0)