Why Pandas Series resample function generated an unexpected start_time after resampling?
<p>I have a time series data in pandas, I wanna calculate the number of data in different bins by using resampling function of pandas with a specified frequency. </p>

<p>The pandas series looks like something below if you use :</p>

<pre><code>test_data.head(10)
</code></pre>

<p>You will get the results (only show top 10 data as it is a large Series):</p>

<pre><code>    Created
2015-11-29 23:28:50    KBH889
2015-11-29 23:30:43    KBH89U
2015-11-29 23:34:06    KBH88K
2015-11-29 23:38:08    KBH8CC
2015-11-29 23:38:36    KBH83T
2015-11-29 23:40:52    KBH8CF
2015-11-29 23:46:27    KBH8F1
2015-11-29 23:50:01    KBH8DQ
2015-11-29 23:54:29    KBH8FV
2015-11-29 23:58:01    KBH8C6
Name: Order_Number, dtype: object
</code></pre>

<p>Then I use resample function with frequency "4541S" (It has to be 4541s exactly, cannot be changed!)</p>

<pre><code>test_data.Order_Number.resample("4541S").count()
</code></pre>

<p>Results:</p>

<pre><code>    Created
2015-11-29 22:42:18     9
2015-11-29 23:57:59    15
2015-11-30 01:13:40     6
2015-11-30 02:29:21     3
2015-11-30 03:45:02     2
Freq: 4541S, Name: Order_Number, dtype: int64
</code></pre>

<p>The resampled results starts with date time index <code>2015-11-29 22:42:18</code>. This is unwanted start_time. I want the time to be the minimal value of the series, in which case  the resampling should happen starting from <code>2015-11-29 23:28:50</code>. Does anybody know how to do this? I tried to use param <code>base</code> in the resampling function to adjust it. It seems it is difficult. </p>

<p>Here is the <a href="https://www.dropbox.com/s/ug1yllhajcp8eb5/test.csv?dl=0" rel="nofollow">csv file</a> for your testing. Import it and resample with count().</p>

<p>I think you are correct in that this should be achieved with the <code>base</code> argument. Unfortunately, I also cannot find a way to easily anchor the resampling with the original first datetimeindex (in your case, '2015-11-29 23:28:50') instead of a calculated one ('2015-11-29 22:42:18').</p>

<p>As a workaround, which might be helpful in your case but not entirely useful for other cases, here's what you can do: get the 'original' datetimeindex from the original data and the 'calculated' first datetimeindex from the resampled data:</p>

<pre><code>original = test_data.index[0]
calculated = test_data.Order_Number.resample('4541S',how='count',base=0).index[0]
</code></pre>

<p>(notice I am using pandas 0.17 resample API - you might need to apply <code>.count()</code> instead of using the 'how' argument)</p>

<p>Then get the difference in seconds between that 'calculated' first datetimeindex and the original first datetimeindex:</p>

<pre><code>base_seconds = int((original - calculated).seconds)
</code></pre>

<p>And finally, use <code>base_seconds</code> as the value for the <code>base</code> argument:</p>

<pre><code>test_data.Order_Number.resample('4541S',how='count',base=base_seconds)
</code></pre>

<p>Which yields</p>

<pre><code>Created
2015-11-29 23:28:50    19
2015-11-30 00:44:31    10
2015-11-30 02:00:12     3
2015-11-30 03:15:53     3
Freq: 4541S, Name: Order_Number, dtype: int64
</code></pre>

<p>I hope this helps.</p>

python pandas time-series