1
$\begingroup$

I am aware of How would I use Map on a Dataset column? still I cannot see how I should use MovingMap on a Dataset.

Even the identity functions does not work!

MovingMap[# &, dataset[[All, "Eons"]], 1]

My dataset is constructed like this

eons = {
  {{2021, 02, 22}, 95},
  {{2021, 02, 23}, 96},
  {{2021, 02, 24}, 100},
  {{2021, 02, 25}, 105}}
dataset = 
 Dataset[Map[Association[{"Date" -> #[[1]], "Eons" -> #[[2]]}] &, 
   eons]]

I can do things that are mere application of fuctions to a single row, e.g. Map[<|#, "Sqrt" -> Sqrt[#Eons]|> &, dataset ]. However using

MovingMap[# &, dataset[All, "Eons"], 1]

as suggested returns itself as output, which usually means mathematica does not know what to do.

$\endgroup$
4
  • $\begingroup$ I believe you want MovingMap[# &, dataset[All, "Eons"], 1] - note the single brackets. You will need to give an example of your dataset for more help though :) $\endgroup$
    – Carl Lange
    May 14 at 8:16
  • $\begingroup$ Thanks for the susggestion, this too gives the same trouble as using Part or [[, mathematica return the input as output ... meaning it did not understand $\endgroup$
    – Rho Phi
    May 14 at 8:31
  • 3
    $\begingroup$ Indeed, it looks like MovingMap doesn't like to work directly on Dataset in this way. You can do either MovingMap[# &, Normal@dataset[All, "Eons"], 1] or dataset[MovingMap[(# &), #, 1] &, "Eons"]. $\endgroup$
    – Carl Lange
    May 14 at 8:43
  • $\begingroup$ From the help of MovingMap: "The data can be a list of values {Subscript[x, 1],Subscript[x, 2],[Ellipsis]}, a list of time-value pairs {{Subscript[t, 1],Subscript[x, 1]},{Subscript[t, 2],Subscript[x, 2]},[Ellipsis]}, a TimeSeries, EventSeries, or TemporalData.", but not Dataset. $\endgroup$ May 14 at 15:31
3
$\begingroup$

As I posted in my comment: it looks like MovingMap doesn't like to work directly on Dataset in this way.

There are a few alternatives.

You can use the Dataset-style syntax to apply a function to a column's values:

dataset[MovingMap[(# &), #, 1] &, "Eons"]

You can do

MovingMap[# &, Normal@dataset[All, "Eons"], 1]

in order to convert the dataset's values into a simple list using Normal.

You can also convert your Dataset into TimeSeries data that MovingMap can operate directly on:

dataset = 
 Dataset[Map[
   Association[{"Date" -> DateObject@#[[1]], "Eons" -> #[[2]]}] &, 
   eons]]
ts = TimeSeries@Normal@Values@dataset
MovingMap[# &, ts, 1] (* returns a TimeSeries as well *)
$\endgroup$
1
  • $\begingroup$ I missed the fact that dataset[f,"columnName"] is essentially Apply to the column! $\endgroup$
    – Rho Phi
    May 15 at 18:59

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.