Skip to content

Instantly share code, notes, and snippets.

@dgunning
Created October 22, 2019 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgunning/0cb00b985129ce8e1e3a34f003913829 to your computer and use it in GitHub Desktop.
Save dgunning/0cb00b985129ce8e1e3a34f003913829 to your computer and use it in GitHub Desktop.
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