Friday, 20 August 2021

Can I use abstract methods to import file-specific formatting of (Python) pandas data?

I have a class FileSet with a method _process_series, which contains a bunch of if-elif blocks doing filetag-specific formatting of different pandas.Series:

    elif filetag == "EntityA":
        ps[filetag+"_Id"] = str(ps[filetag+"_Id"]).strip()
        ps[filetag+"_DateOfBirth"] = str(pd.to_datetime(ps[filetag+"_DateOfBirth"]).strftime('%Y-%m-%d')).strip()
        ps[filetag+"_FirstName"] = str(ps[filetag+"_FirstName"]).strip().capitalize()
        ps[filetag+"_LastName"] = str(ps[filetag+"_LastName"]).strip().capitalize()
        ps[filetag+"_Age"] = relativedelta(datetime.today(), datetime.strptime(ps[filetag+"_DateOfBirth"], "%Y-%m-%d")).years
        return ps

I'd like to define an abstract format method in the class and keep these blocks of formatting in separate modules that are imported when _process_series is called for a given filetag. Forgive the pseudo-code, but something like:

for tag in filetag:
    from my_formatters import tag+'_formatter' as fmt
    ps = self.format(pandas_series, fmt)
    return ps

And the module would contain the formatting block:

# my_formatters.EntityA_formatter
    ps[filetag+"_Id"] = str(ps[filetag+"_Id"]).strip()
    ps[filetag+"_DateOfBirth"] = str(pd.to_datetime(ps[filetag+"_DateOfBirth"]).strftime('%Y-%m-%d')).strip()
    ps[filetag+"_FirstName"] = str(ps[filetag+"_FirstName"]).strip().capitalize()
    ps[filetag+"_LastName"] = str(ps[filetag+"_LastName"]).strip().capitalize()
    ps[filetag+"_Age"] = relativedelta(datetime.today(), datetime.strptime(ps[filetag+"_DateOfBirth"], "%Y-%m-%d")).years
    return ps


from Can I use abstract methods to import file-specific formatting of (Python) pandas data?

No comments:

Post a Comment