Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas Immutable DataFrame

Tags:

I am interested in an Immutable DataFrame to use in a program as a reference table, with read_only properties enforced, after it has been initially constructed (which in my case is during a class def __init__() method).

I see Index Objects are Frozen.

Is there a way to make an entire DataFrame immutable?

like image 749
sanguineturtle Avatar asked Jul 24 '14 08:07

sanguineturtle


People also ask

Are Pandas DataFrame immutable?

All Pandas data structures are value mutable (can be changed) and except Series all are size mutable. Series is size immutable. Note − DataFrame is widely used and one of the most important data structures.

Are data frames mutable?

DataFrame is value-mutable and size-mutable (mutable in terms of the column number).

Is Panda index immutable?

In Pandas, Indexes are immutable like dictionary keys. They also assume homogeneity in data type like NumPy arrays. Before we get into the thick of all these, let us quickly remind ourselves of how to create Series, Indexes, and try to modify index names and get into our lesson for the day.

Are spark Dataframes immutable?

While Pyspark derives its basic data types from Python, its own data structures are limited to RDD, Dataframes, Graphframes. These data frames are immutable and offer reduced flexibility during row/column level handling, as compared to Python.


2 Answers

The StaticFrame package (of which I am an author) implements a Pandas-like interface, and many common Pandas operations, while enforcing immutability in underlying NumPy arrays and immutable Series and Frame containers.

You can make an entire Pandas DataFrame immutable by converting it to a StaticFrame Frame with static_frame.Frame.from_pandas(df). Then you can use it as a truly read-only table.

See StaticFrame documentation of this method: https://static-frame.readthedocs.io/en/latest/api_detail/frame.html#frame-constructor

like image 145
flexatone Avatar answered Oct 09 '22 19:10

flexatone


Try code something like this

class Bla(object):     def __init__(self):         self._df = pd.DataFrame(index=[1,2,3])      @property     def df(self):         return self._df.copy() 

this will allow you to get the df back, using b.df, but you will not be able to assign to it. So in short you have a df in class that behaves in the "Immutable DataFrame", purely in that it blocks changes to the original. the returned object is however still a mutable data frame so it will not behave like an Immutable one in other ways. I.e. you will not be able to use it as key for dictionary, etc.

like image 38
Joop Avatar answered Oct 09 '22 19:10

Joop