Package PyFoam :: Package Wrappers :: Module Pandas
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Wrappers.Pandas

  1  #  ICE Revision: $Id$ 
  2  """Extended version of the Pandas-Dataframe 
  3  """ 
  4   
  5  from pandas import DataFrame,Series 
  6  from numpy import hstack,unique 
  7  from math import isnan 
  8   
  9  from PyFoam.Error import error,warning,PyFoamException 
 10   
 11  from PyFoam.ThirdParty.six import string_types,text_type,u 
 12   
13 -class PyFoamDataFrame(DataFrame):
14 """This class adds some convenience functions to the regular Datafram class""" 15 16 validOtherTypes=(DataFrame,Series) 17
18 - def __init__(self,*args,**kwargs):
19 """Adds no data. Just passes the arguments to the super-class""" 20 super(PyFoamDataFrame,self).__init__(*args,**kwargs) 21 if not self.__allStrings(): 22 raise PandasWrapperPyFoamException("Columns must be strings")
23 # if not self.axes[0].is_unique: 24 # # not working as expected 25 # # self.drop_duplicates(inplace=True) 26 # self.axes[0].is_unique=True 27
28 - def __allStrings(self,keys=None):
29 if keys==None: 30 keys=self.keys() 31 return keys.map(lambda k:isinstance(k,string_types)).all()
32
33 - def addData(self,other, 34 sameIndex=True, 35 mergeIndex=False, 36 prefix=None, 37 suffix=None, 38 allowExtrapolate=False, 39 interpolationMethod="values"):
40 """Add data from another DataFrame or Series 41 @param other: data as Pandas-DataFrame or Series 42 @param sameIndex: assum both have the same indices. If False the other data will be interpolated to the current indices 43 @param mergeIndex: make the result indices a mixture of the indices""" 44 if not sameIndex and mergeIndex: 45 raise PandasWrapperPyFoamException("Can't specify sameIndex=False and mergeIndex=True at the same time") 46 if not isinstance(other,self.validOtherTypes): 47 raise PandasWrapperPyFoamException("Other data is of type",type(other), 48 "should be one of",self.validOtherTypes) 49 if isinstance(other,DataFrame): 50 o=other 51 else: 52 o=DataFrame(other) 53 54 k=o.keys() 55 if not self.__allStrings(k): 56 raise PandasWrapperPyFoamException("Added data with non-string columns") 57 v=k.copy() 58 if prefix: 59 v=[prefix+n for n in v] 60 if suffix: 61 v=[n+suffix for n in v] 62 if len(set(v)&set(self.keys()))>0: 63 raise PandasWrapperPyFoamException("Keys of this",self.keys(),"and other",v, 64 "intersect",set(v)&set(self.keys())) 65 keys=dict(zip(k,v)) 66 interpolate=False # only interpolate if necessary 67 if len(self.index)!=len(o.index) or (self.index!=o.index).any(): 68 if sameIndex and not mergeIndex: 69 raise PandasWrapperPyFoamException("Other data has different index. Specify sameIndex=False or mergeIndex=True") 70 ni=unique(hstack([self.index,o.index])) 71 interpolate=True 72 if mergeIndex: 73 minOld=min(self.index) 74 maxOld=max(self.index) 75 76 result=self.reindex(index=ni,copy=False).interpolate( 77 method=interpolationMethod) 78 79 if not allowExtrapolate: 80 result[result.index<minOld]=float("NaN") 81 result[result.index>maxOld]=float("NaN") 82 else: 83 # make sure we have values at the current position 84 # o=o.reindex_axis(ni,axis='index').interpolate(method=interpolationMethod) 85 o=o.reindex(index=ni,columns=o.columns).interpolate(method=interpolationMethod) 86 # ,takeable=True 87 result=self.copy() 88 else: 89 result=self.copy() 90 91 minOld=min(o.index) 92 maxOld=max(o.index) 93 for k,v in keys.items(): 94 result[v]=o[k] 95 if interpolate: 96 result[v]=result[v].interpolate(method=interpolationMethod) 97 if not allowExtrapolate: 98 result[v][result.index<minOld]=float("NaN") 99 result[v][result.index>maxOld]=float("NaN") 100 101 return PyFoamDataFrame(result)
102
103 - def integrate(self,columns=None):
104 """Integrate by using the trapezoid rule. Return a dictionary with values. 105 @param values: list of column names. If unset all are integrated""" 106 return self.__integrateInternal(columns)[0]
107
108 - def validLength(self,columns=None):
109 """Length were the values are valid (not NaN) Return a dictionary with values. 110 @param values: list of column names. If unset all are integrated""" 111 return self.__integrateInternal(columns)[1]
112
113 - def weightedAverage(self,columns=None):
114 """Weighted average. Return a dictionary with values. 115 @param values: list of column names. If unset all are integrated""" 116 integral,length=self.__integrateInternal(columns) 117 result={} 118 for k in integral: 119 if length[k]>0 and not isnan(length[k]): 120 result[k]=integral[k]/length[k] 121 else: 122 result[k]=float("NaN") 123 return result
124
125 - def __integrateInternal(self,columns):
126 if columns==None: 127 columns=self.keys() 128 integrals={} 129 lengths={} 130 ind=self.index 131 132 for k in columns: 133 integrals[k]=0 134 lengths[k]=0 135 if len(ind)<2: # no weighting possible 136 integrals[k]=float("NaN") 137 continue 138 val=self[k].values 139 for i in range(len(ind)): 140 if not isnan(val[i]): 141 w=0 142 if i>0: 143 w+=0.5*(ind[i]-ind[i-1]) 144 if i+1<len(ind): 145 w+=0.5*(ind[i+1]-ind[i]) 146 lengths[k]+=w 147 integrals[k]+=w*val[i] 148 if lengths[k]==0: 149 integrals[k]=float("NaN") 150 151 return integrals,lengths
152
153 - def describe(self,*args,**kwargs):
154 """Adds our own statistics to the regular describe""" 155 d=super(PyFoamDataFrame,self).describe(*args,**kwargs) 156 integral,length=self.__integrateInternal(self.keys()) 157 d=d.append(DataFrame(data=integral,index=["integral"])) 158 d=d.append(DataFrame(data=length,index=["valid length"])) 159 a={} 160 for k in integral: 161 if length[k]>0 and not isnan(length[k]): 162 a[k]=integral[k]/length[k] 163 else: 164 a[k]=float("NaN") 165 d=d.append(DataFrame(data=a,index=["weighted average"])) 166 return d
167
168 -class PandasWrapperPyFoamException(PyFoamException):
169 """The PyFoam-exception that does not expect to be caught""" 170
171 - def __init__(self,*text):
172 descr="Problem in wrapper to pandas-library" 173 # super(FatalErrorPyFoamException,self).__init__(descr,*text) # does not work with Python 2.4 174 PyFoamException.__init__(self,descr,*text)
175