ENH: DataFrame.to_expr() method #41837
Labels
DataFrame
DataFrame data structure
Enhancement
IO Data
IO issues that don't fit into a more specific label
Needs Discussion
Requires discussion from core team before further action
Is your feature request related to a problem?
I would like to write some unit tests for my Pandas code. I want to test that some
DataFrame
is equal to an expected value. The expected value is complicated and I would like an easy way to get the Python code to construct it. My program already computes the expectedDataFrame
value but I need a way to serialize/deserialize it for use in my test code.Here is a StackOverflow question with more detail.
Describe the solution you'd like
I would like there to be a
DataFrame.to_expr()
method. It should return astr
containing valid Python code that can be used to re-construct theDataFrame
.To the greatest extent possible, it should be true that
pd.testing.assert_frame_equal(df1, eval(df2.to_expr()))
throws anAssertionError
if and only ifpd.testing.assert_frame_equal(df1, df2)
throws anAssertionError
. I am usingassert_frame_equal
because it checks column dtypes, whereasDataFrame.equals()
does not.Concretely, I think the return value of
.to_expr()
should be something likeNote that on many Python objects, this
.to_expr()
method is called__repr__()
. The Python docs state:However,
DataFrame.__repr__
is already defined to print a different representation (which is arguably more useful in an interactive environment).API breaking implications
This is a backwards-compatible change.
Describe alternatives you've considered
I've used
DataFrame.to_dict()
andDataFrame.from_dict()
for this purpose in the past. However, this doesn't preserve the type, and so it doesn't work if you're working with an emptyDataFrame
. I also worry thatfrom_dict
will sometimes fail to infer the original type even for non-emptyDataFrame
s.The text was updated successfully, but these errors were encountered: