@@ -58,7 +58,7 @@ from dateutil.relativedelta import relativedelta
58
58
from dateutil.parser import DEFAULTPARSER
59
59
60
60
from pytz.tzinfo import BaseTzInfo as _pytz_BaseTzInfo
61
- from pandas.compat import parse_date, string_types, iteritems, StringIO
61
+ from pandas.compat import parse_date, string_types, iteritems, StringIO, callable
62
62
63
63
import operator
64
64
import collections
@@ -640,22 +640,61 @@ class NaTType(_NaT):
640
640
def __long__ (self ):
641
641
return NPY_NAT
642
642
643
- def weekday (self ):
644
- return np.nan
645
-
646
- def toordinal (self ):
647
- return - 1
648
-
649
643
def __reduce__ (self ):
650
644
return (__nat_unpickle, (None , ))
651
645
646
+
652
647
fields = [' year' , ' quarter' , ' month' , ' day' , ' hour' ,
653
648
' minute' , ' second' , ' millisecond' , ' microsecond' , ' nanosecond' ,
654
649
' week' , ' dayofyear' , ' days_in_month' , ' daysinmonth' , ' dayofweek' ]
655
650
for field in fields:
656
651
prop = property(fget = lambda self : np.nan)
657
652
setattr (NaTType, field, prop)
658
653
654
+ # GH9513 NaT methods (except to_datetime64) to raise, return np.nan, or return NaT
655
+ # create functions that raise, for binding to NaTType
656
+ def _make_error_func (func_name ):
657
+ def f (*args , **kwargs ):
658
+ raise ValueError (" NaTType does not support " + func_name)
659
+ f.__name__ = func_name
660
+ return f
661
+
662
+ def _make_nat_func (func_name ):
663
+ def f (*args , **kwargs ):
664
+ return NaT
665
+ f.__name__ = func_name
666
+ return f
667
+
668
+ def _make_nan_func (func_name ):
669
+ def f (*args , **kwargs ):
670
+ return np.nan
671
+ f.__name__ = func_name
672
+ return f
673
+
674
+ _nat_methods = [' date' , ' now' , ' replace' , ' to_datetime' , ' today' ]
675
+
676
+ _nan_methods = [' weekday' , ' isoweekday' ]
677
+
678
+ _implemented_methods = [' to_datetime64' ]
679
+ _implemented_methods.extend(_nat_methods)
680
+ _implemented_methods.extend(_nan_methods)
681
+
682
+ for _method_name in _nat_methods:
683
+ # not all methods exist in all versions of Python
684
+ if hasattr (NaTType, _method_name):
685
+ setattr (NaTType, _method_name, _make_nat_func(_method_name))
686
+
687
+ for _method_name in _nan_methods:
688
+ if hasattr (NaTType, _method_name):
689
+ setattr (NaTType, _method_name, _make_nan_func(_method_name))
690
+
691
+ for _maybe_method_name in dir (NaTType):
692
+ _maybe_method = getattr (NaTType, _maybe_method_name)
693
+ if (callable (_maybe_method)
694
+ and not _maybe_method_name.startswith(" _" )
695
+ and _maybe_method_name not in _implemented_methods):
696
+ setattr (NaTType, _maybe_method_name, _make_error_func(_maybe_method_name))
697
+
659
698
def __nat_unpickle (*args ):
660
699
# return constant defined in the module
661
700
return NaT
0 commit comments