Skip to content

ENH: Improve Pandas scalars #383

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

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5c0dc1c
ENH: Improve DatetimeTZDtype
bashtage Oct 11, 2022
9f5058f
ENH: Improve PeriodDtype
bashtage Oct 11, 2022
7ad615a
ENH: Improve IntervalDtype
bashtage Oct 11, 2022
fb6d782
ENH: Improve CategoricalDtype
bashtage Oct 11, 2022
fecd9f9
ENH: Improve StringDtype and StringArray
bashtage Oct 11, 2022
5adba7b
ENH: Improve BooleanDtype and BooleanArray
bashtage Oct 11, 2022
c4dca27
ENH: Improve Timestamp
bashtage Oct 11, 2022
d86adfd
ENH: Improve Timedelta
bashtage Oct 11, 2022
79f56c2
ENH: Further improvements to Timestamp
bashtage Oct 11, 2022
cc390f5
CLN: Additional cleanups to pass tests
bashtage Oct 11, 2022
c044a8c
ENH: Improve Period
bashtage Oct 11, 2022
f58c47c
TST Add tests for dtypes
Oct 11, 2022
fbfa4c1
Merge remote-tracking branch 'upstream/main' into pandas-scalars
Oct 11, 2022
ccbde88
TST: Correct types in tests
Oct 12, 2022
25aa8c9
Merge branch 'pandas-scalars' of github.com:bashtage/pandas-stubs int…
bashtage Oct 12, 2022
290f951
BUG: Correct errors in period and interval
bashtage Oct 12, 2022
e62cc10
TST: Add tests for Period
bashtage Oct 12, 2022
9c6b963
TST: Add tests for timedelta
bashtage Oct 12, 2022
e93ca54
TST: Add tests for arrays
bashtage Oct 13, 2022
9070b0d
TST: Add more scalar tests
Oct 13, 2022
28e127e
ENH: Complete Timedelta
bashtage Oct 13, 2022
f59c689
Merge remote-tracking branch 'upstream/main' into pandas-scalars
bashtage Oct 13, 2022
2f262d4
ENH: Improve Timestamp and enable tests
bashtage Oct 13, 2022
7dd8c56
TST: Add reverse ops
bashtage Oct 14, 2022
9c2d500
ENH/TST: Improve Period and its tests
bashtage Oct 14, 2022
0c2f5ca
ENH/TST: Improve Timestamp, Timedelta and their tests
bashtage Oct 14, 2022
e224537
REF: Move tests to test_scalar
bashtage Oct 14, 2022
08156cc
CLN: Final fixes to passing
bashtage Oct 14, 2022
1672751
BUG: Correct Interval
bashtage Oct 14, 2022
4c3ea13
ENH: Improve array typing
bashtage Oct 15, 2022
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
17 changes: 13 additions & 4 deletions pandas-stubs/_libs/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,37 @@ class Interval(IntervalMixin, Generic[_OrderableT]):
@overload
def __mul__(self: Interval[float], y: float) -> Interval[float]: ...
@overload
def __mul__(self: Interval[Timedelta], y: float) -> Interval[Timedelta]: ...
@overload
def __rmul__(
self: Interval[int], y: _OrderableScalarT
) -> Interval[_OrderableScalarT]: ...
@overload
def __rmul__(self: Interval[float], y: float) -> Interval[float]: ...
@overload
def __rmul__(self: Interval[Timedelta], y: float) -> Interval[Timedelta]: ...
@overload
def __truediv__(
self: Interval[int], y: _OrderableScalarT
) -> Interval[_OrderableScalarT]: ...
@overload
def __truediv__(self: Interval[float], y: float) -> Interval[float]: ...
@overload
def __truediv__(self: Interval[Timedelta], y: float) -> Interval[Timedelta]: ...
@overload
def __floordiv__(
self: Interval[int], y: _OrderableScalarT
) -> Interval[_OrderableScalarT]: ...
@overload
def __floordiv__(self: Interval[float], y: float) -> Interval[float]: ...
@overload
def __floordiv__(self: Interval[Timedelta], y: float) -> Interval[Timedelta]: ...
@overload
def overlaps(self: Interval[_OrderableT], other: Interval[_OrderableT]) -> bool: ...

def intervals_to_interval_bounds(
intervals: np.ndarray, validate_closed: bool = ...
) -> tuple[np.ndarray, np.ndarray, str]: ...
@overload
def overlaps(self: Interval[int], other: Interval[float]) -> bool: ...
@overload
def overlaps(self: Interval[float], other: Interval[int]) -> bool: ...

