I have written a small python package which extends a pandas dataframe with a few additional methods.
At the moment, I have this code in my package:
def init():
@pd.api.extensions.register_dataframe_accessor("test")
class _:
def __init__(self, pandas_obj):
self._obj = pandas_obj
def myMethod(self):
pass
I then do the following in python:
import pandas as pd
import mypackage as mp
mp.init()
test = pd.Dataframe(<define data frame>)
test.mp.myMethod()
My question is, is it possible to do the pandas import and register the accessor from within the __init__.py in mypackage, so that once mypackage is imported, I automatically have access to mymethod without the init() step? My current approach feels a bit clunky...
I might be missing something in your question, but I think you might be barking up the wrong tree. There's nothing special about
__init__.pyin this regard--anything you write in__init__.pyis executed when you import the package, so I don't think you need thatinit()function at all. If you have a file containing:Now you can just use it by importing
mypackagelike:As an aside, one reason you might explicitly want something like your
init()function is principle of least surprise: Sinceregister_dataframe_accessormodifies the namespace ofDataFrameinstances for all users (including other libraries) there is a small possibility that yourregister_dataframe_accessor, just by importing your package, might override some other package's dataframe acccessor if they happen to share the same name.If the name is reasonably unique this may not be a problem though. It also may simply not be a problem for your package depending on how it's intended to be used.