You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
pyright with useLibraryCodeForTypes = 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)
These constructors can be very simple wrappers, a rough sketch:
deftimedelta(value: Any= ..., unit: Optional[str] =None, **kwargs: Any) ->Timedelta:
"""Utility function that ensures that the constructor does not return NaT."""td= (
Timedelta(unit=unit, **kwargs)
ifvalueisEllipsiselseTimedelta(value, unit=unit, **kwargs)
)
ifisinstance(td, NaTType):
raiseValueError("Constructor returned NaT")
returntddeftimestamp(value: Any= ..., **kwargs: Any) ->Timestamp:
"""Utility function that ensures that the constructor does not return NaT."""ts=Timestamp(**kwargs) ifvalueisEllipsiselseTimestamp(value, **kwargs)
ifisinstance(ts, NaTType):
raiseValueError("Constructor returned NaT")
returnts
There’s an issue about introducing a separate NaTD specific to Timedelta. If you did that ( and the same for Period), then NaT could become a Timestamp, and you would get type-safety in the constructors without new constructors
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__
andpd.Timedelta.__new__
can returnNaT
, which is a different type. This can lead to silent type errors, depending on the type-checker used. Consider the following example:Type-checking results:
mypy --strict
: No errors (w/ and w/opandas-stubs
)pyright
withuseLibraryCodeForTypes = true
: 4 errors (only formally correct result)pyright
withuseLibraryCodeForTypes = false
: no errorsFeature Description
Introduce new constructors
timestamp
andtimedelta
(in analogy to howpyarrow
does constructors), which are guaranteed to returnpd.Timestamp
andpd.Timedelta
types, or raise an exception in the case whenNaT
is encountered.Alternative Solutions
Split
pd.NaT
into two different types,Timestamp("NaT")
andTimedelta("NaT")
(as is the case innumpy
), which are instances of the respective types. (#24983)Additional Context
The text was updated successfully, but these errors were encountered: