Pandas timeseries with superimposed lines
<p>I have two data frames in pandas one with four timesseries with second by second data like the following</p>

<pre><code>timestamp            ID     value1 value2 value3 value4
2016/01/01T01:01:01  1234      100   50      50    60
2016/01/01T01:01:02  1234      101   48      48    52
2016/01/01T01:01:02  1234      101   48      48    52
....
</code></pre>

<p>and a second with averages from selected intervals</p>

<pre><code>ID start_time    end_time  avg_value1 avg_value2 avg_value3 avg_value4
1234 01:01:01      01:01:15     100.1    50.2        49          55
...
</code></pre>

<p>I would like to plot these two as timeseries superimposed over each other with the averages appearing as flat lines starting at start_time and ending at end_time. How would I go about doing this in the latest version of pandas?</p>

<p>The easiest way is to put all the data into a single DataFrame and use the built-in <code>.plot()</code> method.</p>

<p>Assuming your original DataFrame is called <code>df</code> the the code below should solve your issue (you might need to strip out the "ID" column):</p>

<pre><code>means = df.groupby(pd.TimeGrouper('15s')).mean()
means.columns = ['avg_'+col for col in df.columns]
merged_df = pd.concat([df, means], axis=1).fillna(method='ffill')
merged_df.plot()
</code></pre>

<p>Using some intraday 1s candle stock data, you get something like this:</p>

<p><a href="http://i.stack.imgur.com/xGGXl.png" rel="nofollow"><img src="http://i.stack.imgur.com/xGGXl.png" alt="enter image description here"></a></p>

<p>If you want to further customize your plots I am afraid you will have to spend a few hours/days studying the basics of matplotlib.</p>

python pandas