Note that there are some explanatory texts on larger screens.

plurals
  1. POList Comprehension Loop
    text
    copied!<p>I have a csv file with date, time., price, mag, signal. 62035 rows; there are 42 times of day associated to each unique date in the file.</p> <p>For each date, when there is an 'S' in the signal column append the corresponding price at the time the 'S' occurred. Below is the attempt.</p> <pre><code>from pandas import * from numpy import * from io import * from os import * from sys import * DF1 = read_csv('___.csv') idf=DF1.set_index(['date','time','price'],inplace=True) sStore=[] for i in idf.index[i][0]: sStore.append([idf.index[j][2] for j in idf[j][1] if idf['signal']=='S']) sStore.head() </code></pre> <pre><code>Traceback (most recent call last) &lt;ipython-input-7-8769220929e4&gt; in &lt;module&gt;() 1 sStore=[] 2 ----&gt; 3 for time in idf.index[i][0]: 4 5 sStore.append([idf.index[j][2] for j in idf[j][1] if idf['signal']=='S']) NameError: name 'i' is not defined </code></pre> <p>I do not understand why the i index is not permitted here. Thanks.</p> <p>I also think it's strange that : </p> <p>idf.index.levels[0] will show the dates "not parsed" as it is in the file but out of order. Despite that parse_date=True as an argument in set_index.</p> <p>I bring this up since I was thinking of side swiping the problem with something like: </p> <pre><code>for i in idf.index.levels[0]: sStore.append([idf.index[j][2] for j in idf.index.levels[1] if idf['signal']=='S']) sStore.head() </code></pre> <p><strong>My edit 12/30/2012 based on DSM's comment below:</strong></p> <p>I would like to use your idea to get the P&amp;L, as I commented below. Where if S!=B, for any given date, we difference using the closing time, 1620. </p> <pre><code>v=[df["signal"]=="S"] t=[df["time"]=="1620"] u=[df["signal"]!="S"] df["price"][[v and (u and t)]] </code></pre> <p>That is, "give me the price at 1620; (even when it doesn't give a "sell signal", S) so that I can diff. with the "extra B's"--for the special case where B>S. This ignores the symmetric concern (where S>B) but for now I want to understand this logical issue. </p> <p>On traceback, this expression gives: </p> <pre><code>ValueError: boolean index array should have 1 dimension </code></pre> <p>Note that in order to invoke df["time'] <strong>I do not set_index here</strong>. Trying the union operator | gives: </p> <pre><code>TypeError: unsupported operand type(s) for |: 'list' and 'list' </code></pre> <p><strong>Looking at Max Fellows's approach</strong>,</p> <p>@Max Fellows </p> <p>The point is to close out the positions at the end of the day; so we need to capture to price at the close to "unload" all those B, S which were accumulated; but didn't net each other out. If I say: </p> <pre><code>filterFunc1 = lambda row: row["signal"] == "S" and ([row["signal"] != "S"][row["price"]=="1620"]) filterFunc2 =lambda row: ([row["price"]=="1620"][row["signal"] != "S"]) filterFunc=filterFunc1 and filterFunc2 filteredData = itertools.ifilter(filterFunc, reader) </code></pre> <p>On traceback:</p> <pre><code>IndexError: list index out of range </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload