I have created a class based on a pandas.DataFrame
object which initializes with a csv
file as shown here:
import pandas as pd
class CustomDataFrame(pd.DataFrame):
def __init__(self, input_file):
df = pd.read_csv(input_file)
super().__init__(df)
#...
This way, I have a CustomDataFrame
type that has additional specific methods to operate on itself. The problem I have with this setup is that when I take a slice of the object, it returns a pandas.DataFrame
object instead of keeping the same type. In other words:
> blip = mypackage.CustomDataFrame('test.csv')
> type(blip)
mypackage.CustomDataFrame
> type(blip[1:3])
pandas.core.frame.DataFrame
Is there a simple way to correct my custom class such that it can operate on itself in all the ways that a pandas.DataFrame
can, while returning this custom class each time rather than just the built-in version of the DataFrame
?