'pandas: declare a pandas dataframe as a constant
I would like to declare my dataframe as constant, so no matter what operations are happening in a modeule it's values or column names do not change. I know that it is possible to define constant variables using slot =() like so,
class CONST(object):
__slots__ = ()
my_constant = 123
CONST = CONST()
CONST.my_constant = 345 # AttributeError: 'CONST' object attribute 'my_constant' is read-only
however when i try the same thing on pandas dataframe, it is not constant anymore.
import pandas as pd
df1 = pd.DataFrame({'text': ['the weather is good']})
class CONST(object):
__slots__ = ()
my_constant = pd.DataFrame({'text': ['the weather is good']})
CONST = CONST()
CONST.my_constant.columns =['message']
I receive no error this time saying that it is read_only. I also looked at this response here but got the same output that shows my pandas dataframe is not read-only.
Solution 1:[1]
Your first solution is applicable only for immutable data, consider following example
class CONST(object):
__slots__ = ()
my_constant = {"x":1,"y":2,"z":3}
CONST = CONST()
CONST.my_constant.clear()
print(CONST.my_constant)
output
{}
so in order to get it working you would need immutable (frozen) version of DataFrame, see pandas Immutable DataFrame for possible solutions.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Daweo |