简体   繁体   English

Pandas date_range行为不一致

[英]Pandas date_range inconsistent behavior

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: 当使用pandas(v.13.1)date_range函数时,我得到关于'end'是否在返回范围内的不一致行为:

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. 请注意,在第一种情况下,最后一次是指定的'end',而在第二种情况下,最后一次是较小的。 Can anyone explain this? 有谁能解释一下?

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 . 现在, date_range行为是一致的,始终包括DatetimeIndex始终<= endstartend And the output format became pretty too. 输出格式也变得相当。

Following code is using pandas 0.17.0: 以下代码使用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 . 请注意, DatetimeIndex的结尾永远不会超过date_range end日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM