@@ -317,50 +317,56 @@ def _add_datelike(self, other):
317
317
def _sub_datelike (self , other ):
318
318
raise NotImplementedError
319
319
320
- def __add__ (self , other ):
321
- from pandas .core .index import Index
322
- from pandas .tseries .tdi import TimedeltaIndex
323
- from pandas .tseries .offsets import DateOffset
324
- if isinstance (other , TimedeltaIndex ):
325
- return self ._add_delta (other )
326
- elif isinstance (self , TimedeltaIndex ) and isinstance (other , Index ):
327
- if hasattr (other ,'_add_delta' ):
328
- return other ._add_delta (self )
329
- raise TypeError ("cannot add TimedeltaIndex and {typ}" .format (typ = type (other )))
330
- elif isinstance (other , Index ):
331
- return self .union (other )
332
- elif isinstance (other , (DateOffset , timedelta , np .timedelta64 , tslib .Timedelta )):
333
- return self ._add_delta (other )
334
- elif com .is_integer (other ):
335
- return self .shift (other )
336
- elif isinstance (other , (tslib .Timestamp , datetime )):
337
- return self ._add_datelike (other )
338
- else : # pragma: no cover
339
- return NotImplemented
340
-
341
- def __sub__ (self , other ):
342
- from pandas .core .index import Index
343
- from pandas .tseries .tdi import TimedeltaIndex
344
- from pandas .tseries .offsets import DateOffset
345
- if isinstance (other , TimedeltaIndex ):
346
- return self ._add_delta (- other )
347
- elif isinstance (self , TimedeltaIndex ) and isinstance (other , Index ):
348
- if not isinstance (other , TimedeltaIndex ):
349
- raise TypeError ("cannot subtract TimedeltaIndex and {typ}" .format (typ = type (other )))
350
- return self ._add_delta (- other )
351
- elif isinstance (other , Index ):
352
- return self .difference (other )
353
- elif isinstance (other , (DateOffset , timedelta , np .timedelta64 , tslib .Timedelta )):
354
- return self ._add_delta (- other )
355
- elif com .is_integer (other ):
356
- return self .shift (- other )
357
- elif isinstance (other , (tslib .Timestamp , datetime )):
358
- return self ._sub_datelike (other )
359
- else : # pragma: no cover
360
- return NotImplemented
361
-
362
- __iadd__ = __add__
363
- __isub__ = __sub__
320
+ @classmethod
321
+ def _add_datetimelike_methods (cls ):
322
+ """ add in the datetimelike methods (as we may have to override the superclass) """
323
+
324
+ def __add__ (self , other ):
325
+ from pandas .core .index import Index
326
+ from pandas .tseries .tdi import TimedeltaIndex
327
+ from pandas .tseries .offsets import DateOffset
328
+ if isinstance (other , TimedeltaIndex ):
329
+ return self ._add_delta (other )
330
+ elif isinstance (self , TimedeltaIndex ) and isinstance (other , Index ):
331
+ if hasattr (other ,'_add_delta' ):
332
+ return other ._add_delta (self )
333
+ raise TypeError ("cannot add TimedeltaIndex and {typ}" .format (typ = type (other )))
334
+ elif isinstance (other , Index ):
335
+ return self .union (other )
336
+ elif isinstance (other , (DateOffset , timedelta , np .timedelta64 , tslib .Timedelta )):
337
+ return self ._add_delta (other )
338
+ elif com .is_integer (other ):
339
+ return self .shift (other )
340
+ elif isinstance (other , (tslib .Timestamp , datetime )):
341
+ return self ._add_datelike (other )
342
+ else : # pragma: no cover
343
+ return NotImplemented
344
+ cls .__add__ = __add__
345
+
346
+ def __sub__ (self , other ):
347
+ from pandas .core .index import Index
348
+ from pandas .tseries .tdi import TimedeltaIndex
349
+ from pandas .tseries .offsets import DateOffset
350
+ if isinstance (other , TimedeltaIndex ):
351
+ return self ._add_delta (- other )
352
+ elif isinstance (self , TimedeltaIndex ) and isinstance (other , Index ):
353
+ if not isinstance (other , TimedeltaIndex ):
354
+ raise TypeError ("cannot subtract TimedeltaIndex and {typ}" .format (typ = type (other )))
355
+ return self ._add_delta (- other )
356
+ elif isinstance (other , Index ):
357
+ return self .difference (other )
358
+ elif isinstance (other , (DateOffset , timedelta , np .timedelta64 , tslib .Timedelta )):
359
+ return self ._add_delta (- other )
360
+ elif com .is_integer (other ):
361
+ return self .shift (- other )
362
+ elif isinstance (other , (tslib .Timestamp , datetime )):
363
+ return self ._sub_datelike (other )
364
+ else : # pragma: no cover
365
+ return NotImplemented
366
+ cls .__sub__ = __sub__
367
+
368
+ cls .__iadd__ = __add__
369
+ cls .__isub__ = __sub__
364
370
365
371
def _add_delta (self , other ):
366
372
return NotImplemented
@@ -469,5 +475,3 @@ def repeat(self, repeats, axis=None):
469
475
"""
470
476
return self ._simple_new (self .values .repeat (repeats ),
471
477
name = self .name )
472
-
473
-
0 commit comments