Skip to content
Open
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
16 changes: 9 additions & 7 deletions pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from collections.abc import (
)
from datetime import (
datetime,
time,
timedelta,
tzinfo as _tzinfo,
)
Expand Down Expand Up @@ -32,6 +33,7 @@ from pandas._typing import (
IntervalClosedType,
TimeUnit,
TimeZones,
np_1darray,
np_ndarray_dt,
np_ndarray_td,
)
Expand All @@ -55,7 +57,6 @@ class DatetimeIndex(
copy: bool = ...,
name: Hashable = ...,
) -> Self: ...
def __reduce__(self): ...

# various ignores needed for mypy, as we do want to restrict what can be used in
# arithmetic for these types
Expand All @@ -78,18 +79,19 @@ class DatetimeIndex(
def to_series(
self, index: Index | None = None, name: Hashable | None = None
) -> Series[Timestamp]: ...
def snap(self, freq: str = ...): ...
def slice_indexer(self, start=..., end=..., step=...): ...
def snap(self, freq: Frequency = "S") -> Self: ...
@property
def inferred_type(self) -> str: ...
def indexer_at_time(self, time, asof: bool = ...): ...
def indexer_at_time(
self, time: str | time, asof: bool = False
) -> np_1darray[np.intp]: ...
def indexer_between_time(
self,
start_time: datetime | str,
end_time: datetime | str,
start_time: time | str,
end_time: time | str,
include_start: bool = True,
include_end: bool = True,
): ...
) -> np_1darray[np.intp]: ...
def to_julian_date(self) -> Index[float]: ...
def isocalendar(self) -> DataFrame: ...
@property
Expand Down
142 changes: 142 additions & 0 deletions tests/indexes/test_datetime_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
from __future__ import annotations

from datetime import time

import numpy as np
import pandas as pd
from typing_extensions import (
assert_type,
)

from tests import (
check,
np_1darray,
)


