I frequently place objects which are not strings or numbers in my DataFrames. I've read many Stack Overflow comments which discourage this, and I know that it general it should be avoided, but I believe that I have a valid use case. If the objects are iterable (like another DataFrame), I protect them from Pandas attempt to broadcast the elements by wrapping the object in a single-element list. This all works, but it doesn't print nicely.
I looked into pandas.set_option()
to see whether there was a way to clean up printing in the way that I wanted. When I didn't find it, I wrote a work-around. I defined a PrettyDataFrame subclass which has only one method, __str__()
. When a column's dtype is "object"
, I assume that object is a list, and I print the dtype of element zero. A typical output from an IPython interpreter session looks like this:
In [3]: exp
Out[3]:
embed_dim kernel_size min_loss best_epoch elapsed_time history embedding best_weights
0 1 3 -0.717174 32 361.163418 <PrettyDataFrame> <DataFrame> <Series>
1 1 3 -0.717349 34 424.519406 <PrettyDataFrame> <DataFrame> <Series>
2 1 3 -0.717181 25 287.420010 <PrettyDataFrame> <DataFrame> <Series>
3 1 3 -0.717192 31 327.283332 <PrettyDataFrame> <DataFrame> <Series>
In [4]: exp.loc[2].history
Out[4]:
initial_epoch final_epoch min_loss best_epoch best_weights loss
0 0 19 -0.717125 19 <list> <ndarray>
1 20 28 -0.717181 25 <list> <ndarray>
As the Pandas GitHub guide recommends, I posted to Stack Overflow first, asking whether the feature I wanted exists. After waiting for several days and receiving no replies, I'm moving here to continue the discussion.
I'm not ready to submit a pull request because what I wrote is rather specific to my needs. I don't know if anyone else would find this useful, or if they would want it to look different. Additionally, if this kind of pretty-printing was part of a standard DataFrame instead of a weird subclass, the printing behavior would presumably have to be configurable through set_option(), and I haven't begun to think about how to define that.
Looking forward to any discussions. Thanks.
Comment From: mroeschke
Thanks for the suggestion, but it appears there's not much appetite for this feature from the community and core devs (since nested objects are not greatly supported in pandas). Closing but can reopen if there's renewed interest