Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas date_range inconsistent behavior

Tags:

python

pandas

When using the pandas (v. 13.1) date_range function I get inconsistent behavior about whether or not the 'end' is inside in the returned range:

In [1]: pd.date_range(start='2014-06-09 15:36:55', 
                      end='2014-06-09 15:37:46', 
                      freq='20s')
Out[1]: [2014-06-09 15:36:55, ..., 2014-06-09 15:37:55]

And

In [2]: pd.date_range(start='2014-06-09 15:36:55', 
                      end='2014-06-09 15:37:46', 
                      freq='10s')
Out[2]: [2014-06-09 15:36:55, ..., 2014-06-09 15:37:45]

Note that in the first case the last time is later that the specified 'end', while in the second case the last time is smaller. Can anyone explain this?

like image 418
sjosund Avatar asked Nov 10 '22 05:11

sjosund


1 Answers

I believe this issue has already been fix in some release. Now the date_range behaviour is consistent, always including the start and the end of the DatetimeIndex always <= end. And the output format became pretty too.

Following code is using pandas 0.17.0:

In [1]: import pandas as pd

In [2]: pd.date_range(start='2014-06-09 15:36:55', 
   ...:                       end='2014-06-09 15:37:46', 
   ...:                       freq='20s')
Out[2]: 
DatetimeIndex(['2014-06-09 15:36:55', '2014-06-09 15:37:15',
               '2014-06-09 15:37:35'],
              dtype='datetime64[ns]', freq='20S')

In [3]: pd.date_range(start='2014-06-09 15:36:55', 
                      end='2014-06-09 15:37:46', 
                      freq='10s')
Out[3]: 
DatetimeIndex(['2014-06-09 15:36:55', '2014-06-09 15:37:05',
               '2014-06-09 15:37:15', '2014-06-09 15:37:25',
               '2014-06-09 15:37:35', '2014-06-09 15:37:45'],
              dtype='datetime64[ns]', freq='10S')

In [4]: pd.date_range(start='2014-06-09 15:36:55', 
                      end='2014-06-09 15:37:46', 
                      freq='15s')
Out[4]: 
DatetimeIndex(['2014-06-09 15:36:55', '2014-06-09 15:37:10',
               '2014-06-09 15:37:25', '2014-06-09 15:37:40'],
              dtype='datetime64[ns]', freq='15S')

In [5]: pd.date_range(start='2014-06-09 15:36:55', 
                      end='2014-06-09 15:37:46', 
                      freq='1s')
Out[5]: 
DatetimeIndex(['2014-06-09 15:36:55', '2014-06-09 15:36:56',
                   ......                    ......
               '2014-06-09 15:37:45', '2014-06-09 15:37:46'],
              dtype='datetime64[ns]', freq='S')

Note that the end of the DatetimeIndex never exceeds end date of date_range.

like image 56
YaOzI Avatar answered Nov 15 '22 09:11

YaOzI