NB: The problem illustrated below is truly ubiquitous. I hope the examples given below are sufficiently different to demonstrate this fact, and to discourage answers that hinge on the details of the examples.
Example 1:
columnSpecs = Module[
{
numberOfRows = 4
, columnNames = {"a", "b", "c"}
, numberOfColumns
, columns
}
, numberOfColumns = Length[columnNames]
; SeedRandom[0]
; columns = Partition[ RandomSample[ Range[numberOfRows * numberOfColumns]], numberOfRows]
; AssociationThread[columnNames, columns]
];
ugly = Dataset[columnSpecs][Transpose];
nice = Dataset[ Transpose[columnSpecs, AllowedHeads -> All]];
Now, the datasets ugly
and nice
have exactly the same content:
Normal[ugly] == Normal[nice]
(* True *)
but they "look" different:
{ugly, nice}
(ugly: "What does nice
have that I don't?" Life is so unfair.)
Example 2:
In this case, the RHS expressions in the definitions of ugly
and nice
have exactly the same form, but differ in content.
ugly = Dataset[{<|"a" -> 11, "b" -> 2 , "c" -> 12|>}];
nice = Dataset[{<|"a" -> 11, "b" -> "2", "c" -> 12|>}];
{ugly, nice}
Bottom line: Datasets
get displayed inconsistently.
How can one canonicalize "ugly" Dataset
s so that they look "nice"?
As pointed out at the beginning of this post, in my experience, "ugly" Dataset
s arise in a very wide variety of ways, and therefore it would be a fool's errand to attempt to solve this problem at the point of generation of these "ugly" Dataset
s (there would be too many do's and don't's to remember). For instance, one could avoid the "ugly" Dataset
of Example 1 by using Dataset[columnSpecs][Transpose[#, AllowedHeads->All]&]
instead of Dataset[columnSpecs][Transpose]
, but this solution is very specific to the particularities of Transpose
, and is therefore useless for the case shown in Example 2.
Hence, I'm much more interested in ways to canonicalize (i.e. fix) the "ugly" Dataset
s after-the-fact (the "remedial" strategy) than in ways to avoid them in the first place (the "preventive" strategy). One can hope the remedial strategy would be the one more likely to yield the more generally applicable solution.
TypeSystem
thing. CompareFullForm@nice
withFullForm@ugly
and check this excellent Q&A. $\endgroup$