-1

I'm struggling with how to correctly feed a list of strings one-by-one into a pre-defined function. The list looks like this:

riclist = ["XAU=", "XAG=", "XPT=", "XPD="]

And the function looks like this (note that ek.get_timeseries is a pre-defined function from the Eikon library, but this problem could be generalized to any similar one):

def get_variable(input):

    chosenric = riclist[ ##each item one-by-one## ]

    var = ek.get_timeseries(rics=chosenric, 
                            start_date=2018-01-01,
                            fields="CLOSE") 

    return(var)

And the end result I'm after is a DataFrame with the time-series for all n variables in riclist.

K Upad
  • 1
  • 1
  • why not loop through your list of strings? `for mystr in riclist: ek.get_timeseries(rics=chosenric, start_date=2018-01-01, fields="CLOSE") ` – jkhadka Aug 09 '18 at 11:16
  • 1
    Wait a second.., You have an argument `input` (which should be renamed because it is overwriting the existing `input` function) which is never used – U13-Forward Aug 09 '18 at 11:16
  • can you provide an example without `ek.get_timeseries`? have you read about [mcve](https://stackoverflow.com/help/mcve)? – Azat Ibrakov Aug 09 '18 at 11:17
  • What does `ek.get_timeseries` return, e.g. list, array, series, something else? – jpp Aug 09 '18 at 11:17
  • if you need a dataFrame from a list just do pd.DataFrame(riclist) where pd is a pandas library. you can add the time field after that. – Ashutosh Chapagain Aug 09 '18 at 11:19

5 Answers5

0

How about:

for item in riclist:
    var = ek.get_timeseries(rics=chosenric, 
                        start_date=2018-01-01,
                        fields="CLOSE")
    ....do the rest here for each item
quest
  • 3,576
  • 2
  • 16
  • 26
0

I may be missing something in the question, but if the question is:

How to run a function on a list of arguments then there are a few simple ways:

  1. results = [get_variable(input) for input in riclist]
  2. results = map(get_variable, riclist)
Maxim Dunavicher
  • 635
  • 8
  • 19
0

Not sure if I completely understand your question, but are you looking for a for loop?

for ric in riclist:
    var = ek.get_timeseries(rics=ric, 
                            start_date=2018-01-01,
                            fields="CLOSE") 

then, if you want to return one at a time, you could use a generator

    yield var
rennerj2
  • 76
  • 8
  • This works to an extent - I can define the function and generate an output using "yield", the problem is storing the result as a DataFrame where the columns are each of the variables in "riclist". – K Upad Aug 09 '18 at 12:42
0

Returns a dict-like {chosenric : timeseries}

def get_variable(riclist):
    return {chosenric : ek.get_timeseries(rics=chosenric, start_date=2018-01-01, fields="CLOSE") for chosenric in riclist}
  • This also works well - but any help converting the output to a DataFrame rather than a dict? – K Upad Aug 09 '18 at 12:44
  • Doesn't this work? : df = pd.DataFrame(data=get_variable(riclist)) If not, send me the output you got from the function I wrote. – José Pedro Duarte Aug 09 '18 at 13:07
  • Unfortunately doesn't work - ValueError: If using all scalar values, you must pass an index – K Upad Aug 09 '18 at 13:19
  • Try it like this then: `def get_variable(riclist): return {chosenric : [ek.get_timeseries(rics=chosenric, start_date=2018-01-01, fields="CLOSE")] for chosenric in riclist} df = pd.DataFrame(data=get_variable(riclist))` (Sorry, cant paste code in here properly, i guess) – José Pedro Duarte Aug 09 '18 at 13:31
  • It doesn't quite work - the entire time-series for a single column is fit into a single cell (so the DataFrame is a 1x4 size) – K Upad Aug 09 '18 at 14:03
  • If you can send me the output of my function would be great, because I can't use "ek". Otherwise I'll be here just "non-sense" trying. – José Pedro Duarte Aug 09 '18 at 14:26
  • In: df Out: XAG= \ 0 XAG= CLOSE Date 2018-07-... XAU= \ 0 XAU= CLOSE Date 20... XPD= \ 0 XPD= CLOSE Date 2018-07-... XPT= 0 XPT= CLOSE Date 2018-07-... Each ticker (XAG=, XAU= etc) is its own cell in the DataFrame with the entire time series crammed in it – K Upad Aug 09 '18 at 14:47
0

It is probably better to ask these kind of questions on the Thomson Reuters Developer Portal, because you are asking something (licensed) product specific, which is not useful for the StackOverflow community.

That being said, the get_timeseries function can be fed a list of instruments directly. It always returns a pandas dataframe. So you probably want to do this:

df = ek.get_timeseries(riclist, fields="CLOSE", start_date='2018-01-01')

If you really need it to be part of a function, you could do this:

def get_variable():

    var = ek.get_timeseries(rics=riclist, 
                            start_date="2018-01-01",
                            fields="CLOSE") 

    return(var)

Please check out the documentation

**Disclaimer: I am currently employed by Thomson Reuters

PythonSherpa
  • 2,560
  • 3
  • 19
  • 40