class IntervalTree(IntervalMixin):
def __init__(
Expand Down
154 changes: 122 additions & 32 deletions pandas-stubs/_libs/tslibs/period.pyi
Original file line number Diff line number Diff line change
@@ -1,34 +1,120 @@
from typing import Any
import datetime
from typing import (
Literal,
Union,
overload,
)

import numpy as np
from pandas import (
Index,
PeriodIndex,
Timedelta,
)
from pandas.core.series import (
PeriodSeries,
TimedeltaSeries,
)
from typing_extensions import TypeAlias

from pandas._typing import npt

from .timestamps import Timestamp

class IncompatibleFrequency(ValueError): ...

class Period:
from pandas._libs.tslibs.offsets import BaseOffset

_PeriodAddSub: TypeAlias = Union[
Timedelta, datetime.timedelta, np.timedelta64, np.int64, int, BaseOffset
]

_PeriodFreqHow: TypeAlias = Literal[
"S",
"E",
"Start",
"Finish",
"Begin",
"End",
"s",
"e",
"start",
"finish",
"begin",
"end",
]

class PeriodMixin:
@property
def end_time(self) -> Timestamp: ...
@property
def start_time(self) -> Timestamp: ...

class Period(PeriodMixin):
def __init__(
self,
value: Any = ...,
freqstr: Any = ...,
ordinal: Any = ...,
year: Any = ...,
month: int = ...,
quarter: Any = ...,
day: int = ...,
hour: int = ...,
minute: int = ...,
second: int = ...,
value: Period | str | None = ...,
freq: str | BaseOffset | None = ...,
ordinal: int | None = ...,
year: int | None = ...,
month: int | None = ...,
quarter: int | None = ...,
day: int | None = ...,
hour: int | None = ...,
minute: int | None = ...,
second: int | None = ...,
) -> None: ...
def __add__(self, other) -> Period: ...
def __eq__(self, other) -> bool: ...
def __ge__(self, other) -> bool: ...
def __gt__(self, other) -> bool: ...
@overload
def __sub__(self, other: _PeriodAddSub) -> Period: ...
@overload
def __sub__(self, other: Period) -> BaseOffset: ...
@overload
def __sub__(self, other: PeriodIndex) -> Index: ...
@overload
def __sub__(self, other: TimedeltaSeries) -> PeriodSeries: ...
@overload
def __add__(self, other: _PeriodAddSub) -> Period: ...
@overload
def __add__(self, other: Index) -> PeriodIndex: ...
@overload
def __add__(self, other: TimedeltaSeries) -> PeriodSeries: ...
@overload # type: ignore[override]
def __eq__(self, other: Period) -> bool: ...
@overload
def __eq__(self, other: PeriodIndex) -> npt.NDArray[np.bool_]: ...
@overload
def __ge__(self, other: Period) -> bool: ...
@overload
def __ge__(self, other: PeriodIndex) -> npt.NDArray[np.bool_]: ...
@overload
def __gt__(self, other: Period) -> bool: ...
@overload
def __gt__(self, other: PeriodIndex) -> npt.NDArray[np.bool_]: ...
def __hash__(self) -> int: ...
def __le__(self, other) -> bool: ...
def __lt__(self, other) -> bool: ...
def __new__(cls, *args, **kwargs) -> Period: ...
def __ne__(self, other) -> bool: ...
def __radd__(self, other) -> Period: ...
def __reduce__(self, *args, **kwargs) -> Any: ... # what should this be?
def __rsub__(self, other) -> Period: ...
def __setstate__(self, *args, **kwargs) -> Any: ... # what should this be?
@overload
def __le__(self, other: Period) -> bool: ...
@overload
def __le__(self, other: PeriodIndex) -> npt.NDArray[np.bool_]: ...
@overload
def __lt__(self, other: Period) -> bool: ...
@overload
def __lt__(self, other: PeriodIndex) -> npt.NDArray[np.bool_]: ...
@overload # type: ignore[override]
def __ne__(self, other: Period) -> bool: ...
@overload
def __ne__(self, other: PeriodIndex) -> npt.NDArray[np.bool_]: ...
# Ignored due to indecipherable error from mypy:
# Forward operator "__add__" is not callable [misc]
@overload
def __radd__(self, other: _PeriodAddSub) -> Period: ... # type: ignore[misc]
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 the error might be due to having conflicts between one of the __add__() declarations allowing Period. So try __radd__() with each one of the types in _PeriodAddSub to narrow down the possible cause.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it is because Period + Index -> PeriodIndex here but Index + Period -> Index there.

# Real signature is -> PeriodIndex, but conflicts with Index.__add__
# Changing Index is very hard due to Index inheritance
# Signatures of "__radd__" of "Period" and "__add__" of "Index"
# are unsafely overlapping
@overload
def __radd__(self, other: Index) -> Index: ...
@overload
def __radd__(self, other: TimedeltaSeries) -> PeriodSeries: ...
@property
def day(self) -> int: ...
@property
Expand All @@ -42,7 +128,7 @@ class Period:
@property
def end_time(self) -> Timestamp: ...
@property
def freq(self) -> Any: ...
def freq(self) -> BaseOffset: ...
@property
def freqstr(self) -> str: ...
@property
Expand Down Expand Up @@ -71,12 +157,16 @@ class Period:
def weekofyear(self) -> int: ...
@property
def year(self) -> int: ...
# Static methods
@property
def day_of_year(self) -> int: ...
@property
def day_of_week(self) -> int: ...
def asfreq(self, freq: str | BaseOffset, how: _PeriodFreqHow = ...) -> Period: ...
@classmethod
def now(cls) -> Period: ...
# Methods
def asfreq(self, freq: str, how: str = ...) -> Period: ...
def now(cls, freq: str | BaseOffset = ...) -> Period: ...
def strftime(self, fmt: str) -> str: ...
def to_timestamp(self, freq: str, how: str = ...) -> Timestamp: ...

from .timestamps import Timestamp
def to_timestamp(
self,
freq: str | BaseOffset | None = ...,
how: _PeriodFreqHow = ...,
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 these arguments are correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you missing a "do not" before think? Not sure how to interpret this comment since it seems to be saying I have them correct. They are all tested FWIW.

) -> Timestamp: ...
Loading