diff --git a/pandas-stubs/_libs/tslibs/period.pyi b/pandas-stubs/_libs/tslibs/period.pyi index d38dbcfc2..315030954 100644 --- a/pandas-stubs/_libs/tslibs/period.pyi +++ b/pandas-stubs/_libs/tslibs/period.pyi @@ -61,7 +61,12 @@ class PeriodMixin: class Period(PeriodMixin): def __init__( self, - value: Period | str | None = ..., + value: Period + | str + | datetime.datetime + | datetime.date + | Timestamp + | None = ..., freq: str | BaseOffset | None = ..., ordinal: int | None = ..., year: int | None = ..., diff --git a/pandas-stubs/core/indexes/period.pyi b/pandas-stubs/core/indexes/period.pyi index 8edd2cae7..49b23b102 100644 --- a/pandas-stubs/core/indexes/period.pyi +++ b/pandas-stubs/core/indexes/period.pyi @@ -1,4 +1,5 @@ from collections.abc import Hashable +import datetime from typing import overload import numpy as np @@ -84,8 +85,18 @@ class PeriodIndex( # type: ignore[misc] def freqstr(self) -> str: ... def period_range( - start: str | pd.Period | None = ..., - end: str | pd.Period | None = ..., + start: str + | datetime.datetime + | datetime.date + | pd.Timestamp + | pd.Period + | None = ..., + end: str + | datetime.datetime + | datetime.date + | pd.Timestamp + | pd.Period + | None = ..., periods: int | None = ..., freq: str | BaseOffset | None = ..., name: Hashable | None = ..., diff --git a/tests/test_scalars.py b/tests/test_scalars.py index 510536502..958a03b61 100644 --- a/tests/test_scalars.py +++ b/tests/test_scalars.py @@ -1,5 +1,6 @@ from __future__ import annotations +import datetime import datetime as dt from typing import ( TYPE_CHECKING, @@ -1690,6 +1691,20 @@ def test_period_construction() -> None: pd.Period, ) check(assert_type(pd.Period(freq="Q", year=2012, quarter=2), pd.Period), pd.Period) + check( + assert_type( + pd.Period(value=datetime.datetime(2012, 1, 1), freq="D"), pd.Period + ), + pd.Period, + ) + check( + assert_type(pd.Period(value=datetime.date(2012, 1, 1), freq="D"), pd.Period), + pd.Period, + ) + check( + assert_type(pd.Period(value=pd.Timestamp(2012, 1, 1), freq="D"), pd.Period), + pd.Period, + ) def test_period_properties() -> None: diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index 01d6a29b4..20a965545 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -838,6 +838,31 @@ def test_period_range() -> None: ), pd.PeriodIndex, ) + check( + assert_type( + pd.period_range( + pd.Timestamp("2001-01-01"), end=pd.Timestamp("2002-01-01"), freq="Q" + ), + pd.PeriodIndex, + ), + pd.PeriodIndex, + ) + check( + assert_type( + pd.period_range( + dt.datetime(2001, 1, 1), end=dt.datetime(2002, 1, 1), freq="Q" + ), + pd.PeriodIndex, + ), + pd.PeriodIndex, + ) + check( + assert_type( + pd.period_range(dt.date(2001, 1, 1), end=dt.date(2002, 1, 1), freq="Q"), + pd.PeriodIndex, + ), + pd.PeriodIndex, + ) def test_to_datetime_scalar() -> None: