Research

  • [x] I have searched the [pandas] tag on StackOverflow for similar questions.

  • [x] I have asked my usage related question on StackOverflow.

Link to question on StackOverflow

https://stackoverflow.com/questions/79594258/best-way-to-extend-subclass-pandas-dataframe

Question about pandas

I've written a package to work with energy-related timeseries. At its center is a class (PfLine) that is essentially a wrapper around pandas.DataFrame, and it implements various methods and properties that are also available on DataFrames - like .loc, .asfreq(), .index, etc.

I am currently in the middle of a rewrite of this package, and think it would be a good idea to have closer integration with pandas. This page lays out several possibilities, and I am unsure which route to take - and was hoping to find some sparring here.

Let me describe a bit what I'm trying to accomplish with the PfLine class:

  • Behaves like a DataFrame, with specific column names allowed and some data conversion (and validation) on initialisation.

  • Is immutable to avoid data from becoming inconsistent.

  • Has additional methods.

The methods could be directly under PfLine.method() or under e.g. df.pfl.method().

What is probably important: a way is needed for the user to specify a (still under development) configuration object (commodity) when initialising the PfLine. This object contains information used in coercing the data, e.g. what are the correct units and which timezones are allowed for the index.

Comment From: rhshadrach

Behaves like a DataFrame... Is immutable

These two are in conflict, pandas is not designed to be immutable. At the very least, you'd have to workaround:

  • __setitem__, .loc, .iloc, .at, .iat
  • .to_numpy(), .values
  • Any method with an inplace argument
  • Any method which acts inplace (e.g. update, insert)

But with these requirements, I believe the only feasible option would be subclass DataFrame / Series.

Comment From: jbrockmendel

Closing as addressed. Can reopen if there are further questions.