Resampling timeseries with a given timedelta
<p>I am using Pandas to structure and process Data. This is my DataFrame:</p>

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

<p>I want to do a resampling of time-series data, and have, for every ID (named here "3"), all bitrate scores, from beginning to end (beginning_time / end_time). For exemple, for the first row, I want to have all seconds, from 2016-07-08 02:17:42 to 2016-07-08 02:17:55, with the same bitrate score, and the same ID of course. Something like this :</p>

<p>For example, given :</p>

<pre><code>df = pd.DataFrame(
{'Id' : ['CODI126640013.ts', 'CODI126622312.ts'],
 'beginning_time':['2016-07-08 02:17:42', '2016-07-08 02:05:35'], 
 'end_time' :['2016-07-08 02:17:55', '2016-07-08 02:26:11'],
 'bitrate': ['3750000', '3750000']})
</code></pre>

<p>which gives :</p>

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

<p>And I want to have for the first row :</p>

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

<p>Same thing for the secend row..
So the objectif is to resample the deltaTime between the beginning and the end times, the bitrate score must be the same of course.</p>

<p>I'm trying this code:</p>

<pre><code>df['new_beginning_time'] = pd.to_datetime(df['beginning_time'])
df.set_index('new_beginning_time').groupby('Id', group_keys=False).apply(lambda df: df.resample('S').ffill()).reset_index()
</code></pre>

<p>But in this context, it didn't work ! Any ideas ? Thank you very much !</p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.melt.html" rel="nofollow"><code>melt</code></a> with <a href="http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#groupby-syntax-with-window-and-resample-operations" rel="nofollow"><code>resample</code> - 0.18.1 version of pandas</a>:</p>

<pre><code>df.beginning_time = pd.to_datetime(df.beginning_time)
df.end_time = pd.to_datetime(df.end_time)
df = pd.melt(df, id_vars=['Id','bitrate'], value_name='dates').drop('variable', axis=1)
df.set_index('dates', inplace=True)
print(df)
                                   Id  bitrate
dates                                         
2016-07-08 02:17:42  CODI126640013.ts  3750000
2016-07-08 02:05:35  CODI126622312.ts  3750000
2016-07-08 02:17:55  CODI126640013.ts  3750000
2016-07-08 02:26:11  CODI126622312.ts  3750000

print (df.groupby('Id').resample('1S').ffill())
                                                    Id  bitrate
Id               dates                                         
CODI126622312.ts 2016-07-08 02:05:35  CODI126622312.ts  3750000
                 2016-07-08 02:05:36  CODI126622312.ts  3750000
                 2016-07-08 02:05:37  CODI126622312.ts  3750000
                 2016-07-08 02:05:38  CODI126622312.ts  3750000
                 2016-07-08 02:05:39  CODI126622312.ts  3750000
                 2016-07-08 02:05:40  CODI126622312.ts  3750000
                 2016-07-08 02:05:41  CODI126622312.ts  3750000
                 2016-07-08 02:05:42  CODI126622312.ts  3750000
                 2016-07-08 02:05:43  CODI126622312.ts  3750000
                 2016-07-08 02:05:44  CODI126622312.ts  3750000
                 2016-07-08 02:05:45  CODI126622312.ts  3750000
                 2016-07-08 02:05:46  CODI126622312.ts  3750000
                 2016-07-08 02:05:47  CODI126622312.ts  3750000
                 ...
                 ...
</code></pre>

python datetime pandas group-by resampling