I'm building a database type object which, when an index is not found, uses an api to retrieve the information, save it to the object/file and return it.
I'd like to do this by overloading the
import pandas as pd
"""DataBase Object which can be updated by external api"""
def __init__(self, path, api=None):
self.api = api
loc is a property that creates returns a name called
_loc if its not
None else it creates a
pandas.core.indexing._LocIndexer on demand. Indexers, by default have access to the DataFrame that created them, so you can modify the DataFrame on a key miss.
You can override the behaviour of
DataFrame.loc by subclassing
_LocIndexer as thus.
class MyLocIndexer(_LocIndexer): def __getitem__(self, key): try: return super().__getitem__(key) except KeyError: item = db.fetch_item(key) self[key] = item return item # `return self[key]' is better as it also works when accessing a # whole axis class MyDataFrame(DataFrame): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._loc = MyLocIndexer(self, "loc")
The above is written in python3, so you will have to fix the super statements if you are using python2.