-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
TYP: libperiod #40990
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
TYP: libperiod #40990
Changes from all commits
f834870
c98f25e
a81761d
de64c6a
0ec5f86
fd2dc45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
from typing import Literal | ||
|
||
import numpy as np | ||
|
||
from pandas._libs.tslibs.nattype import NaTType | ||
from pandas._libs.tslibs.offsets import BaseOffset | ||
from pandas._libs.tslibs.timestamps import Timestamp | ||
from pandas._typing import ( | ||
Frequency, | ||
Timezone, | ||
) | ||
|
||
INVALID_FREQ_ERR_MSG: str | ||
DIFFERENT_FREQ: str | ||
|
||
class IncompatibleFrequency(ValueError): ... | ||
|
||
def periodarr_to_dt64arr( | ||
periodarr: np.ndarray, # const int64_t[:] | ||
freq: int, | ||
) -> np.ndarray: ... # np.ndarray[np.int64] | ||
|
||
def period_asfreq_arr( | ||
arr: np.ndarray, # ndarray[int64_t] arr, | ||
freq1: int, | ||
freq2: int, | ||
end: bool, | ||
) -> np.ndarray: ... # np.ndarray[np.int64] | ||
|
||
def get_period_field_arr( | ||
field: str, | ||
arr: np.ndarray, # const int64_t[:] | ||
freq: int, | ||
) -> np.ndarray: ... # np.ndarray[np.int64] | ||
|
||
def from_ordinals( | ||
values: np.ndarray, # const int64_t[:] | ||
freq: Frequency, | ||
) -> np.ndarray: ... # np.ndarray[np.int64] | ||
|
||
def extract_ordinals( | ||
values: np.ndarray, # np.ndarray[object] | ||
freq: Frequency | int, | ||
) -> np.ndarray: ... # np.ndarray[np.int64] | ||
|
||
def extract_freq( | ||
values: np.ndarray, # np.ndarray[object] | ||
) -> BaseOffset: ... | ||
|
||
# exposed for tests | ||
def period_asfreq(ordinal: int, freq1: int, freq2: int, end: bool) -> int: ... | ||
|
||
def period_ordinal( | ||
y: int, m: int, d: int, h: int, min: int, s: int, us: int, ps: int, freq: int | ||
) -> int: ... | ||
|
||
def freq_to_dtype_code(freq: BaseOffset) -> int: ... | ||
def validate_end_alias(how: str) -> Literal["E", "S"]: ... | ||
|
||
class Period: | ||
ordinal: int # int64_t | ||
freq: BaseOffset | ||
|
||
# error: "__new__" must return a class instance (got "Union[Period, NaTType]") | ||
def __new__( # type: ignore[misc] | ||
cls, | ||
value=None, | ||
freq=None, | ||
ordinal=None, | ||
year=None, | ||
month=None, | ||
quarter=None, | ||
day=None, | ||
hour=None, | ||
minute=None, | ||
second=None, | ||
) -> Period | NaTType: ... | ||
|
||
@classmethod | ||
def _maybe_convert_freq(cls, freq) -> BaseOffset: ... | ||
|
||
@classmethod | ||
def _from_ordinal(cls, ordinal: int, freq) -> Period: ... | ||
|
||
@classmethod | ||
def now(cls, freq=...) -> Period: ... | ||
|
||
def strftime(self, fmt: str) -> str: ... | ||
|
||
def to_timestamp( | ||
self, | ||
freq: str | BaseOffset | None =..., | ||
how: str = ..., | ||
tz: Timezone | None = ..., | ||
) -> Timestamp: ... | ||
|
||
def asfreq(self, freq, how=...) -> Period: ... | ||
|
||
@property | ||
def freqstr(self) -> str: ... | ||
|
||
@property | ||
def is_leap_year(self) -> bool: ... | ||
|
||
@property | ||
def daysinmonth(self) -> int: ... | ||
|
||
@property | ||
def days_in_month(self) -> int: ... | ||
|
||
@property | ||
def qyear(self) -> int: ... | ||
|
||
@property | ||
def quarter(self) -> int: ... | ||
|
||
@property | ||
def day_of_year(self) -> int: ... | ||
|
||
@property | ||
def weekday(self) -> int: ... | ||
|
||
@property | ||
def day_of_week(self) -> int: ... | ||
|
||
@property | ||
def week(self) -> int: ... | ||
|
||
@property | ||
def weekofyear(self) -> int: ... | ||
|
||
@property | ||
def second(self) -> int: ... | ||
|
||
@property | ||
def minute(self) -> int: ... | ||
|
||
@property | ||
def hour(self) -> int: ... | ||
|
||
@property | ||
def day(self) -> int: ... | ||
|
||
@property | ||
def month(self) -> int: ... | ||
|
||
@property | ||
def year(self) -> int: ... | ||
|
||
@property | ||
def end_time(self) -> Timestamp: ... | ||
|
||
@property | ||
def start_time(self) -> Timestamp: ... | ||
|
||
def __sub__(self, other) -> Period | BaseOffset: ... | ||
|
||
def __add__(self, other) -> Period: ... |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -295,9 +295,17 @@ def _generate_range(cls, start, end, periods, freq, fields): | |
# ----------------------------------------------------------------- | ||
# DatetimeLike Interface | ||
|
||
def _unbox_scalar(self, value: Period | NaTType, setitem: bool = False) -> np.int64: | ||
# error: Argument 1 of "_unbox_scalar" is incompatible with supertype | ||
# "DatetimeLikeArrayMixin"; supertype defines the argument type as | ||
# "Union[Union[Period, Any, Timedelta], NaTType]" | ||
def _unbox_scalar( # type: ignore[override] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think DatetimeLikeArrayMixin needs to be Generic[DatetimeLikeScalarT]. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying this i get I'll troubleshoot this a bit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok to leave as a 'fix later' ignore so we can start benefitting straight away from the added types There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good |
||
self, | ||
value: Period | NaTType, | ||
setitem: bool = False, | ||
) -> np.int64: | ||
if value is NaT: | ||
return np.int64(value.value) | ||
# error: Item "Period" of "Union[Period, NaTType]" has no attribute "value" | ||
return np.int64(value.value) # type: ignore[union-attr] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a false positive. if not planning to 'fix later', can either cast or add a reference to a mypy issue. maybe python/mypy#7642, although AFAICT the discussion there is more around a singleton class (i.e. final, not subclassed) rather than a singleton instance. however, from python/mypy#7642 (comment)
so probably an appropriate issue to discuss this. |
||
elif isinstance(value, self._scalar_type): | ||
self._check_compatible_with(value, setitem=setitem) | ||
return np.int64(value.ordinal) | ||
|
@@ -482,9 +490,9 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray: | |
freq = Period._maybe_convert_freq(freq) | ||
base = freq._period_dtype_code | ||
|
||
new_data = self.asfreq(freq, how=how) | ||
new_parr = self.asfreq(freq, how=how) | ||
|
||
new_data = libperiod.periodarr_to_dt64arr(new_data.asi8, base) | ||
new_data = libperiod.periodarr_to_dt64arr(new_parr.asi8, base) | ||
return DatetimeArray(new_data)._with_freq("infer") | ||
|
||
# -------------------------------------------------------------------- | ||
|
@@ -910,7 +918,7 @@ def raise_on_incompatible(left, right): | |
|
||
|
||
def period_array( | ||
data: Sequence[Period | None] | AnyArrayLike, | ||
data: Sequence[Period | str | None] | AnyArrayLike, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and from the docstring pd.NaT (np.nan also seems to be acceptable but not pd.NA) and also datetime.datetime
|
||
freq: str | Tick | None = None, | ||
copy: bool = False, | ||
) -> PeriodArray: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need a alias to a Literal for this to match accessor type is the cython code.