@@ -854,68 +854,6 @@ class NaTType(_NaT):
854854 return NotImplemented
855855
856856
857- fields = [' year' , ' quarter' , ' month' , ' day' , ' hour' ,
858- ' minute' , ' second' , ' millisecond' , ' microsecond' , ' nanosecond' ,
859- ' week' , ' dayofyear' , ' days_in_month' , ' daysinmonth' , ' dayofweek' ,
860- ' weekday_name' ]
861- for field in fields:
862- prop = property(fget = lambda self : np.nan)
863- setattr (NaTType, field, prop)
864-
865-
866- # GH9513 NaT methods (except to_datetime64) to raise, return np.nan, or
867- # return NaT create functions that raise, for binding to NaTType
868- def _make_error_func (func_name ):
869- def f (*args , **kwargs ):
870- raise ValueError (" NaTType does not support " + func_name)
871- f.__name__ = func_name
872- return f
873-
874-
875- def _make_nat_func (func_name ):
876- def f (*args , **kwargs ):
877- return NaT
878- f.__name__ = func_name
879- return f
880-
881-
882- def _make_nan_func (func_name ):
883- def f (*args , **kwargs ):
884- return np.nan
885- f.__name__ = func_name
886- return f
887-
888-
889- # GH14940
890- _round_methods = [' round' , ' floor' , ' ceil' ]
891-
892- _nat_methods = [' date' , ' now' , ' replace' , ' to_pydatetime' , ' today' ]
893- _nat_methods.extend(_round_methods)
894-
895- _nan_methods = [' weekday' , ' isoweekday' , ' total_seconds' ]
896-
897- _implemented_methods = [' to_datetime' , ' to_datetime64' , ' isoformat' ]
898- _implemented_methods.extend(_nat_methods)
899- _implemented_methods.extend(_nan_methods)
900-
901- for _method_name in _nat_methods:
902- # not all methods exist in all versions of Python
903- if hasattr (NaTType, _method_name) or _method_name in _round_methods:
904- setattr (NaTType, _method_name, _make_nat_func(_method_name))
905-
906- for _method_name in _nan_methods:
907- if hasattr (NaTType, _method_name):
908- setattr (NaTType, _method_name, _make_nan_func(_method_name))
909-
910- for _maybe_method_name in dir (NaTType):
911- _maybe_method = getattr (NaTType, _maybe_method_name)
912- if (callable (_maybe_method)
913- and not _maybe_method_name.startswith(" _" )
914- and _maybe_method_name not in _implemented_methods):
915- setattr (NaTType, _maybe_method_name,
916- _make_error_func(_maybe_method_name))
917-
918-
919857def __nat_unpickle (*args ):
920858 # return constant defined in the module
921859 return NaT
@@ -1417,22 +1355,6 @@ cdef class _NaT(_Timestamp):
14171355 return NotImplemented
14181356
14191357
1420- def _delta_to_nanoseconds (delta ):
1421- if isinstance (delta, np.ndarray):
1422- return delta.astype(' m8[ns]' ).astype(' int64' )
1423- if hasattr (delta, ' nanos' ):
1424- return delta.nanos
1425- if hasattr (delta, ' delta' ):
1426- delta = delta.delta
1427- if is_timedelta64_object(delta):
1428- return delta.astype(" timedelta64[ns]" ).item()
1429- if is_integer_object(delta):
1430- return delta
1431- return (delta.days * 24 * 60 * 60 * 1000000
1432- + delta.seconds * 1000000
1433- + delta.microseconds) * 1000
1434-
1435-
14361358# lightweight C object to hold datetime & int64 pair
14371359cdef class _TSObject:
14381360 cdef:
@@ -3803,6 +3725,112 @@ def array_strptime(ndarray[object] values, object fmt,
38033725 return result
38043726
38053727
3728+ # ----------------------------------------------------------------------
3729+ # NaT methods/property setups
3730+
3731+
3732+ # inject the Timestamp field properties
3733+ # these by definition return np.nan
3734+ fields = [' year' , ' quarter' , ' month' , ' day' , ' hour' ,
3735+ ' minute' , ' second' , ' millisecond' , ' microsecond' , ' nanosecond' ,
3736+ ' week' , ' dayofyear' , ' days_in_month' , ' daysinmonth' , ' dayofweek' ,
3737+ ' weekday_name' ]
3738+ for field in fields:
3739+ prop = property(fget = lambda self : np.nan)
3740+ setattr (NaTType, field, prop)
3741+
3742+
3743+ # define how we are handling NaT methods & inject
3744+ # to the NaTType class; these can return NaT, np.nan
3745+ # or raise respectively
3746+ _nat_methods = [' date' , ' now' , ' replace' , ' to_pydatetime' ,
3747+ ' today' , ' round' , ' floor' , ' ceil' ]
3748+ _nan_methods = [' weekday' , ' isoweekday' , ' total_seconds' ]
3749+ _implemented_methods = [' to_datetime' , ' to_datetime64' , ' isoformat' ]
3750+ _implemented_methods.extend(_nat_methods)
3751+ _implemented_methods.extend(_nan_methods)
3752+
3753+
3754+ def _get_docstring (_method_name ):
3755+ # NaT serves double duty as Timestamp & Timedelta
3756+ # missing value, so need to acquire doc-strings for both
3757+
3758+ try :
3759+ return getattr (Timestamp, _method_name).__doc__
3760+ except AttributeError :
3761+ pass
3762+
3763+ try :
3764+ return getattr (Timedelta, _method_name).__doc__
3765+ except AttributeError :
3766+ pass
3767+
3768+ return None
3769+
3770+
3771+ for _method_name in _nat_methods:
3772+
3773+ def _make_nat_func (func_name ):
3774+ def f (*args , **kwargs ):
3775+ return NaT
3776+ f.__name__ = func_name
3777+ f.__doc__ = _get_docstring(_method_name)
3778+ return f
3779+
3780+ setattr (NaTType, _method_name, _make_nat_func(_method_name))
3781+
3782+
3783+ for _method_name in _nan_methods:
3784+
3785+ def _make_nan_func (func_name ):
3786+ def f (*args , **kwargs ):
3787+ return np.nan
3788+ f.__name__ = func_name
3789+ f.__doc__ = _get_docstring(_method_name)
3790+ return f
3791+
3792+ setattr (NaTType, _method_name, _make_nan_func(_method_name))
3793+
3794+
3795+ # GH9513 NaT methods (except to_datetime64) to raise, return np.nan, or
3796+ # return NaT create functions that raise, for binding to NaTType
3797+ for _maybe_method_name in dir (NaTType):
3798+ _maybe_method = getattr (NaTType, _maybe_method_name)
3799+ if (callable (_maybe_method)
3800+ and not _maybe_method_name.startswith(" _" )
3801+ and _maybe_method_name not in _implemented_methods):
3802+
3803+ def _make_error_func (func_name ):
3804+ def f (*args , **kwargs ):
3805+ raise ValueError (" NaTType does not support " + func_name)
3806+ f.__name__ = func_name
3807+ f.__doc__ = _get_docstring(_method_name)
3808+ return f
3809+
3810+ setattr (NaTType, _maybe_method_name,
3811+ _make_error_func(_maybe_method_name))
3812+
3813+
3814+ # ----------------------------------------------------------------------
3815+ # Conversion routines
3816+
3817+
3818+ def _delta_to_nanoseconds (delta ):
3819+ if isinstance (delta, np.ndarray):
3820+ return delta.astype(' m8[ns]' ).astype(' int64' )
3821+ if hasattr (delta, ' nanos' ):
3822+ return delta.nanos
3823+ if hasattr (delta, ' delta' ):
3824+ delta = delta.delta
3825+ if is_timedelta64_object(delta):
3826+ return delta.astype(" timedelta64[ns]" ).item()
3827+ if is_integer_object(delta):
3828+ return delta
3829+ return (delta.days * 24 * 60 * 60 * 1000000
3830+ + delta.seconds * 1000000
3831+ + delta.microseconds) * 1000
3832+
3833+
38063834cdef inline _get_datetime64_nanos(object val):
38073835 cdef:
38083836 pandas_datetimestruct dts
@@ -3891,9 +3919,6 @@ def cast_to_nanoseconds(ndarray arr):
38913919
38923920 return result
38933921
3894- # ----------------------------------------------------------------------
3895- # Conversion routines
3896-
38973922
38983923def pydt_to_i8 (object pydt ):
38993924 """
0 commit comments