Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A delegate class for pandas dataframes .. an alternative to subclassing pandas dataframes
import pandas as pd
class DataFrameHolder:
def __init__(self, data: pd.DataFrame, title=''):
self.data = data
self.title = title
def __len__(self):
return len(self.data)
def __getitem__(self, item):
if isinstance(item, list):
return self.data[item]
return self.data.iloc[item]
def query(self, query_str: str):
result = self.data.query(query_str)
return DataFrameHolder(result, title=self.title)
def __repr__(self):
return self.data.__repr__()
def _repr_html_(self):
html = f"<h3>{self.title}</h3>"
html = html + self.data._repr_html_()
return html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.