Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat representation should I use in Pandas for data valid throughout an interval?
    primarykey
    data
    text
    <p>I have a series of hourly prices. Each price is valid throughout the whole 1-hour period. <strong>What is the best way to represent these prices in Pandas that would enable me to index them in arbitrary higher frequencies (such as minutes or seconds) and do arithmetic with them?</strong></p> <h2>Data specifics</h2> <p>Sample prices might be:</p> <pre><code>&gt;&gt;&gt; prices = Series(randn(5), pd.date_range('2013-01-01 12:00', periods = 5, freq='H')) &gt;&gt;&gt; prices 2013-01-01 12:00:00 -1.001692 2013-01-01 13:00:00 -1.408082 2013-01-01 14:00:00 -0.329637 2013-01-01 15:00:00 1.005882 2013-01-01 16:00:00 1.202557 Freq: H </code></pre> <p>Now, what representation to use if I want the value at <code>13:37:42</code>(I expect it to be the same as at 13:00)?</p> <pre><code>&gt;&gt;&gt; prices['2013-01-01 13:37:42'] ... KeyError: &lt;Timestamp: 2013-01-01 13:37:42&gt; </code></pre> <h2>Resampling</h2> <p>I know I could resample the prices and fill in the details (<code>ffill</code>, right?), but that doesn't seem like such a nice solution, because I have to assume the frequency I'm going to be indexing it at and it reduces readability with too many unnecessary data points.</p> <h2>Time spans</h2> <p>At first glance a <code>PeriodIndex</code> seems to work</p> <pre><code>&gt;&gt;&gt; price_periods = prices.to_period() &gt;&gt;&gt; price_periods['2013-01-01 13:37:42'] -1.408082 </code></pre> <p>But a time-spanned series doesn't offer some of the other functionality I expect from a <code>Series</code>. Say that I have another series <code>amounts</code> that says how many items I bought in a certain moment. If I wanted to calculate the prices I would want to multiply the two series'</p> <pre><code>&gt;&gt;&gt; amounts = Series([1,2,2], pd.DatetimeIndex(['2013-01-01 13:37', '2013-01-01 13:57', '2013-01-01 14:05'])) &gt;&gt;&gt; amounts*price_periods </code></pre> <p>but that yields an exception and sometimes even freezes my IPy Notebook. Indexing doesn't help either.</p> <pre><code>&gt;&gt;&gt; ts_periods[amounts.index] </code></pre> <p>Are <code>PeriodIndex</code> structures still a work in progress or these features aren't going to be added? Is there maybe some other structure I should have used (or should use for now, before <code>PeriodIndex</code> matures)? I'm using Pandas version <code>0.9.0.dev-1e68fd9</code>.</p>
    singulars
    1. This table or related slice is empty.
    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