def test_index_relops() -> None:
# GH 265
data = check(
assert_type(
pd.date_range("2022-01-01", "2022-01-31", freq="D"), pd.DatetimeIndex
),
pd.DatetimeIndex,
)
x = pd.Timestamp("2022-01-17")
idx = check(
assert_type(pd.Index(data, name="date"), "pd.Index[pd.Timestamp]"), pd.Index
)
check(assert_type(data[x <= idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x < idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x >= idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x > idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[idx <= x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[idx < x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[idx >= x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[idx > x], pd.DatetimeIndex), pd.DatetimeIndex)

dt_idx = pd.DatetimeIndex(data, name="date")
check(assert_type(data[x <= dt_idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x < dt_idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x >= dt_idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x > dt_idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[dt_idx <= x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[dt_idx < x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[dt_idx >= x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[dt_idx > x], pd.DatetimeIndex), pd.DatetimeIndex)

ind = pd.Index([1, 2, 3])
check(assert_type(ind <= 2, np_1darray[np.bool]), np_1darray[np.bool])
check(assert_type(ind < 2, np_1darray[np.bool]), np_1darray[np.bool])
check(assert_type(ind >= 2, np_1darray[np.bool]), np_1darray[np.bool])
check(assert_type(ind > 2, np_1darray[np.bool]), np_1darray[np.bool])


def test_datetime_index_constructor() -> None:
check(assert_type(pd.DatetimeIndex(["2020"]), pd.DatetimeIndex), pd.DatetimeIndex)
check(
assert_type(pd.DatetimeIndex(["2020"], name="ts"), pd.DatetimeIndex),
pd.DatetimeIndex,
)
check(
assert_type(pd.DatetimeIndex(["2020"], freq="D"), pd.DatetimeIndex),
pd.DatetimeIndex,
)
check(
assert_type(pd.DatetimeIndex(["2020"], tz="Asia/Kathmandu"), pd.DatetimeIndex),
pd.DatetimeIndex,
)

# https://github.com/microsoft/python-type-stubs/issues/115
df = pd.DataFrame({"A": [1, 2, 3], "B": [5, 6, 7]})

check(
assert_type(
pd.DatetimeIndex(data=df["A"], tz=None, ambiguous="NaT", copy=True),
pd.DatetimeIndex,
),
pd.DatetimeIndex,
)


def test_intersection() -> None:
# GH 744
index = pd.DatetimeIndex(["2022-01-01"])
check(assert_type(index.intersection(index), pd.DatetimeIndex), pd.DatetimeIndex)
check(
assert_type(index.intersection([pd.Timestamp("1/1/2023")]), pd.DatetimeIndex),
pd.DatetimeIndex,
)


def test_datetime_index_max_min_reductions() -> None:
dtidx = pd.DatetimeIndex(["2020-01-01", "2020-01-02"])
check(assert_type(dtidx.argmax(), np.int64), np.int64)
check(assert_type(dtidx.argmin(), np.int64), np.int64)
check(assert_type(dtidx.max(), pd.Timestamp), pd.Timestamp)
check(assert_type(dtidx.min(), pd.Timestamp), pd.Timestamp)


def test_datetimeindex_shift() -> None:
ind = pd.date_range("2023-01-01", "2023-02-01")
check(assert_type(ind.shift(1), pd.DatetimeIndex), pd.DatetimeIndex)


def test_datetimeindex_indexer_at_time() -> None:
dti = pd.date_range("2023-01-01", "2023-02-01")
check(
assert_type(dti.indexer_at_time("10:00"), np_1darray[np.intp]),
np_1darray[np.intp],
)
check(
assert_type(dti.indexer_at_time(time(10)), np_1darray[np.intp]),
np_1darray[np.intp],
)


def test_datetimeindex_indexer_between_time() -> None:
dti = pd.date_range("2023-01-01", "2023-02-01")
check(
assert_type(
dti.indexer_between_time(
"10:00", time(11), include_start=False, include_end=True
),
np_1darray[np.intp],
),
np_1darray[np.intp],
)
check(
assert_type(dti.indexer_between_time(time(10), "11:00"), np_1darray[np.intp]),
np_1darray[np.intp],
)


def test_datetimeindex_snap() -> None:
dti = pd.date_range("2023-01-01", "2023-02-01")
check(
assert_type(
dti.snap("MS"),
pd.DatetimeIndex,
),
pd.DatetimeIndex,
)
81 changes: 0 additions & 81 deletions tests/indexes/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,37 +275,6 @@ def test_index_arithmetic() -> None:
check(assert_type(3 // idx, "pd.Index[float]"), pd.Index, np.float64)


def test_index_relops() -> None:
# GH 265
data = pd.date_range("2022-01-01", "2022-01-31", freq="D")
x = pd.Timestamp("2022-01-17")
idx = pd.Index(data, name="date")
check(assert_type(data[x <= idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x < idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x >= idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x > idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[idx < x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[idx >= x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[idx > x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[idx <= x], pd.DatetimeIndex), pd.DatetimeIndex)

dt_idx = pd.DatetimeIndex(data, name="date")
check(assert_type(data[x <= dt_idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x >= dt_idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x < dt_idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x > dt_idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[dt_idx <= x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[dt_idx >= x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[dt_idx < x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[dt_idx > x], pd.DatetimeIndex), pd.DatetimeIndex)

ind = pd.Index([1, 2, 3])
check(assert_type(ind <= 2, np_1darray[np.bool]), np_1darray[np.bool])
check(assert_type(ind >= 2, np_1darray[np.bool]), np_1darray[np.bool])
check(assert_type(ind < 2, np_1darray[np.bool]), np_1darray[np.bool])
check(assert_type(ind > 2, np_1darray[np.bool]), np_1darray[np.bool])


def test_range_index_union() -> None:
check(
assert_type(
Expand Down Expand Up @@ -1167,33 +1136,6 @@ def test_index_constructors() -> None:
pd.Index(flist, dtype=np.float16)


def test_datetime_index_constructor() -> None:
check(assert_type(pd.DatetimeIndex(["2020"]), pd.DatetimeIndex), pd.DatetimeIndex)
check(
assert_type(pd.DatetimeIndex(["2020"], name="ts"), pd.DatetimeIndex),
pd.DatetimeIndex,
)
check(
assert_type(pd.DatetimeIndex(["2020"], freq="D"), pd.DatetimeIndex),
pd.DatetimeIndex,
)
check(
assert_type(pd.DatetimeIndex(["2020"], tz="Asia/Kathmandu"), pd.DatetimeIndex),
pd.DatetimeIndex,
)

# https://github.com/microsoft/python-type-stubs/issues/115
df = pd.DataFrame({"A": [1, 2, 3], "B": [5, 6, 7]})

check(
assert_type(
pd.DatetimeIndex(data=df["A"], tz=None, ambiguous="NaT", copy=True),
pd.DatetimeIndex,
),
pd.DatetimeIndex,
)


def test_iter() -> None:
# GH 723
with pytest_warns_bounded(
Expand All @@ -1207,16 +1149,6 @@ def test_iter() -> None:
check(assert_type(ts, pd.Timestamp), pd.Timestamp)


def test_intersection() -> None:
# GH 744
index = pd.DatetimeIndex(["2022-01-01"])
check(assert_type(index.intersection(index), pd.DatetimeIndex), pd.DatetimeIndex)
check(
assert_type(index.intersection([pd.Timestamp("1/1/2023")]), pd.DatetimeIndex),
pd.DatetimeIndex,
)


def test_annotate() -> None:
# GH 502
df = pd.DataFrame({"a": [1, 2]})
Expand Down Expand Up @@ -1368,24 +1300,11 @@ def test_disallow_empty_index() -> None:
_0 = pd.Index() # type: ignore[call-overload] # pyright: ignore[reportCallIssue]


def test_datetime_index_max_min_reductions() -> None:
dtidx = pd.DatetimeIndex(["2020-01-01", "2020-01-02"])
check(assert_type(dtidx.argmax(), np.int64), np.int64)
check(assert_type(dtidx.argmin(), np.int64), np.int64)
check(assert_type(dtidx.max(), pd.Timestamp), pd.Timestamp)
check(assert_type(dtidx.min(), pd.Timestamp), pd.Timestamp)


def test_periodindex_shift() -> None:
ind = pd.period_range(start="2022-06-01", periods=10)
check(assert_type(ind.shift(1), pd.PeriodIndex), pd.PeriodIndex)


def test_datetimeindex_shift() -> None:
ind = pd.date_range("2023-01-01", "2023-02-01")
check(assert_type(ind.shift(1), pd.DatetimeIndex), pd.DatetimeIndex)


def test_timedeltaindex_shift() -> None:
ind = pd.date_range("1/1/2021", "1/5/2021") - pd.Timestamp("1/3/2019")
# broken on 3.0.0.dev0 as of 20250813, fix with pandas-dev/pandas/issues/62094
Expand Down