pythonpandasdatetimeindexdatetime64

Sublcassing Pandas DatetimeIndex


I am trying to create a custom datetime class in python to support a time series model. After reviewing some of the existing python libraries I found that the pandas DatetimeIndex class provides much of the functionality I am looking for, but I would still like to add some methods for my needs. I have tried to subclass the DatetimeIndex class with a custom init constructor and then call the super init constructor, but I can't seem to get this to work. Has anyone done this? It would be great to get some insight on how to accomplish this.

This is about the simplest example I can think of. Here is a class defined in a module called time:

from pandas import DatetimeIndex

class DatetimeSub(DatetimeInex):

    def __init___(initTime):

        super().__init__(initTime)

When I test the class in the console with the following code:

import numpy as np

from time import DatetimeSub

testTime =  TimeEfo2(np.arange('1985-01-01 12:00','1985-02-01 12:00', dtype="datetime64[D]"))

I get the following error:

TypeError: object.init() takes exactly one argument (the instance to initialize)

Which I don't understand because you can instantiate DatetimeIndex with a datetime64 array. This code works fine:

from pandas import DatetimeIndex

import numpy as np

dT = DatetimeIndex(np.arange('1985-01-01 12:00','1985-02-01 12:00', dtype="datetime64[D]"))

Thanks khuynh! That works. And how about if I wanted to create my own custom constructor such as:

from pandas import DatetimeIndex
import numpy as np

class DatetimeSub(DatetimeIndex):
    def __init__(self, bgnTime, endTime, unit = 'D'):
        # I am now constructing my numpy datetime64 array here:
        initTime = np.arange(bgnTime, endTime, dtype="datetime64[{}]".format(unit))
        # How do I call the super class to populate with the numpy array? This does not work:
        super().__init__(initTime)

Thanks!


Solution

  • The first argument to a constructor is always self. So if you want to pass in initTime as a second argument, you'll need to add it to the constructor like so:

    from pandas import DatetimeIndex
    import numpy as np
    
    class DatetimeSub(DatetimeIndex):
        def __init__(self, initTime):
            super().__init__()
    
    dt = DatetimeIndex(np.arange('1985-01-01 12:00','1985-02-01 12:00', dtype="datetime64[D]"))
    
    ds = DatetimeSub(np.arange('1985-01-01 12:00','1985-02-01 12:00', dtype="datetime64[D]"))
    
    print(dt)
    
    print(ds)
    

    output:

    DatetimeIndex(['1985-01-01', '1985-01-02', '1985-01-03', '1985-01-04',
                   '1985-01-05', '1985-01-06', '1985-01-07', '1985-01-08',
                   '1985-01-09', '1985-01-10', '1985-01-11', '1985-01-12',
                   '1985-01-13', '1985-01-14', '1985-01-15', '1985-01-16',
                   '1985-01-17', '1985-01-18', '1985-01-19', '1985-01-20',
                   '1985-01-21', '1985-01-22', '1985-01-23', '1985-01-24',
                   '1985-01-25', '1985-01-26', '1985-01-27', '1985-01-28',
                   '1985-01-29', '1985-01-30', '1985-01-31'],
                  dtype='datetime64[ns]', freq=None)
    DatetimeSub(['1985-01-01', '1985-01-02', '1985-01-03', '1985-01-04',
                 '1985-01-05', '1985-01-06', '1985-01-07', '1985-01-08',
                 '1985-01-09', '1985-01-10', '1985-01-11', '1985-01-12',
                 '1985-01-13', '1985-01-14', '1985-01-15', '1985-01-16',
                 '1985-01-17', '1985-01-18', '1985-01-19', '1985-01-20',
                 '1985-01-21', '1985-01-22', '1985-01-23', '1985-01-24',
                 '1985-01-25', '1985-01-26', '1985-01-27', '1985-01-28',
                 '1985-01-29', '1985-01-30', '1985-01-31'],
                dtype='datetime64[ns]', freq=None)