Speed up loop to count with python

I have a data frame like this :

<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-baqh{text-align:center;vertical-align:top}
.tg .tg-nrix{text-align:center;vertical-align:middle}
.tg .tg-0lax{text-align:left;vertical-align:top}
</style>
<table class="tg">
  <tr>
    <th class="tg-nrix">id</th>
    <th class="tg-baqh">name</th>
    <th class="tg-baqh">time</th>
    <th class="tg-0lax">count</th>
  </tr>
  <tr>
    <td class="tg-baqh">id1</td>
    <td class="tg-baqh">name1</td>
    <td class="tg-baqh">6:06:34</td>
    <td class="tg-0lax" rowspan="3">1</td>
  </tr>
  <tr>
    <td class="tg-baqh">id1</td>
    <td class="tg-baqh">name1</td>
    <td class="tg-baqh">6:07:30</td>
  </tr>
  <tr>
    <td class="tg-baqh">id1</td>
    <td class="tg-baqh">name1</td>
    <td class="tg-baqh">7:02:34</td>
  </tr>
  <tr>
    <td class="tg-baqh">id1</td>
    <td class="tg-baqh">name1</td>
    <td class="tg-baqh">7:09:10</td>
    <td class="tg-0lax" rowspan="2">2</td>
  </tr>
  <tr>
    <td class="tg-baqh">id1</td>
    <td class="tg-baqh">name1</td>
    <td class="tg-baqh">7:45:56</td>
  </tr>
  <tr>
    <td class="tg-baqh">id2</td>
    <td class="tg-baqh">name1</td>
    <td class="tg-baqh">6:14:13</td>
    <td class="tg-0lax" rowspan="2">3</td>
  </tr>
  <tr>
    <td class="tg-baqh">id2</td>
    <td class="tg-baqh">name1</td>
    <td class="tg-baqh">6:50:08</td>
  </tr>
</table>

data2

By grouping by id and name, I want to count how many times an event occur as follow : all the event between the first time it occurs till one hour later is count as one. Pandas aggregate function + count() don't do it, so i write this script for myself but it takes too much time.

```python
counts= 0

for ids in data2['id'].unique():
    for name in data2['name'].unique():
        dataint = data2.loc[(data2['id'] == ids) & (data2['name'] == name)].sort_values(ascending=True, by=['time'])
        num = len(dataint['time'])
        if num >= 1:
            to = pd.to_datetime(dataint['time'].iat[0])
            tx = pd.to_datetime(dataint['time'].iat[num-1])
            while to <= tx:
                t = to + dt.timedelta(hours=1)
                if any(pd.to_datetime(i) >= to and pd.to_datetime(i) <= t for i in dataint['time']):
                    counts= counts+1
                else:
                    counts= counts
                to = t
        else:
            counts= counts
counts
```

It works, but it is too slow. Any help to speed it up