Customizing a DataFrame
In your company, any data has to come with a timestamp recording when the dataset was created, to make sure that outdated information is not being used. You would like to use pandas
DataFrames for processing data, but you would need to customize the class to allow for the use of timestamps.
In this exercise, you will implement a small LoggedDF
class that inherits from a regular pandas
DataFrame but has a created_at
attribute storing the timestamp. You will then augment the standard to_csv()
method to always include a column storing the creation date.
Tip: all DataFrame methods have many parameters, and it is not sustainable to copy all of them for each method you're customizing. The trick is to use variable-length arguments *args
and **kwargs
to catch all of them.
This exercise is part of the course
Object-Oriented Programming in Python
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import pandas as pd
____
# Define LoggedDF inherited from pd.DataFrame and add the constructor
____
ldf = LoggedDF({"col1": [1,2], "col2": [3,4]})
print(ldf.values)
print(ldf.created_at)