pydata/pandas — pull request #6908 by sinhrks (2014-04-18 12:34:42+00:00)
Title: BUG: TimeGrouper outputs different result by column order

`TimeGrouper` may output incorrect results depending on the target column order. The problem seems to be caused by 2 parts.
- `TimeGrouper._get_time_bins` and related methods expects sorted values input.
- `BinGrouper.get_iterator` expects sorted data input.

```
>>> df = pd.DataFrame({'Branch' : 'A A A A A A A B'.split(),
'Buyer': 'Carl Mark Carl Carl Joe Joe Joe Carl'.split(),
'Quantity': [1,3,5,1,8,1,9,3],
'Date' : [
datetime(2013,1,1,13,0), datetime(2013,1,1,13,5),
datetime(2013,10,1,20,0), datetime(2013,10,2,10,0),
datetime(2013,10,1,20,0), datetime(2013,10,2,10,0),
datetime(2013,12,2,12,0), datetime(2013,12,2,14,0),]})

# correct
>>> df.groupby([pd.Grouper(freq='1M',key='Date'),'Buyer']).sum()
Quantity
Date Buyer
2013-01-31 Carl 1
Mark 3
2013-10-31 Carl 6
Joe 9
2013-12-31 Carl 3
Joe 9

[6 rows x 1 columns]

>>> df_sorted = df.sort('Quantity') # change "Date" column unsorted
# incorrect
>>> df_sorted.groupby([pd.Grouper(freq='1M',key='Date'),'Buyer']).sum()
Quantity
Date Buyer
2013-01-31 Carl 1
2013-10-31 Carl 1
Joe 1
Mark 3
2013-12-31 Carl 8
Joe 17

[6 rows x 1 columns]

>>> df_sorted.groupby([pd.Grouper(freq='1M',key='Date', sort=True),'Buyer']).sum()
# same incorrect result
```

```
# correct
>>> df.groupby([pd.Grouper(freq='6M',key='Date'),'Buyer']).sum()
Quantity
Date Buyer
2013-01-31 Carl 1
Mark 3
2014-01-31 Carl 9
Joe 18

[4 rows x 1 columns]

# incorrect
>>> df_sorted.groupby([pd.Grouper(freq='6M',key='Date'),'Buyer']).sum()
Quantity
Date Buyer
2013-01-31 Carl 1
2014-01-31 Carl 9
Joe 18
Mark 3

[4 rows x 1 columns]
```