How can I subclass from numpy datetime64 ? For instance using the standard datetime I can easily subclass:
import datetime as dt
class SubFromDateTime(dt.datetime):
def __new__(cls):
return dt.datetime.__new__(cls, 2012, 1, 1)
print type(SubFromDateTime())
>>>
<class '__main__.SubFromDateTime'>
However, using datetime64 the following always returns the datetime64 reference, not my class...
from numpy import datetime64
class SubFromDT64(datetime64):
def __new__(cls):
return datetime64.__new__(cls, '20120101')
print type(SubFromDT64())
>>>
<type 'numpy.datetime64'>
How can I fix this? I'd basically like to write a simple wrapper for datetime64, which allows me to add custom functions, such as obtaining the month of a given date with a simple .Month() method. With the upper example I can easily add methods, in the lower example it will never recognize my methods and thinks it's a datetime64 object.
I ended up subclassing ndarray which creates a datetime64 array. Works like a charm for my purposes. In case anyone is interested here the code: