classPandasField(Model,arbitrary_types_allowed=True):"""Pandas Field model"""name:strdtype:Anydvalue:Optional[Any]=None# Convertersdefto_dp(self)->models.IField:"""Convert to Table Schema Field Returns: Table Schema Field """# TypeField=models.Fieldifpdc.is_bool_dtype(self.dtype):# type: ignoreField=models.BooleanFieldelifpdc.is_datetime64_any_dtype(self.dtype):# type: ignoreField=models.DatetimeFieldelifpdc.is_integer_dtype(self.dtype):# type: ignoreField=models.IntegerFieldelifpdc.is_numeric_dtype(self.dtype):# type: ignoreField=models.NumberFieldelifself.dvalueisnotNone:ifisinstance(self.dvalue,(list,tuple)):# type: ignoreField=models.ArrayFieldelifisinstance(self.dvalue,datetime.datetime):Field=models.DatetimeFieldelifisinstance(self.dvalue,datetime.date):Field=models.DateFieldelifisinstance(self.dvalue,isodate.Duration):# type: ignoreField=models.DurationFieldelifisinstance(self.dvalue,dict):Field=models.ObjectFieldelifisinstance(self.dvalue,str):Field=models.StringFieldelifisinstance(self.dvalue,datetime.time):Field=models.TimeField# Namefield=Field(name=self.name)returnfield@classmethoddeffrom_dp(cls,field:models.IField)->PandasField:"""Create Pandas Field from Table Schema Field Parameters: field: Table Schema Field Returns: Pandas Field """ifnotfield.name:raiseError(f"Field name is required to convert to pandas: {field}")# Typedtype=np.dtype("O")iffield.type=="array":dtype=np.dtype(list)# type: ignoreeliffield.type=="boolean":dtype=np.dtype(bool)eliffield.type=="datetime":dtype=pd.DatetimeTZDtype(tz="UTC")eliffield.type=="integer":dtype=np.dtype(int)eliffield.type=="geojson":dtype=np.dtype(dict)eliffield.type=="number":dtype=np.dtype(float)eliffield.type=="object":dtype=np.dtype(dict)eliffield.type=="string":dtype=np.dtype(str)eliffield.type=="year":dtype=np.dtype(int)returnPandasField(name=field.name,dtype=dtype)
@classmethoddeffrom_dp(cls,field:models.IField)->PandasField:"""Create Pandas Field from Table Schema Field Parameters: field: Table Schema Field Returns: Pandas Field """ifnotfield.name:raiseError(f"Field name is required to convert to pandas: {field}")# Typedtype=np.dtype("O")iffield.type=="array":dtype=np.dtype(list)# type: ignoreeliffield.type=="boolean":dtype=np.dtype(bool)eliffield.type=="datetime":dtype=pd.DatetimeTZDtype(tz="UTC")eliffield.type=="integer":dtype=np.dtype(int)eliffield.type=="geojson":dtype=np.dtype(dict)eliffield.type=="number":dtype=np.dtype(float)eliffield.type=="object":dtype=np.dtype(dict)eliffield.type=="string":dtype=np.dtype(str)eliffield.type=="year":dtype=np.dtype(int)returnPandasField(name=field.name,dtype=dtype)