Skip to content

Update timestamps.pyi #818

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 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions pandas-stubs/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,6 @@ class Timestamp(datetime):
def days_in_month(self) -> int: ...
@property
def daysinmonth(self) -> int: ...
@property
def unit(self) -> str: ...
def as_unit(self, unit: str, round_ok: bool = ...) -> Timestamp: ...
Comment on lines +305 to +306
Copy link
Collaborator

Choose a reason for hiding this comment

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

The only possible values for the return of unit are Literal["s", "ms", "us", "ns"] In pandas, this is now defined in pandas._typing.py via:

TimeUnit = Literal["s", "ms", "us", "ns"]

So can you do the following:

  1. Add that line to pandas-stubs/_typing.pyi
  2. Make the return value of unit be TimeUnit
  3. Make the allowable values of unit in as_unit to be TimeUnit

You probably will have to change the test for ts.unit to use assert_type() testing against TimeUnit

If you want to go beyond that, the declaration Literal["s", "ms", "us", "ns"] appears in the following files and should be replaced with TimeUnit:

  • pandas-stubs/core/frame.pyi
  • pandas-stubs/core/indexes/datetimes.pyi
  • pandas-stubs/core/series.pyi
  • pandas-stubs/io/json/_json.pyi

But if you don't want to make that change, it's OK - I can just create a new issue for that once this PR is done.

3 changes: 3 additions & 0 deletions tests/test_scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,7 @@ def test_timestamp_properties() -> None:
check(assert_type(ts.dayofyear, int), int)
check(assert_type(ts.days_in_month, int), int)
check(assert_type(ts.daysinmonth, int), int)
check(assert_type(ts.unit, str), str)
check(assert_type(ts.is_leap_year, bool), bool)
check(assert_type(ts.is_month_end, bool), bool)
check(assert_type(ts.is_month_start, bool), bool)
Expand Down Expand Up @@ -1530,6 +1531,8 @@ def test_timestamp_misc_methods() -> None:
pd.Timestamp,
)

check(assert_type(ts2.as_unit('ns'), pd.Timestamp), pd.Timestamp)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
check(assert_type(ts2.as_unit('ns'), pd.Timestamp), pd.Timestamp)
check(assert_type(ts2.as_unit("ns"), pd.Timestamp), pd.Timestamp)

use the pre-commit hook. You need double quotes here.


check(assert_type(ts2.round("1s"), pd.Timestamp), pd.Timestamp)
check(assert_type(ts2.round("1s", ambiguous="raise"), pd.Timestamp), pd.Timestamp)
check(assert_type(ts2.round("1s", ambiguous=True), pd.Timestamp), pd.Timestamp)
Expand Down