I am trying to subclass Pandas' DataFrame object.
class AbundanceFrame(pd.DataFrame):
'Subclass of DataFrame used for creating simulated dataset with various component timeseries'
def __init__(self, days,*args,**kw):
'Constructor for AbundanceFrame class, must pass index of timeseries'
super(AbundanceFrame,self).__init__(index = days,*args,**kw)
self.steps = 0
self.monotonic = 0
I have a number of other methods that add simulated time-series to the resulting AbundanceFrame. The resulting Abundance frame takes on this form:
I then want to apply poisson sampling noise to all data in the abundance frame.
def apply_poisson_noise(self,keys=False):
temp = self.copy()
#print type(temp)
if keys != False:
for key in keys:
temp[key] = np.random.poisson(self[key])
else:
temp = self.apply(np.random.poisson)
return temp
With the above I can create an AbundanceFrame without problems. However when I attempt to apply_poisson_noise() it returns a DataFrame and not an AbundanceFrame. I have been searching online and have not found a method for applying functions to DataFrames in place for pandas.
I would like to know how I can have this functionality and return an AbundanceFrame.
Thank you!
Solved the problem: (Building on the response of user4589964) In apply_poisson_noise() I just call the AbundanceFrame constructor and give it the calculated data.
from copy import deepcopy
class AbundanceFrame(pd.DataFrame):
'Subclass of DataFrame used for creating simulated dataset with various component timeseries'
def __init__(self, days,steps=0,monotonic=0,*args,**kw):
'Constructor for AbundanceFrame class, must pass index of timeseries'
super(AbundanceFrame,self).__init__(index = days,*args,**kw)
self.steps = steps
self.monotonic = monotonic
def apply_poisson_noise(self,keys=False):
temp = deepcopy(self)
if keys != False:
for key in keys:
temp[key] = np.random.poisson(self[key])
temp = AbundanceFrame(self.index, columns=self.columns, data = temp.values,
steps=self.steps, monotonic=self.monotonic)
else:
temp = AbundanceFrame(self.index, columns=self.columns, data = self.apply(np.random.poisson),
steps=self.steps, monotonic=self.monotonic)
return temp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.