Skip to content
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

ENH: Improve general datetime functions #352

Merged
merged 15 commits into from
Oct 4, 2022
16 changes: 12 additions & 4 deletions pandas-stubs/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from typing import (
ClassVar,
Literal,
TypeVar,
Union,
overload,
)

Expand All @@ -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",
Expand Down Expand Up @@ -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(
Expand Down
22 changes: 13 additions & 9 deletions pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ from datetime import (
timedelta,
tzinfo,
)
from typing import overload
from typing import (
Hashable,
Literal,
overload,
)

import numpy as np
from pandas import (
Expand All @@ -23,7 +27,6 @@ from pandas._typing import (
AnyArrayLike,
ArrayLike,
DatetimeLike,
IntervalClosedType,
)

from pandas.core.dtypes.dtypes import DatetimeTZDtype
Expand Down Expand Up @@ -87,23 +90,24 @@ def date_range(
start: str | DatetimeLike | None = ...,
end: str | DatetimeLike | None = ...,
periods: int | None = ...,
# TODO: Test timedelta and Timedelta, update pandas docs
Copy link
Contributor

Choose a reason for hiding this comment

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

These are tested, and the docs are updated (I did them).

freq: str | timedelta | Timedelta | BaseOffset = ...,
tz: str | tzinfo = ...,
normalize: bool = ...,
name: str | None = ...,
inclusive: IntervalClosedType = ...,
**kwargs,
name: Hashable | None = ...,
inclusive: Literal["left", "right"] | None = ...,
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
) -> DatetimeIndex: ...
def bdate_range(
start: str | DatetimeLike | None = ...,
end: str | DatetimeLike | None = ...,
periods: int | None = ...,
# TODO: Test timedelta and Timedelta, update pandas docs
freq: str | timedelta | Timedelta | BaseOffset = ...,
tz: str | tzinfo = ...,
normalize: bool = ...,
name: str | None = ...,
name: Hashable | None = ...,
weekmask: str | None = ...,
holidays: list | None = ...,
inclusive: IntervalClosedType = ...,
**kwargs,
# TODO: Check if dt.date is allowed
holidays: list[str | DatetimeLike] | None = ...,
inclusive: Literal["left", "right"] | None = ...,
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
) -> DatetimeIndex: ...
9 changes: 8 additions & 1 deletion pandas-stubs/core/indexes/period.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import Hashable

import numpy as np
import pandas as pd
from pandas.core.indexes.datetimelike import (
DatetimeIndexOpsMixin as DatetimeIndexOpsMixin,
)
Expand Down Expand Up @@ -46,5 +49,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 | pd.DateOffset | None = ...,
name: Hashable | None = ...,
) -> PeriodIndex: ...
19 changes: 16 additions & 3 deletions pandas-stubs/core/indexes/timedeltas.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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__(
Expand Down Expand Up @@ -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 = ...,
bashtage marked this conversation as resolved.
Show resolved Hide resolved
periods: int | None = ...,
freq: str | DateOffset | None = ...,
name: Hashable | None = ...,
closed: Literal["left", "right"] | None = ...,
) -> TimedeltaIndex: ...
28 changes: 19 additions & 9 deletions pandas-stubs/core/tools/datetimes.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from datetime import datetime
from typing import (
Literal,
Sequence,
TypedDict,
Union,
overload,
)

import numpy as np
import pandas as pd
from pandas import (
Index,
Timestamp,
Expand All @@ -25,6 +27,7 @@ from pandas._typing import (
AnyArrayLike,
DateTimeErrorChoices,
IgnoreRaise,
npt,
)

ArrayConvertible: TypeAlias = Union[list, tuple, AnyArrayLike]
Expand Down Expand Up @@ -53,9 +56,6 @@ class FulldatetimeDict(YearMonthDayDict, total=False):

DictConvertible: TypeAlias = Union[FulldatetimeDict, DataFrame]

def should_cache(
arg: ArrayConvertible, unique_share: float = ..., check_count: int | None = ...
) -> bool: ...
@overload
def to_datetime(
arg: DatetimeScalar,
Expand All @@ -67,7 +67,7 @@ def to_datetime(
exact: bool = ...,
unit: str | None = ...,
infer_datetime_format: bool = ...,
origin=...,
origin: int | Literal["julian", "unix"] | pd.Timestamp = ...,
cache: bool = ...,
) -> Timestamp: ...
@overload
Expand All @@ -81,11 +81,12 @@ def to_datetime(
exact: bool = ...,
unit: str | None = ...,
infer_datetime_format: bool = ...,
origin=...,
origin: Literal["julian", "unix"] | pd.Timestamp = ...,
cache: bool = ...,
) -> Timestamp | NaTType: ...
@overload
def to_datetime(
# TODO: Test dataframe return type
arg: Series | DictConvertible,
errors: DateTimeErrorChoices = ...,
dayfirst: bool = ...,
Expand All @@ -95,12 +96,21 @@ def to_datetime(
exact: bool = ...,
unit: str | None = ...,
infer_datetime_format: bool = ...,
origin=...,
origin: int | Literal["julian", "unix"] | pd.Timestamp = ...,
cache: bool = ...,
) -> TimestampSeries: ...
@overload
def to_datetime(
arg: list | tuple | np.ndarray | Index | ExtensionArray,
# TODO: Test other types
arg: Sequence[int | float | datetime]
| list[str]
| tuple[int | float | str | datetime, ...]
| npt.NDArray[np.datetime64]
| npt.NDArray[np.str_]
| npt.NDArray[np.int_]
| npt.NDArray[np.float_]
| Index
| ExtensionArray,
errors: DateTimeErrorChoices = ...,
dayfirst: bool = ...,
yearfirst: bool = ...,
Expand All @@ -109,7 +119,7 @@ def to_datetime(
exact: bool = ...,
unit: str | None = ...,
infer_datetime_format: bool = ...,
origin=...,
# TODO: Origin needs int in pandas docs
origin: int | Literal["julian", "unix"] | pd.Timestamp = ...,
cache: bool = ...,
) -> DatetimeIndex: ...
def to_time(arg, format=..., infer_time_format: bool = ..., errors: str = ...): ...
24 changes: 15 additions & 9 deletions pandas-stubs/core/tools/timedeltas.pyi
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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: ...
8 changes: 7 additions & 1 deletion pandas-stubs/tseries/frequencies.pyi
Original file line number Diff line number Diff line change
@@ -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: ...
Loading