BUG/ENH: cleanup for Timestamp arithmetic by shoyer · Pull Request #8916 · pandas-dev/pandas (original) (raw)
So _typ
is needed if you need type comparisons (isinstance) in cython when you can't import things (well you can, but you don' generally want to), e.g. TimedeltaIndex and such.
However you eliminated the need for much of that, so +1 (though I think you went too far and now accept clear wrong types, e.g. dti + dti which must raise a TypeError)
Need to check return types on a matrix of operations, they should all be pandas types (Timestamp/Timedelta/TimedeltaIndex/DateIndex), when operated with pandas types (e.g. a np.datetime64 + np.timedelta64) obviously will yield a np.datetime64
So need to wrap all return ops (which is a pass thru if its the right type already). The only wrinkle is that you need to determine array/scalars (I think simple enough to detect if one of the operands is an array-like (e.g. has a dtype), then you return DatetimeIndex/TimedeltaIndex).
I think this needs a systematic test (their are some in tseries/test_base, so can prob just add on
And fully-compare e.g. don't just compare the values, but test the types as well
ts = Timestamp('20130101')
In [5]: ts-pd.to_datetime(['1999-12-31']).values
Out[5]: array([86400000000000], dtype='timedelta64[ns]')
This must raise a TypeError. It is a clear operation that should simply not be allowed. This is the point of a TypeError.
In [6]: pd.date_range('20130101',periods=3)+ts
NotImplementedError:
Furthermore Series / Timedelta / Timestamp ops need to be consistent with these. So need to matrix tests these (these raise the appropriate TypeErrors). I am still big -1 on ALWAYS raising NotImplementedError. This is just not very useful. So i'd like you to enumerate all the cases so they can be discussed (e.g. td + tdi, td - tdi, ts + tdi)....etc. Indicate where you would put NotImplementedError and TypeError, etc.
We still have the side issue of:
dti+dti, dti-dti 'work' but are set operations. need to think about this again.