diff --git a/pandas-stubs/_libs/tslibs/timedeltas.pyi b/pandas-stubs/_libs/tslibs/timedeltas.pyi index 75ff28c87..aa09000ad 100644 --- a/pandas-stubs/_libs/tslibs/timedeltas.pyi +++ b/pandas-stubs/_libs/tslibs/timedeltas.pyi @@ -3,6 +3,7 @@ from typing import ( ClassVar, Literal, TypeVar, + Union, overload, ) @@ -17,10 +18,7 @@ from pandas._typing import npt # This should be kept consistent with the keys in the dict timedelta_abbrevs # in pandas/_libs/tslibs/timedeltas.pyx -UnitChoices: TypeAlias = Literal[ - "Y", - "y", - "M", +TimeDeltaUnitChoices: TypeAlias = Literal[ "W", "w", "D", @@ -60,6 +58,16 @@ UnitChoices: TypeAlias = Literal[ "nanosecond", "n", ] + +UnitChoices: TypeAlias = Union[ + TimeDeltaUnitChoices, + Literal[ + "Y", + "y", + "M", + ], +] + _S = TypeVar("_S", bound=timedelta) def ints_to_pytimedelta( diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index a14b9647d..4ef2c6c75 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -19,6 +19,7 @@ from typing import ( import numpy as np from numpy import typing as npt from pandas.core.arrays import ExtensionArray +from pandas.core.frame import DataFrame from pandas.core.generic import NDFrame from pandas.core.groupby.grouper import Grouper from pandas.core.indexes.base import Index @@ -46,6 +47,27 @@ PandasScalar: TypeAlias = Union[ DatetimeLike: TypeAlias = Union[datetime.datetime, np.datetime64, Timestamp] +DatetimeDictArg: TypeAlias = Union[ + Sequence[int], Sequence[float], list[str], tuple[Scalar, ...], AnyArrayLike +] +DictConvertible: TypeAlias = Union[FulldatetimeDict, DataFrame] + +class YearMonthDayDict(TypedDict, total=True): + year: DatetimeDictArg + month: DatetimeDictArg + day: DatetimeDictArg + +class FulldatetimeDict(YearMonthDayDict, total=False): + hour: DatetimeDictArg + hours: DatetimeDictArg + minute: DatetimeDictArg + minutes: DatetimeDictArg + second: DatetimeDictArg + seconds: DatetimeDictArg + ms: DatetimeDictArg + us: DatetimeDictArg + ns: DatetimeDictArg + # dtypes NpDtype: TypeAlias = Union[ str, np.dtype[np.generic], type[Union[str, complex, bool, object]] @@ -178,7 +200,7 @@ IndexingInt: TypeAlias = Union[ int, np.int_, np.integer, np.unsignedinteger, np.signedinteger, np.int8 ] TimestampConvertibleTypes: TypeAlias = Union[ - Timestamp, datetime.datetime, np.datetime64, np.int64, float, str + Timestamp, datetime.datetime, datetime.date, np.datetime64, np.int64, float, str ] TimedeltaConvertibleTypes: TypeAlias = Union[ Timedelta, datetime.timedelta, np.timedelta64, np.int64, float, str diff --git a/pandas-stubs/core/indexes/datetimes.pyi b/pandas-stubs/core/indexes/datetimes.pyi index 7bd4bbe5d..5b6cc4216 100644 --- a/pandas-stubs/core/indexes/datetimes.pyi +++ b/pandas-stubs/core/indexes/datetimes.pyi @@ -1,8 +1,13 @@ from datetime import ( + date, timedelta, tzinfo, ) -from typing import overload +from typing import ( + Hashable, + Sequence, + overload, +) import numpy as np from pandas import ( @@ -90,10 +95,10 @@ def date_range( freq: str | timedelta | Timedelta | BaseOffset = ..., tz: str | tzinfo = ..., normalize: bool = ..., - name: str | None = ..., + name: Hashable | None = ..., inclusive: IntervalClosedType = ..., - **kwargs, ) -> DatetimeIndex: ... +@overload def bdate_range( start: str | DatetimeLike | None = ..., end: str | DatetimeLike | None = ..., @@ -101,9 +106,22 @@ def bdate_range( freq: str | timedelta | Timedelta | BaseOffset = ..., tz: str | tzinfo = ..., normalize: bool = ..., - name: str | None = ..., + name: Hashable | None = ..., + weekmask: str | None = ..., + holidays: None = ..., + inclusive: IntervalClosedType = ..., +) -> DatetimeIndex: ... +@overload +def bdate_range( + start: str | DatetimeLike | None = ..., + end: str | DatetimeLike | None = ..., + periods: int | None = ..., + *, + freq: str | timedelta | Timedelta | BaseOffset, + tz: str | tzinfo = ..., + normalize: bool = ..., + name: Hashable | None = ..., weekmask: str | None = ..., - holidays: list | None = ..., + holidays: Sequence[str | DatetimeLike | date], inclusive: IntervalClosedType = ..., - **kwargs, ) -> DatetimeIndex: ... diff --git a/pandas-stubs/core/indexes/period.pyi b/pandas-stubs/core/indexes/period.pyi index 520d53644..665bd28b3 100644 --- a/pandas-stubs/core/indexes/period.pyi +++ b/pandas-stubs/core/indexes/period.pyi @@ -1,9 +1,14 @@ +from typing import Hashable + import numpy as np +import pandas as pd from pandas.core.indexes.datetimelike import ( DatetimeIndexOpsMixin as DatetimeIndexOpsMixin, ) from pandas.core.indexes.numeric import Int64Index +from pandas._libs.tslibs import BaseOffset + class PeriodIndex(DatetimeIndexOpsMixin, Int64Index): def __new__( cls, @@ -46,5 +51,9 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index): def memory_usage(self, deep: bool = ...): ... def period_range( - start=..., end=..., periods=..., freq=..., name=... + start: str | pd.Period | None = ..., + end: str | pd.Period | None = ..., + periods: int | None = ..., + freq: str | BaseOffset | None = ..., + name: Hashable | None = ..., ) -> PeriodIndex: ... diff --git a/pandas-stubs/core/indexes/timedeltas.pyi b/pandas-stubs/core/indexes/timedeltas.pyi index 962270e33..9b06abfb6 100644 --- a/pandas-stubs/core/indexes/timedeltas.pyi +++ b/pandas-stubs/core/indexes/timedeltas.pyi @@ -1,5 +1,10 @@ -from typing import overload +from typing import ( + Hashable, + Literal, + overload, +) +from pandas import DateOffset from pandas.core.indexes.accessors import TimedeltaIndexProperties from pandas.core.indexes.datetimelike import DatetimeTimedeltaMixin from pandas.core.indexes.datetimes import DatetimeIndex @@ -9,7 +14,10 @@ from pandas._libs import ( Timedelta, Timestamp, ) -from pandas._typing import num +from pandas._typing import ( + TimedeltaConvertibleTypes, + num, +) class TimedeltaIndex(DatetimeTimedeltaMixin, TimedeltaIndexProperties): def __new__( @@ -42,5 +50,10 @@ class TimedeltaIndex(DatetimeTimedeltaMixin, TimedeltaIndexProperties): def to_series(self, index=..., name=...) -> TimedeltaSeries: ... def timedelta_range( - start=..., end=..., periods=..., freq=..., name=..., closed=... + start: TimedeltaConvertibleTypes = ..., + end: TimedeltaConvertibleTypes = ..., + periods: int | None = ..., + freq: str | DateOffset | None = ..., + name: Hashable | None = ..., + closed: Literal["left", "right"] | None = ..., ) -> TimedeltaIndex: ... diff --git a/pandas-stubs/core/tools/datetimes.pyi b/pandas-stubs/core/tools/datetimes.pyi index abd8dbebd..6ed6fe864 100644 --- a/pandas-stubs/core/tools/datetimes.pyi +++ b/pandas-stubs/core/tools/datetimes.pyi @@ -1,18 +1,21 @@ -from datetime import datetime +from datetime import ( + date, + datetime, +) from typing import ( Literal, - TypedDict, + Sequence, Union, overload, ) import numpy as np +import pandas as pd from pandas import ( Index, Timestamp, ) from pandas.core.arrays import ExtensionArray -from pandas.core.frame import DataFrame from pandas.core.indexes.datetimes import DatetimeIndex from pandas.core.series import ( Series, @@ -24,38 +27,18 @@ from pandas._libs.tslibs import NaTType from pandas._typing import ( AnyArrayLike, DateTimeErrorChoices, + DictConvertible, IgnoreRaise, + TimestampConvertibleTypes, + npt, ) ArrayConvertible: TypeAlias = Union[list, tuple, AnyArrayLike] Scalar: TypeAlias = Union[float, str] -DatetimeScalar: TypeAlias = Union[Scalar, datetime, np.datetime64] +DatetimeScalar: TypeAlias = Union[Scalar, datetime, np.datetime64, date] DatetimeScalarOrArrayConvertible: TypeAlias = Union[DatetimeScalar, ArrayConvertible] -DatetimeDictArg: TypeAlias = Union[list[Scalar], tuple[Scalar, ...], AnyArrayLike] - -class YearMonthDayDict(TypedDict, total=True): - year: DatetimeDictArg - month: DatetimeDictArg - day: DatetimeDictArg - -class FulldatetimeDict(YearMonthDayDict, total=False): - hour: DatetimeDictArg - hours: DatetimeDictArg - minute: DatetimeDictArg - minutes: DatetimeDictArg - second: DatetimeDictArg - seconds: DatetimeDictArg - ms: DatetimeDictArg - us: DatetimeDictArg - ns: DatetimeDictArg - -DictConvertible: TypeAlias = Union[FulldatetimeDict, DataFrame] - -def should_cache( - arg: ArrayConvertible, unique_share: float = ..., check_count: int | None = ... -) -> bool: ... @overload def to_datetime( arg: DatetimeScalar, @@ -67,7 +50,7 @@ def to_datetime( exact: bool = ..., unit: str | None = ..., infer_datetime_format: bool = ..., - origin=..., + origin: Literal["julian", "unix"] | TimestampConvertibleTypes = ..., cache: bool = ..., ) -> Timestamp: ... @overload @@ -81,7 +64,7 @@ def to_datetime( exact: bool = ..., unit: str | None = ..., infer_datetime_format: bool = ..., - origin=..., + origin: Literal["julian", "unix"] | TimestampConvertibleTypes = ..., cache: bool = ..., ) -> Timestamp | NaTType: ... @overload @@ -95,12 +78,19 @@ def to_datetime( exact: bool = ..., unit: str | None = ..., infer_datetime_format: bool = ..., - origin=..., + origin: Literal["julian", "unix"] | TimestampConvertibleTypes = ..., cache: bool = ..., ) -> TimestampSeries: ... @overload def to_datetime( - arg: list | tuple | np.ndarray | Index | ExtensionArray, + arg: Sequence[int | float | datetime] + | list[str] + | tuple[int | float | str | datetime, ...] + | npt.NDArray[np.datetime64] + | npt.NDArray[np.str_] + | npt.NDArray[np.int_] + | Index + | ExtensionArray, errors: DateTimeErrorChoices = ..., dayfirst: bool = ..., yearfirst: bool = ..., @@ -109,7 +99,6 @@ def to_datetime( exact: bool = ..., unit: str | None = ..., infer_datetime_format: bool = ..., - origin=..., + origin: Literal["julian", "unix"] | TimestampConvertibleTypes = ..., cache: bool = ..., ) -> DatetimeIndex: ... -def to_time(arg, format=..., infer_time_format: bool = ..., errors: str = ...): ... diff --git a/pandas-stubs/core/tools/timedeltas.pyi b/pandas-stubs/core/tools/timedeltas.pyi index 8d84fa7c6..c2b881628 100644 --- a/pandas-stubs/core/tools/timedeltas.pyi +++ b/pandas-stubs/core/tools/timedeltas.pyi @@ -1,7 +1,10 @@ -# def to_timedelta(arg, unit: str = ..., errors: str = ...): ... from datetime import timedelta -from typing import overload +from typing import ( + Sequence, + overload, +) +import pandas as pd from pandas import Index from pandas.core.indexes.timedeltas import TimedeltaIndex from pandas.core.series import ( @@ -10,29 +13,32 @@ from pandas.core.series import ( ) from pandas._libs.tslibs import Timedelta -from pandas._libs.tslibs.timedeltas import UnitChoices +from pandas._libs.tslibs.timedeltas import TimeDeltaUnitChoices from pandas._typing import ( ArrayLike, DateTimeErrorChoices, ) -# Copied from pandas/_libs/tslibs/timedeltas.pyx - @overload def to_timedelta( arg: str | float | timedelta, - unit: UnitChoices | None = ..., + unit: TimeDeltaUnitChoices | None = ..., errors: DateTimeErrorChoices = ..., ) -> Timedelta: ... @overload def to_timedelta( arg: Series, - unit: UnitChoices | None = ..., + unit: TimeDeltaUnitChoices | None = ..., errors: DateTimeErrorChoices = ..., ) -> TimedeltaSeries: ... @overload def to_timedelta( - arg: list | tuple | range | ArrayLike | Index, - unit: UnitChoices | None = ..., + arg: Sequence[float | timedelta] + | list[str | float | timedelta] + | tuple[str | float | timedelta, ...] + | range + | ArrayLike + | Index, + unit: TimeDeltaUnitChoices | None = ..., errors: DateTimeErrorChoices = ..., ) -> TimedeltaIndex: ... diff --git a/pandas-stubs/tseries/frequencies.pyi b/pandas-stubs/tseries/frequencies.pyi index 70da435f2..199cb1443 100644 --- a/pandas-stubs/tseries/frequencies.pyi +++ b/pandas-stubs/tseries/frequencies.pyi @@ -1,6 +1,12 @@ +from pandas import ( + DatetimeIndex, + Series, + TimedeltaIndex, +) + from pandas.tseries.offsets import DateOffset as DateOffset def get_period_alias(offset_str: str) -> str | None: ... def to_offset(freq) -> DateOffset | None: ... def get_offset(name: str) -> DateOffset: ... -def infer_freq(index) -> str | None: ... +def infer_freq(index: Series | DatetimeIndex | TimedeltaIndex) -> str | None: ... diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index 24bd12dc4..c6d1fe3b7 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -4,6 +4,7 @@ import datetime as dt from typing import ( TYPE_CHECKING, + Any, Optional, Union, ) @@ -18,6 +19,10 @@ from pandas._libs import NaTType from pandas._libs.tslibs import BaseOffset +if TYPE_CHECKING: + from pandas._typing import FulldatetimeDict +else: + FulldatetimeDict = Any from tests import ( TYPE_CHECKING_INVALID_USAGE, check, @@ -534,3 +539,388 @@ def test_types_to_numpy() -> None: check(assert_type(td_s.to_numpy(), np.ndarray), np.ndarray) check(assert_type(td_s.to_numpy(dtype="int", copy=True), np.ndarray), np.ndarray) check(assert_type(td_s.to_numpy(na_value=0), np.ndarray), np.ndarray) + + +def test_to_timdelta_units() -> None: + check(assert_type(pd.to_timedelta(1, "W"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "w"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "D"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "d"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "days"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "day"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "hours"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "hour"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "hr"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "h"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "m"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "minute"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "min"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "minutes"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "t"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "s"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "seconds"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "sec"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "second"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "ms"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "milliseconds"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "millisecond"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "milli"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "millis"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "l"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "us"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "microseconds"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "microsecond"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "µs"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "micro"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "micros"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "u"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "ns"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "nanoseconds"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "nano"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "nanos"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "nanosecond"), pd.Timedelta), pd.Timedelta) + check(assert_type(pd.to_timedelta(1, "n"), pd.Timedelta), pd.Timedelta) + + +def test_to_timedelta_scalar() -> None: + check( + assert_type(pd.to_timedelta(10, "ms", errors="raise"), pd.Timedelta), + pd.Timedelta, + ) + check( + assert_type(pd.to_timedelta("10ms", errors="ignore"), pd.Timedelta), + pd.Timedelta, + ) + check( + assert_type( + pd.to_timedelta(dt.timedelta(milliseconds=10), errors="coerce"), + pd.Timedelta, + ), + pd.Timedelta, + ) + + +def test_to_timedelta_series() -> None: + s = pd.Series([10, 20, 30, 40]) + s2 = pd.Series(["10ms", "20ms", "30ms"]) + check(assert_type(pd.to_timedelta(s, "ms"), "TimedeltaSeries"), pd.Series) + check(assert_type(pd.to_timedelta(s2), "TimedeltaSeries"), pd.Series) + + +def test_to_timedelta_index() -> None: + arg0 = [1.0, 2.0, 3.0] + arg1 = [ + dt.timedelta(milliseconds=1), + dt.timedelta(milliseconds=2), + dt.timedelta(milliseconds=3), + ] + arg2 = tuple(arg0) + arg3 = tuple(arg1) + arg4 = range(0, 10) + arg5 = np.arange(10) + arg6 = pd.Index(arg5) + check( + assert_type(pd.to_timedelta(arg0, "ms"), pd.TimedeltaIndex), pd.TimedeltaIndex + ) + check( + assert_type(pd.to_timedelta(arg1, "ms"), pd.TimedeltaIndex), pd.TimedeltaIndex + ) + check( + assert_type(pd.to_timedelta(arg2, "ms"), pd.TimedeltaIndex), pd.TimedeltaIndex + ) + check( + assert_type(pd.to_timedelta(arg3, "ms"), pd.TimedeltaIndex), pd.TimedeltaIndex + ) + check( + assert_type(pd.to_timedelta(arg4, "ms"), pd.TimedeltaIndex), pd.TimedeltaIndex + ) + check( + assert_type(pd.to_timedelta(arg5, "ms"), pd.TimedeltaIndex), pd.TimedeltaIndex + ) + check( + assert_type(pd.to_timedelta(arg6, "ms"), pd.TimedeltaIndex), pd.TimedeltaIndex + ) + + +def test_bdate_range_holidays() -> None: + pd.bdate_range("2000-1-1", "2001-1-1", freq="C", holidays=["2000-12-15"]) + pd.bdate_range( + "2000-1-1", "2001-1-1", freq="C", holidays=[pd.Timestamp(2000, 12, 15)] + ) + pd.bdate_range( + "2000-1-1", "2001-1-1", freq="C", holidays=[np.datetime64("2000-12-15")] + ) + pd.bdate_range( + "2000-1-1", "2001-1-1", freq="C", holidays=[dt.datetime(2000, 12, 15)] + ) + pd.bdate_range( + "2000-1-1", "2001-1-1", freq="C", holidays=[dt.date(2000, 12, 15)], name=("a",) + ) + + +def test_period_range() -> None: + check( + assert_type( + pd.period_range(pd.Period("2001Q1"), end=pd.Period("2010Q1")), + pd.PeriodIndex, + ), + pd.PeriodIndex, + ) + check( + assert_type(pd.period_range("2001Q1", end=pd.Period("2010Q1")), pd.PeriodIndex), + pd.PeriodIndex, + ) + check( + assert_type( + pd.period_range("2001-01-01", end="2010-01-01", freq="Q"), pd.PeriodIndex + ), + pd.PeriodIndex, + ) + check( + assert_type(pd.period_range("2001Q1", periods=100, freq="Q"), pd.PeriodIndex), + pd.PeriodIndex, + ) + check( + assert_type(pd.period_range("2001Q1", periods=100, freq=Day()), pd.PeriodIndex), + pd.PeriodIndex, + ) + check( + assert_type( + pd.period_range("2001Q1", periods=100, freq=Day(), name=("A",)), + pd.PeriodIndex, + ), + pd.PeriodIndex, + ) + + +def test_to_datetime_scalar() -> None: + check(assert_type(pd.to_datetime("2000-01-01"), pd.Timestamp), pd.Timestamp) + check(assert_type(pd.to_datetime(1), pd.Timestamp), pd.Timestamp) + check(assert_type(pd.to_datetime(1.5), pd.Timestamp), pd.Timestamp) + check( + assert_type(pd.to_datetime(dt.datetime(2000, 1, 1)), pd.Timestamp), pd.Timestamp + ) + check(assert_type(pd.to_datetime(dt.date(2000, 1, 1)), pd.Timestamp), pd.Timestamp) + check( + assert_type(pd.to_datetime(np.datetime64("2000-01-01")), pd.Timestamp), + pd.Timestamp, + ) + + +def test_to_datetime_scalar_extended() -> None: + check(assert_type(pd.to_datetime("2000-01-01"), pd.Timestamp), pd.Timestamp) + check(assert_type(pd.to_datetime(1), pd.Timestamp), pd.Timestamp) + check(assert_type(pd.to_datetime(1.5), pd.Timestamp), pd.Timestamp) + check( + assert_type(pd.to_datetime(dt.datetime(2000, 1, 1)), pd.Timestamp), pd.Timestamp + ) + check(assert_type(pd.to_datetime(dt.date(2000, 1, 1)), pd.Timestamp), pd.Timestamp) + check( + assert_type(pd.to_datetime(np.datetime64("2000-01-01")), pd.Timestamp), + pd.Timestamp, + ) + + +def test_to_datetime_series() -> None: + s = pd.Series(["2000-01-01", "2000-01-02"]) + check(assert_type(pd.to_datetime(s), "TimestampSeries"), pd.Series) + d: FulldatetimeDict = { + "year": [2000, 2000, 2000], + "month": [1, 1, 1], + "day": [1, 2, 3], + } + df = pd.DataFrame(d) + d_ex: FulldatetimeDict = { + "year": [2000, 2000, 2000], + "month": [1, 1, 1], + "day": [1, 2, 3], + "hour": [1, 1, 1], + "hours": [1, 1, 1], + "minute": [1, 1, 1], + "minutes": [1, 1, 1], + "second": [1, 1, 1], + "seconds": [1, 1, 1], + "ms": [1, 1, 1], + "us": [1, 1, 1], + "ns": [1, 1, 1], + } + check(assert_type(pd.to_datetime(df), "TimestampSeries"), pd.Series) + check(assert_type(pd.to_datetime(d), "TimestampSeries"), pd.Series) + check(assert_type(pd.to_datetime(d_ex), "TimestampSeries"), pd.Series) + + +def test_to_datetime_array() -> None: + check(assert_type(pd.to_datetime([1, 2, 3]), pd.DatetimeIndex), pd.DatetimeIndex) + check( + assert_type(pd.to_datetime([1.5, 2.5, 3.5]), pd.DatetimeIndex), pd.DatetimeIndex + ) + check( + assert_type( + pd.to_datetime( + [ + dt.datetime(2000, 1, 1), + dt.datetime(2000, 1, 2), + dt.datetime(2000, 1, 3), + ] + ), + pd.DatetimeIndex, + ), + pd.DatetimeIndex, + ) + check( + assert_type( + pd.to_datetime(["2000-01-01", "2000-01-02", "2000-01-03"]), pd.DatetimeIndex + ), + pd.DatetimeIndex, + ) + check(assert_type(pd.to_datetime((1, 2, 3)), pd.DatetimeIndex), pd.DatetimeIndex) + check( + assert_type(pd.to_datetime((1.5, 2.5, 3.5)), pd.DatetimeIndex), pd.DatetimeIndex + ) + check( + assert_type( + pd.to_datetime( + ( + dt.datetime(2000, 1, 1), + dt.datetime(2000, 1, 2), + dt.datetime(2000, 1, 3), + ) + ), + pd.DatetimeIndex, + ), + pd.DatetimeIndex, + ) + check( + assert_type( + pd.to_datetime(("2000-01-01", "2000-01-02", "2000-01-03")), pd.DatetimeIndex + ), + pd.DatetimeIndex, + ) + check( + assert_type( + pd.to_datetime( + np.array( + [ + np.datetime64("2000-01-01"), + np.datetime64("2000-01-02"), + np.datetime64("2000-01-03"), + ] + ) + ), + pd.DatetimeIndex, + ), + pd.DatetimeIndex, + ) + check( + assert_type( + pd.to_datetime(np.array(["2000-01-01", "2000-01-02", "2000-01-03"])), + pd.DatetimeIndex, + ), + pd.DatetimeIndex, + ) + check( + assert_type(pd.to_datetime(np.array([1, 2, 3])), pd.DatetimeIndex), + pd.DatetimeIndex, + ) + pd.to_datetime( + pd.Index([2451544.5, 2451545.5, 2451546.5]), + unit="D", + origin="julian", + ) + check( + assert_type( + pd.to_datetime(pd.Index([1, 2, 3]), origin="unix"), pd.DatetimeIndex + ), + pd.DatetimeIndex, + ) + check( + assert_type(pd.to_datetime(pd.Index([1, 2, 3]), origin=4), pd.DatetimeIndex), + pd.DatetimeIndex, + ) + check( + assert_type( + pd.to_datetime(pd.Index([1, 2, 3]), origin=pd.Timestamp("1999-12-31")), + pd.DatetimeIndex, + ), + pd.DatetimeIndex, + ) + check( + assert_type( + pd.to_datetime(pd.Index([1, 2, 3]), origin=np.datetime64("1999-12-31")), + pd.DatetimeIndex, + ), + pd.DatetimeIndex, + ) + check( + assert_type( + pd.to_datetime(pd.Index([1, 2, 3]), origin=dt.datetime(1999, 12, 31)), + pd.DatetimeIndex, + ), + pd.DatetimeIndex, + ) + check( + assert_type( + pd.to_datetime(pd.Index([1, 2, 3]), origin=dt.date(1999, 12, 31)), + pd.DatetimeIndex, + ), + pd.DatetimeIndex, + ) + + +def test_timedelta_range() -> None: + check( + assert_type( + pd.timedelta_range( + pd.Timedelta(1, unit="D"), pd.Timedelta(10, unit="D"), periods=10 + ), + pd.TimedeltaIndex, + ), + pd.TimedeltaIndex, + ) + check( + assert_type( + pd.timedelta_range(dt.timedelta(1), dt.timedelta(10), periods=10), + pd.TimedeltaIndex, + ), + pd.TimedeltaIndex, + ) + check( + assert_type( + pd.timedelta_range( + np.timedelta64(86400000000000), + np.timedelta64(864000000000000), + periods=10, + ), + pd.TimedeltaIndex, + ), + pd.TimedeltaIndex, + ) + check( + assert_type( + pd.timedelta_range("1 day", "10 days", periods=10), pd.TimedeltaIndex + ), + pd.TimedeltaIndex, + ) + check( + assert_type( + pd.timedelta_range( + np.int64(86400000000000), np.int64(864000000000000), periods=10 + ), + pd.TimedeltaIndex, + ), + pd.TimedeltaIndex, + ) + check( + assert_type( + pd.timedelta_range(86400000000000, 864000000000000, periods=10), + pd.TimedeltaIndex, + ), + pd.TimedeltaIndex, + ) + check( + assert_type( + pd.timedelta_range(86400000000000.0, 864000000000000.0, periods=10), + pd.TimedeltaIndex, + ), + pd.TimedeltaIndex, + )