Note that there are some explanatory texts on larger screens.

plurals
  1. POPandas Inter-row calculations
    primarykey
    data
    text
    <p>I have a DataFrame with daily OHLCV data.</p> <p>I can calculate the range with:</p> <pre><code>s['Range'] = s['High'] - s['Low'] </code></pre> <p>Simple. Now I would like to calculate a new column which I've called <code>s['OIR']</code> (OIR = Open-In-Range)</p> <p>The <code>['OIR']</code> column checks to see if we opened in range and it does this by testing if we opened above yesterdays low and below yesterday's high. I need to reference the previous rows and I'm not quite sure how to do it. The return values would be True/False.</p> <p>Thanks.</p> <hr> <p>edit: I'm new to StackExchange and Python. Not sure where to drop sample data. Here's an image of the dataframe.</p> <p><a href="http://i47.tinypic.com/142eb2a.png" rel="nofollow">http://i47.tinypic.com/142eb2a.png</a></p> <hr> <p>Sample Data: Dictionary convert to DataFrame</p> <pre><code>{'High': {&lt;Timestamp: 2007-03-02 00:00:00&gt;: 1384.5, &lt;Timestamp: 2007-03-05 00:00:00&gt;: 1373.0}, 'Last': {&lt;Timestamp: 2007-03-02 00:00:00&gt;: 1365.0, &lt;Timestamp: 2007-03-05 00:00:00&gt;: 1351.5}, 'Low': {&lt;Timestamp: 2007-03-02 00:00:00&gt;: 1364.25, &lt;Timestamp: 2007-03-05 00:00:00&gt;: 1350.5}, 'OIR': {&lt;Timestamp: 2007-03-02 00:00:00&gt;: False, &lt;Timestamp: 2007-03-05 00:00:00&gt;: False}, 'Open': {&lt;Timestamp: 2007-03-02 00:00:00&gt;: 1378.5, &lt;Timestamp: 2007-03-05 00:00:00&gt;: 1356.75}, 'Range': {&lt;Timestamp: 2007-03-02 00:00:00&gt;: 20.25, &lt;Timestamp: 2007-03-05 00:00:00&gt;: 22.5}, 'Volume': {&lt;Timestamp: 2007-03-02 00:00:00&gt;: 1706906, &lt;Timestamp: 2007-03-05 00:00:00&gt;: 1984041}} </code></pre> <hr> <p>Answer: </p> <pre><code>s['OIR'] = ((s['Open'] &lt; s['High'].shift(1)) &amp; (s['Open'] &gt; s['Low'].shift(1))) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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