Message Boards Message Boards

1
|
2790 Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:

How do I select nested lists?

This is a list manipulation question. For context, FinancialData[“name”,”prop”,{start,end}] returns a list with nested elements. For example,...

...are dividends paid by General Electric over that period.

What is the best way to select ranges within the list, say the date-dividend elements during 1 Dec 2020 to 31 August 2021? In other words, to get... {{{2020, 12, 18}, 0.08}, {{2021, 3, 5}, 0.08}, {{2021, 6, 25}, 0.08}}?
Those examples are small to simplify my question. An obvious answer is repeated calls to FinancialData[]. But I'm sure I can avoid that inefficiency if I understand more about list manipulation in Mathematica. If you answer with a code example, I might need some explanation with it.

POSTED BY: Jay Gourley
5 Replies
Posted 1 year ago

Normal is a sort of helper function that turns specialized structures into more simple representations, typically a List. TimeSeries is one of these special forms, but I wanted to use Select, and Select needs a List or at least something List-ish. Normal got me what I wanted. TimeSeries also has properties, so instead of Normal[dataTS] I could have used dataTS["DatePath"], and that's actually probably more natural.

For your other question, there is special syntax for Function. You can write an expression for the body of a function where arguments are represented by Slot expressions (which are typically just written as #) and & is added in post-fix position. So, Function[x, x^2] could be written as #^2&. In your case, I wanted to use Select with Between as the filtering predicate. Select expects the second argument to be a function (either a literal Function or a symbol/expression that can be applied to the individual elements). So, I needed to construct a function with a Between expression.

POSTED BY: Eric Rimbey

For this operation on a TimeSeries we can use the dedicated TimeSeriesWindow:

dataTS = 
 FinancialData["GE", "Dividend", {{2020, 1, 1}, {2021, 12, 31}}]
TimeSeriesWindow[dataTS, {{2020, 12, 1}, {2021, 9, 1}}]
POSTED BY: Gianluca Gorni

& is the punctuation to mark the end of a pure (or "anonymous") function. You can find the description of any keyboard shortcut by typing that shortcut into the search box in the documentation center. It's probably worth going through a tutorial on anonymous functions -- like Chapter 26 of the 3e of "An Elementary Introduction to the Wolfram Language".

In the details section of the TimeSeries help page, the docs note that the "Path" property generates the time-value pairs of the TimeSeries, and that Normal of a TimeSeries is an alternate way to generate those time-value pairs.

POSTED BY: Phil Earnhardt

Perfect. Better than I expected. Thanks. If you would,... What does the "&" do after Between[]? And what does Normal[] do to a TimeSeries? I read documentation on & and Normal but still don't see how they apply here.

POSTED BY: Jay Gourley
Posted 1 year ago

With

dataTS = FinancialData["GE", "Dividend", {{2020, 1, 1}, {2021, 12, 31}}]

you could do this

Select[Normal[dataTS], Between[#[[1]], {DateObject[{2020, 12, 1, 0, 0, 0}], DateObject[{2021, 9, 1, 0, 0, 0}]}] &]

With the legacy behavior, you could convert to DateObject in your selector:

dataLeg = FinancialData["GE", "Dividend", {{2020, 1, 1}, {2021, 12, 31}}, Method -> "Legacy"];
Select[dataLeg, Between[DateObject[#[[1]]], {DateObject[{2020, 12, 1, 0, 0, 0}], DateObject[{2021, 9, 1, 0, 0, 0}]}] &]
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract