-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Closed
Labels
EnhancementNeeds DiscussionRequires discussion from core team before further actionRequires discussion from core team before further actionTimedeltaTimedelta data typeTimedelta data typeTimestamppd.Timestamp and associated methodspd.Timestamp and associated methods
Description
Feature Type
-
Adding new functionality to pandas
-
Changing existing functionality in pandas
-
Removing existing functionality in pandas
Problem Description
The default constructors pd.Timestamp.__new__ and pd.Timedelta.__new__ can return NaT, which is a different type. This can lead to silent type errors, depending on the type-checker used. Consider the following example:
import numpy as np
from pandas import Timedelta, Timestamp
t: Timestamp = Timestamp("2024-04-29T18:00:00")
t2: Timestamp = Timestamp(np.datetime64("nat")) # actually NaTType!
dt: Timedelta = Timedelta(1, "h")
dt2: Timedelta = Timedelta(np.timedelta64("nat")) # actually NaTType!Type-checking results:
mypy --strict: No errors (w/ and w/opandas-stubs)pyrightwithuseLibraryCodeForTypes = true: 4 errors (only formally correct result)pyrightwithuseLibraryCodeForTypes = false: no errors
Feature Description
Introduce new constructors timestamp and timedelta (in analogy to how pyarrow does constructors), which are guaranteed to return pd.Timestamp and pd.Timedelta types, or raise an exception in the case when NaT is encountered.
Alternative Solutions
Split pd.NaT into two different types, Timestamp("NaT") and Timedelta("NaT") (as is the case in numpy), which are instances of the respective types. (#24983)
Additional Context
Metadata
Metadata
Assignees
Labels
EnhancementNeeds DiscussionRequires discussion from core team before further actionRequires discussion from core team before further actionTimedeltaTimedelta data typeTimedelta data typeTimestamppd.Timestamp and associated methodspd.Timestamp and associated methods