Skip to content

period_range and Period should accept date, datetime and pd.Timestamp #728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion pandas-stubs/_libs/tslibs/period.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from typing import (
)

import numpy as np
import pandas
from pandas import (
Index,
PeriodIndex,
Expand Down Expand Up @@ -61,7 +62,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 = ...,
Expand Down
15 changes: 13 additions & 2 deletions pandas-stubs/core/indexes/period.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Hashable
import datetime
from typing import overload

import numpy as np
Expand Down Expand Up @@ -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 = ...,
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
@overload
def apply(
self,
func: Callable[..., Scalar | Sequence | set | Mapping | None],
func: Callable[..., Scalar | Sequence | set | Mapping | NAType | None],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change came up because you haven't merged with main. Can you make sure you have done that?

convertDType: _bool = ...,
args: tuple = ...,
**kwds,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_scalars.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import datetime
import datetime as dt
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
)
import xarray as xr

from pandas._libs.missing import NAType
from pandas._libs.tslibs.timedeltas import Timedelta
from pandas._libs.tslibs.timestamps import Timestamp
from pandas._typing import (
Expand Down Expand Up @@ -470,6 +471,8 @@ def get_depth(url: str) -> int:
ss = s.astype(str)
check(assert_type(ss.apply(get_depth), pd.Series), pd.Series, np.int64)

check(assert_type(s.apply(lambda x: pd.NA), pd.Series), pd.Series, NAType)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above - this change is already in main



def test_types_element_wise_arithmetic() -> None:
s = pd.Series([0, 1, -10])
Expand Down
21 changes: 21 additions & 0 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,27 @@ def test_period_range() -> None:
),
pd.PeriodIndex,
)
check(
assert_type(
pd.period_range(pd.Timestamp("2001-01-01"), end="2002-01-01", freq="Q"),
pd.PeriodIndex,
),
pd.PeriodIndex,
)
check(
assert_type(
pd.period_range(dt.datetime(2001, 1, 1), end="2002-01-01", freq="Q"),
pd.PeriodIndex,
),
pd.PeriodIndex,
)
check(
assert_type(
pd.period_range(dt.date(2001, 1, 1), end="2002-01-01", freq="Q"),
pd.PeriodIndex,
),
pd.PeriodIndex,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these tests, you should also change the end argument to be the corresponding type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done



def test_to_datetime_scalar() -> None:
Expand Down