samedi 12 octobre 2019

provide alternative api to Python class via property-like redirection

This is a general Python design question, but to be specific I'll be talking about pandas.DataFrame class and its unstack method.

My goal is to provide an alternative version of unstack, however I don't want to override the original version, rather provide a way to access my version via .x redirection layer:

import pandas as pd
dat = pd.DataFrame(some args)
# call my function
dat.x.unstack()
# call original function
dat.unstack()

A design requirement is that the implementation of my version should be able to call the original unstack.

For the sake of this question one can assume I'm allowed to add a little bit of code to pandas.DataFrame, but that addition should be minimal given it's a public package. A fully non-intrusive solution would be a plus.

Aucun commentaire:

Enregistrer un commentaire