Skip to content

remove np.datetime64 from S1, make adding two TimestampSeries fail #527

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 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions pandas-stubs/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,15 @@ class Timestamp(datetime):
@overload
def __eq__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc] # pyright: ignore[reportOverlappingOverload]
@overload
def __eq__(self, other: TimestampSeries | Series[np.datetime64]) -> Series[bool]: ... # type: ignore[misc]
def __eq__(self, other: TimestampSeries) -> Series[bool]: ... # type: ignore[misc]
@overload
def __eq__(self, other: npt.NDArray[np.datetime64] | Index) -> np_ndarray_bool: ... # type: ignore[misc]
@overload
def __eq__(self, other: object) -> Literal[False]: ...
@overload
def __ne__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc] # pyright: ignore[reportOverlappingOverload]
@overload
def __ne__(self, other: TimestampSeries | Series[np.datetime64]) -> Series[bool]: ... # type: ignore[misc]
def __ne__(self, other: TimestampSeries) -> Series[bool]: ... # type: ignore[misc]
@overload
def __ne__(self, other: npt.NDArray[np.datetime64] | Index) -> np_ndarray_bool: ... # type: ignore[misc]
@overload
Expand Down
1 change: 0 additions & 1 deletion pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ S1 = TypeVar(
complex,
Timestamp,
Timedelta,
np.datetime64,
Period,
Interval[int],
Interval[float],
Expand Down
12 changes: 11 additions & 1 deletion pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ class _LocIndexerSeries(_LocIndexer, Generic[S1]):
) -> None: ...

class Series(IndexOpsMixin, NDFrame, Generic[S1]):

_ListLike: TypeAlias = Union[ArrayLike, dict[_str, np.ndarray], list, tuple, Index]
__hash__: ClassVar[None]

Expand All @@ -193,6 +192,16 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
fastpath: bool = ...,
) -> TimestampSeries: ...
@overload
def __new__(
cls,
data: _ListLike,
dtype: Literal["datetime64[ns]"],
index: Axes | None = ...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> TimestampSeries: ...
@overload
def __new__(
cls,
data: PeriodIndex,
Expand Down Expand Up @@ -1800,6 +1809,7 @@ class TimestampSeries(Series[Timestamp]):
@property
def dt(self) -> TimestampProperties: ... # type: ignore[override]
def __add__(self, other: TimedeltaSeries | np.timedelta64) -> TimestampSeries: ... # type: ignore[override]
def __radd__(self, other: TimedeltaSeries | np.timedelta64) -> TimestampSeries: ... # type: ignore[override]
def __mul__(self, other: int | float | Series[int] | Series[float] | Sequence[int | float]) -> TimestampSeries: ... # type: ignore[override]
def __truediv__(self, other: int | float | Series[int] | Series[float] | Sequence[int | float]) -> TimestampSeries: ... # type: ignore[override]
def mean( # type: ignore[override]
Expand Down
7 changes: 1 addition & 6 deletions tests/test_scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

from pandas._typing import np_ndarray_bool
else:

np_ndarray_bool = npt.NDArray[np.bool_]
TimedeltaSeries: TypeAlias = pd.Series
TimestampSeries: TypeAlias = pd.Series
Expand Down Expand Up @@ -386,7 +385,6 @@ def test_interval_cmp():


def test_timedelta_construction() -> None:

check(assert_type(pd.Timedelta(1, "H"), pd.Timedelta), pd.Timedelta)
check(assert_type(pd.Timedelta(1, "T"), pd.Timedelta), pd.Timedelta)
check(assert_type(pd.Timedelta(1, "S"), pd.Timedelta), pd.Timedelta)
Expand Down Expand Up @@ -1078,7 +1076,6 @@ def test_timedelta_cmp_rhs() -> None:


def test_timestamp_construction() -> None:

check(assert_type(pd.Timestamp("2000-1-1"), pd.Timestamp), pd.Timestamp)
check(
assert_type(pd.Timestamp("2000-1-1", tz="US/Pacific"), pd.Timestamp),
Expand Down Expand Up @@ -1256,9 +1253,7 @@ def test_timestamp_cmp() -> None:
c_dt_datetime = dt.datetime(year=2000, month=1, day=1)
c_datetimeindex = pd.DatetimeIndex(["2000-1-1"])
c_np_ndarray_dt64 = np_dt64_arr
c_series_dt64: pd.Series[np.datetime64] = pd.Series(
[1, 2, 3], dtype="datetime64[ns]"
)
c_series_dt64: TimestampSeries = pd.Series([1, 2, 3], dtype="datetime64[ns]")
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to specify dtype=np.datetime64?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No. This is consistent with numpy.

>>> import pandas as pd
>>> import numpy as np
>>> pd.Series([1,2,3], dtype=np.datetime64)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\series.py", line 470, in __init__
    data = sanitize_array(data, index, dtype, copy)
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\construction.py", line 622, in sanitize_array
    subarr = _try_cast(data, dtype, copy, raise_cast_failure)
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\construction.py", line 827, in _try_cast
    return maybe_cast_to_datetime(arr, dtype)
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\dtypes\cast.py", line 1321, in maybe_cast_to_datetime
    dtype = _ensure_nanosecond_dtype(dtype)
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\dtypes\cast.py", line 1468, in _ensure_nanosecond_dtype
    raise ValueError(msg)
ValueError: The 'datetime64' dtype has no unit. Please pass in 'datetime64[ns]' instead.

c_series_timestamp = pd.Series(pd.DatetimeIndex(["2000-1-1"]))
check(assert_type(c_series_timestamp, TimestampSeries), pd.Series, pd.Timestamp)
# Use xor to ensure one is True and the other is False
Expand Down
2 changes: 1 addition & 1 deletion tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def test_fail_on_adding_two_timestamps() -> None:
s1 = pd.Series(pd.to_datetime(["2022-05-01", "2022-06-01"]))
s2 = pd.Series(pd.to_datetime(["2022-05-15", "2022-06-15"]))
if TYPE_CHECKING_INVALID_USAGE:
ssum: pd.Series = s1 + s2 # type: ignore[operator]
ssum: pd.Series = s1 + s2 # type: ignore[operator] # pyright: ignore[reportGeneralTypeIssues]
ts = pd.Timestamp("2022-06-30")
tsum: pd.Series = s1 + ts # type: ignore[operator] # pyright: ignore[reportGeneralTypeIssues]

Expand Down