I have 2 datasets: dsE and dsF that I want to process in a consistent way:

dsE = Dataset[
<|"Col1" -> RandomReal[100], "Col2" ->  RandomReal[100], 
   "Col3" ->  RandomReal[100] 

dsF = Dataset[{
   <|"Col1" -> RandomReal[100], "Col2" ->  RandomReal[100], 
    "Col3" ->  RandomReal[100] |>,
   <|"Col1" -> RandomReal[100], "Col2" ->  RandomReal[100], 
    "Col3" ->  RandomReal[100] |>

I want to write a function that accepts either dsE or dsF to produce a dataset, say dsG, of the shape

(*1 or more Associations*)

Essentially, WL presents a dataset of 1 row as an Association, whereas datasets of greater than 1 row are presented as a List of Associations ~ and I want to make sure that my functions are conforming to exactly the same shape of input data: a List of Associations, regardless of rowsize.

This is proving to be more challenging than I anticipated. I think my approach

If[Head@Normal@ds ==  Symbol["Association"], a = Dataset[{ds}], 

...is going down the wrong route for several reasons - this does not handle the generic case of all datasets, does not use the FP approach, etc etc..

  • 1
    $\begingroup$ You can declare a dataset with one Association as a list with one element: dsE = Dataset[{<|"Col1" -> RandomReal[100], "Col2" -> RandomReal[100], "Col3" -> RandomReal[100]|>}]; $\endgroup$ Mar 26 at 15:01
  • $\begingroup$ Hello @DanielHuber I should have mentioned that I do not have any control over the inputs (dsE,dsF), only the function. Essentially, my function needs to work with a dataset of any rowsize to produce a consistent shape. $\endgroup$
    – berrynice
    Mar 26 at 19:11
  • $\begingroup$ You can check if the head of your data: dat is Dataset, then it it contains a list, or not, then we need to add the list. This can be done by: dat= dat /. HoldPattern@Dataset[x__] :> Dataset[{{x}[[1]]}] $\endgroup$ Mar 26 at 19:47

Maybe something like:

f = If[#[Head] === List, #, #[List]] &;

f @ dsE 

enter image description here

Normal @ f @ dsE
{<|"Col1" -> 95.2175, "Col2" -> 91.2453, "Col3" -> 99.8906|>}
f @ dsF

enter image description here

Normal @ f @ dsF
{<|"Col1" -> 95.9263, "Col2" -> 32.9574, "Col3" -> 42.4036|>,
 <|"Col1" -> 25.8791, "Col2" -> 90.6759,   "Col3" -> 95.1289|>}
  • $\begingroup$ wow, @kglr that's perfect. $\endgroup$
    – berrynice
    Mar 26 at 22:58
  • $\begingroup$ I didn't quite get the intuition for how #[List] works. It appears that you can construct a WL expression to transform anything to any Head (type). Is there any documentation on this? I dont think the syntax guide covers it: reference.wolfram.com/language/guide/Syntax.html $\endgroup$
    – berrynice
    Mar 26 at 23:11
  • 1
    $\begingroup$ @berrynice, I am using Head and List as Querys. That is, dsE[Head] is the same as Query[Head]@dsE. Regarding, "transform anything to any...", note that "Query can operate on a Dataset object or an arbitrary nested expression consisting of lists and associations". $\endgroup$
    – kglr
    Mar 27 at 0:15
  • $\begingroup$ understood! thanks a lot @kglr $\endgroup$
    – berrynice
    Mar 27 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.