Groupby apply lost data 

Test file : [1.xlsx](https://github.com/pandas-dev/pandas/files/2497717/1.xlsx)

```
import pandas as pd

q = pd.read_excel('1.xlsx')

sid = '81bc85dc-bf4b-44ef-98fa-fa13a1f54142'
pid = '96ed6132-fb17-4511-b31c-5bbea2f57069'

def timestamp2datetime(series, unit='ms'):
    return pd.to_datetime(series, unit=unit).dt.tz_localize('UTC').dt.tz_convert('Asia/Shanghai').dt.tz_localize(None)

def func(df):
    if 'datetime_create' not in df.columns:
        df.loc[:, 'datetime_create'] = timestamp2datetime(df.time_create)
        df.index = df.datetime_create  
        df.sort_index(inplace=True)

    so_start = df.loc[(df.store_product_quantity_old>0) & (df.store_product_quantity_new==0)].datetime_create
    so_start = so_start.reset_index(drop=True)

    so_end = df.loc[(df.store_product_quantity_old==0) & (df.store_product_quantity_new>0)].datetime_create
    so_end = so_end.reset_index(drop=True)

    s = pd.DataFrame(index=so_start, data={'stockout_min':1} , dtype='int8')
    e= pd.DataFrame(index=so_end, data={'stockout_min':0},  dtype='int8')
    df = pd.concat((s, e)).sort_index()

    return df
# product_ids = list(df.product_id.unique()[:2]) + [pid]

x = q.groupby(['store_id', 'product_id']).apply(func)
x = x.reset_index()
r1 = x.loc[(x.store_id==sid)&(x.product_id==pid)]

y = q.loc[(q.store_id==sid)&(q.product_id==pid)]
r2 = func(y)

assert r1.shape[0] == r2.shape[0]
```




#### Problem description

I have a df
```

In [12]: df
Out[12]:
                                      store_id                            product_id      ...        origin    time_create
0         81bc85dc-bf4b-44ef-98fa-fa13a1f54142  4fbea3e6-c56c-4693-9856-f601b58522c6      ...             0  1536824704438
1         66e06f08-dec2-498d-883f-24771da18358  78259001-4293-4396-b064-9b5ca38943b0      ...             0  1537407824000
2         66e06f08-dec2-498d-883f-24771da18358  3c741651-1f36-4edd-aaef-7c9db0b85377      ...             0  1537058557513
19        81bc85dc-bf4b-44ef-98fa-fa13a1f54142  3ec55531-9fb2-4f2c-ae2f-97ca26924896      ...             0  1536736230667
22        66e06f08-dec2-498d-883f-24771da18358  5d80024c-bc71-4929-895c-eb7f649229ea      ...             0  1536035099165
27        81bc85dc-bf4b-44ef-98fa-fa13a1f54142  43268951-47bd-4943-b96d-f7d2330adcba      ...             0  1536995816898
51        81bc85dc-bf4b-44ef-98fa-fa13a1f54142  fcc7246a-d3f2-4a7e-9348-3afda45ff062      ...             0  1536658769973
81        66e06f08-dec2-498d-883f-24771da18358  ee635be9-63a5-4af9-b96f-0a9a83d14dfe      ...             0  1536203525425
85        81bc85dc-bf4b-44ef-98fa-fa13a1f54142  7c1af997-2bf0-49a0-91a2-92e64ab080bf      ...             0  1536118168873
88        81bc85dc-bf4b-44ef-98fa-fa13a1f54142  4c04cad1-1ee0-4ccc-8287-8c82af428896      ...             0  1537945572765
99        66e06f08-dec2-498d-883f-24771da18358  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1537428614934
108       81bc85dc-bf4b-44ef-98fa-fa13a1f54142  7300794b-c71a-4c53-ab1d-bc0cfab951e1      ...             0  1536280581980
117       81bc85dc-bf4b-44ef-98fa-fa13a1f54142  d9420746-fa3a-4e33-981d-3da0b53dff2f      ...             0  1537662665770
121       66e06f08-dec2-498d-883f-24771da18358  825df1bc-c791-4426-901b-f27e48bf97f5      ...             0  1536705144767
125       66e06f08-dec2-498d-883f-24771da18358  d433e581-366d-4d7f-b6ff-f3c2be9a0af5      ...             0  1537254879827
151       66e06f08-dec2-498d-883f-24771da18358  535616a0-db50-4468-a599-3c39328ee56a      ...             0  1536978291293
179       66e06f08-dec2-498d-883f-24771da18358  ccc2d0e0-7aff-41fd-95c5-7050c310fc76      ...             0  1537078316208
186       81bc85dc-bf4b-44ef-98fa-fa13a1f54142  6609355a-b779-45ac-8281-93cf2804c48f      ...             0  1536198238856
195       81bc85dc-bf4b-44ef-98fa-fa13a1f54142  c11858fc-8831-4d53-869a-880947b60541      ...            20  1538052840293
226       66e06f08-dec2-498d-883f-24771da18358  2ee37c2b-7b69-4cf4-ac56-934d22fff0df      ...             0  1537316331694
227       81bc85dc-bf4b-44ef-98fa-fa13a1f54142  0503fcd8-67b9-48f0-9ff4-181bcf89afa0      ...            20  1536619144720
274       66e06f08-dec2-498d-883f-24771da18358  de547567-5399-4b25-8129-3212dd423fee      ...             0  1537001231036
283       81bc85dc-bf4b-44ef-98fa-fa13a1f54142  27906a4a-5de0-4bec-a55e-bb672c33f852      ...             0  1537503133373
287       81bc85dc-bf4b-44ef-98fa-fa13a1f54142  fe86d3bd-389b-4148-8c3c-72e7f14ffaa8      ...             0  1537699033113
301       66e06f08-dec2-498d-883f-24771da18358  e4947270-ecf6-444b-be3b-cdc20d96e7d5      ...             0  1537931490300
304       66e06f08-dec2-498d-883f-24771da18358  2607698e-b31e-4195-a6aa-f24e3d7fc5d9      ...             0  1536192769522
307       81bc85dc-bf4b-44ef-98fa-fa13a1f54142  644381c0-b6a7-4143-8fad-460bdb2c0887      ...             0  1537870372385
309       81bc85dc-bf4b-44ef-98fa-fa13a1f54142  60922bd9-dcfd-4aff-892f-8d7d4d1e7373      ...             0  1537247888881
313       81bc85dc-bf4b-44ef-98fa-fa13a1f54142  3f60b1fe-9a6d-499c-9b3b-2606e383b52e      ...             0  1537500496966
324       66e06f08-dec2-498d-883f-24771da18358  0679bc12-0046-477b-85b8-03adb0456a09      ...             0  1538131653418

```

Do groupby apply
```
result = df.groupby(['store_id', 'product_id']).apply(func, *args, **kwargs)
```

`print(result)     # note it have 3 indexs) `

```
                                                                                                          stockout_min
store_id                             product_id                           datetime_create
66e06f08-dec2-498d-883f-24771da18358 000a7fde-de18-4c42-8029-5bcdf09d1aec 2018-09-02 11:56:33.967             0
                                                                          2018-09-03 20:10:31.004             1
                                                                          2018-09-12 12:01:11.313             0
                                                                          2018-09-13 08:32:57.133             1
                                                                          2018-09-17 12:04:47.182             0
                                                                          2018-09-23 08:38:43.894             1
                                                                          2018-09-24 13:06:46.405             0
                                                                          2018-09-30 08:01:16.577             1
                                     000ef64d-eee9-4da1-9668-2fd710a9415e 2018-09-02 11:56:33.967             0
                                                                          2018-09-03 20:10:31.004             1
                                                                          2018-09-12 12:01:11.313             0
                                                                          2018-09-13 08:32:57.133             1
                                                                          2018-09-17 12:04:47.182             0
                                                                          2018-09-23 08:38:43.894             1
                                                                          2018-09-24 13:06:46.405             0
                                                                          2018-09-30 08:01:16.577             1
                                     00319bdb-9d9e-4b6f-af6d-006bddd4ee49 2018-09-02 11:56:33.967             0
                                                                          2018-09-03 20:10:31.004             1
                                                                          2018-09-12 12:01:11.313             0
                                                                          2018-09-13 08:32:57.133             1
                                                                          2018-09-17 12:04:47.182             0
                                                                          2018-09-23 08:38:43.894             1
                                                                          2018-09-24 13:06:46.405             0
                                                                          2018-09-30 08:01:16.577             1
                                     0033120f-6391-4f82-b635-6032ae02f1e5 2018-09-02 11:56:33.967             0
                                                                          2018-09-03 20:10:31.004             1
                                                                          2018-09-12 12:01:11.313             0
                                                                          2018-09-13 08:32:57.133             1
                                                                          2018-09-17 12:04:47.182             0
                                                                          2018-09-23 08:38:43.894             1
...                                                                                                         ...
81bc85dc-bf4b-44ef-98fa-fa13a1f54142 ffac4741-d63d-46a2-963d-941bf3a67154 2018-09-12 12:01:11.313             0
                                                                          2018-09-13 08:32:57.133             1
                                                                          2018-09-17 12:04:47.182             0
                                                                          2018-09-23 08:38:43.894             1
                                                                          2018-09-24 13:06:46.405             0
                                                                          2018-09-30 08:01:16.577             1
                                     ffb81d61-dcc5-4674-91f7-a7a179290af4 2018-09-02 11:56:33.967             0
                                                                          2018-09-03 20:10:31.004             1
                                                                          2018-09-12 12:01:11.313             0
                                                                          2018-09-13 08:32:57.133             1
                                                                          2018-09-17 12:04:47.182             0
                                                                          2018-09-23 08:38:43.894             1
                                                                          2018-09-24 13:06:46.405             0
                                                                          2018-09-30 08:01:16.577             1
                                     ffd91951-b806-4ca9-8f4f-1fce30e5c509 2018-09-02 11:56:33.967             0
                                                                          2018-09-03 20:10:31.004             1
                                                                          2018-09-12 12:01:11.313             0
                                                                          2018-09-13 08:32:57.133             1
                                                                          2018-09-17 12:04:47.182             0
                                                                          2018-09-23 08:38:43.894             1
                                                                          2018-09-24 13:06:46.405             0
                                                                          2018-09-30 08:01:16.577             1
```

select by sid and pid( after apply)

```
s = result.reset_index()
s.loc[(s.store_id==sid)&(s.product_id==pid)]
```

Out
```
                                   store_id                            product_id         datetime_create  stockout_min
46520  81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069 2018-09-02 11:56:33.967             0
46521  81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069 2018-09-03 20:10:31.004             1
46522  81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069 2018-09-12 12:01:11.313             0
46523  81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069 2018-09-13 08:32:57.133             1
46524  81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069 2018-09-17 12:04:47.182             0
46525  81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069 2018-09-23 08:38:43.894             1
46526  81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069 2018-09-24 13:06:46.405             0
46527  81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069 2018-09-30 08:01:16.577             1
```

#### Expected Output


select by sid and pid(without groupby apply)
```
d = df.loc[(df.store_id==sid)&(df.product_id==pid)]
```

out
```
                                      store_id                            product_id      ...        origin    time_create
1980      81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536544237940
5516      81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1537061659735
7846      81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1537781771414
14250     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536380797357
15840     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536387784378
18602     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536634147500
19289     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536915170082
20993     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536044943223
22491     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536659879059
37015     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1535766172464
43814     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536894146128
54754     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1538035482482
59273     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1537754031833
59579     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536998146324
71857     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1537064216176
74654     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1538046879220
80081     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536977328709
83003     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1538299585178
84454     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1537881844058
87554     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536309028094
91508     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1537255518851
93327     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1535860024401
96261     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1537062394558
99765     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536380812081
99896     81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536630609824
102680    81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1536120093328
103164    81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1535958610166
113224    81bc85dc-bf4b-44ef-98fa-fa13a1f54142  96ed6132-fb17-4511-b31c-5bbea2f57069      ...             0  1535958455237
```

Apply `func` without groupby

```
In [11]: func(d)
Out[11]:
                         stockout_min
datetime_create
2018-09-01 09:03:26.347             0
2018-09-01 17:48:52.279             1
2018-09-02 08:43:07.025             0
2018-09-02 15:03:02.009             1
2018-09-02 17:17:59.154             0
2018-09-02 17:26:51.118             1
2018-09-02 17:33:11.269             0
2018-09-02 17:39:22.448             1
2018-09-03 08:41:10.807             0
2018-09-03 16:03:55.925             1
2018-09-03 16:35:28.840             0
2018-09-03 16:37:04.718             1
2018-09-04 09:11:30.790             0
2018-09-04 16:45:40.530             1
2018-09-05 07:44:16.695             0
2018-09-06 17:24:47.614             1
2018-09-06 19:36:26.663             0
2018-09-06 19:36:58.085             1
2018-09-06 22:11:14.210             0
2018-09-06 22:11:38.471             1
2018-09-07 07:39:10.973             0
2018-09-07 20:23:32.641             1
2018-09-08 08:21:47.847             0
2018-09-08 14:37:55.714             1
2018-09-09 08:25:27.755             0
2018-09-09 15:22:50.761             1
2018-09-10 09:08:50.193             0
2018-09-10 17:17:05.368             1
2018-09-10 18:30:03.042             0
2018-09-10 18:30:36.645             1
...                               ...
```




I think above result already show the problem: same function,  after groupby ,  lost a lot of data. 

It is not about `func`, but still give the code: 

```
def select_stock_changepoint(df):
    if 'datetime_create' not in df.columns:
        df.loc[:, 'datetime_create'] = timestamp2datetime(df.time_create)
        df.index = df.datetime_create  
        df.sort_index(inplace=True)
        df = fix_stock_quantity_log(df)

    so_start = df.loc[(df.store_product_quantity_old>0) & (df.store_product_quantity_new==0)].datetime_create
    so_start = so_start.reset_index(drop=True)

    so_end = df.loc[(df.store_product_quantity_old==0) & (df.store_product_quantity_new>0)].datetime_create
    so_end = so_end.reset_index(drop=True)

    s = pd.DataFrame(index=so_start, data={'stockout_min':1} , dtype='int8')
    e= pd.DataFrame(index=so_end, data={'stockout_min':0},  dtype='int8')
    df = pd.concat((s, e)).sort_index()

    return df
```



#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-91-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.22.0
pytest: 3.8.2
pip: 18.1
setuptools: 38.5.1
Cython: None
numpy: 1.14.0
scipy: 1.1.0
pyarrow: 0.9.0
xarray: None
IPython: 6.2.1
sphinx: None
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2018.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.2
openpyxl: 2.5.4
xlrd: 1.1.0
xlwt: None
xlsxwriter: 1.1.1
lxml: 4.2.3
bs4: None
html5lib: 1.0.1
sqlalchemy: 1.2.10
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None


</details>
