Você pode usar a função transform para preservar a cardinalidade do seu DataFrame.
Criando DataFrame de exemplo:
import pandas as pd
from datetime import datetime
df = pd.DataFrame(
[
(0,datetime(2021,12,1,10,0,1)),
(0,datetime(2021,12,1,10,0,2)),
(0,datetime(2021,12,1,10,0,3)),
(0,datetime(2021,12,1,10,0,4)),
(0,datetime(2021,12,1,10,0,5)),
(1,datetime(2021,12,1,10,0,1)),
(1,datetime(2021,12,1,10,1,2)),
(1,datetime(2021,12,1,10,1,3)),
(1,datetime(2021,12,1,10,1,4)),
(1,datetime(2021,12,1,10,1,5)),
(2,datetime(2021,12,1,10,0,1)),
(2,datetime(2021,12,1,10,2,2)),
(2,datetime(2021,12,1,10,2,3)),
(2,datetime(2021,12,1,10,2,4)),
(2,datetime(2021,12,1,10,2,5)),
(3,datetime(2021,12,1,10,0,1)),
(3,datetime(2021,12,1,10,3,2)),
(3,datetime(2021,12,1,10,3,3)),
(3,datetime(2021,12,1,10,3,4)),
(3,datetime(2021,12,1,10,3,5)),
(4,datetime(2021,12,1,10,0,1)),
(4,datetime(2021,12,1,10,4,2)),
(4,datetime(2021,12,1,10,4,3)),
(4,datetime(2021,12,1,10,4,4)),
(4,datetime(2021,12,1,10,4,5)),
], columns=["cycle","dateTime"]
)
df.to_markdown() #<-- usando o markdown de saída a seguir:
|
cycle |
dateTime |
0 |
0 |
2021-12-01 10:00:01 |
1 |
0 |
2021-12-01 10:00:02 |
2 |
0 |
2021-12-01 10:00:03 |
3 |
0 |
2021-12-01 10:00:04 |
4 |
0 |
2021-12-01 10:00:05 |
5 |
1 |
2021-12-01 10:00:01 |
6 |
1 |
2021-12-01 10:01:02 |
7 |
1 |
2021-12-01 10:01:03 |
8 |
1 |
2021-12-01 10:01:04 |
9 |
1 |
2021-12-01 10:01:05 |
10 |
2 |
2021-12-01 10:00:01 |
11 |
2 |
2021-12-01 10:02:02 |
12 |
2 |
2021-12-01 10:02:03 |
13 |
2 |
2021-12-01 10:02:04 |
14 |
2 |
2021-12-01 10:02:05 |
15 |
3 |
2021-12-01 10:00:01 |
16 |
3 |
2021-12-01 10:03:02 |
17 |
3 |
2021-12-01 10:03:03 |
18 |
3 |
2021-12-01 10:03:04 |
19 |
3 |
2021-12-01 10:03:05 |
20 |
4 |
2021-12-01 10:00:01 |
21 |
4 |
2021-12-01 10:04:02 |
22 |
4 |
2021-12-01 10:04:03 |
23 |
4 |
2021-12-01 10:04:04 |
24 |
4 |
2021-12-01 10:04:05 |
Incluindo a Coluna deltaT
df['deltaT'] = df.groupby('cycle')['dateTime'].transform('max') - df.groupby('cycle')['dateTime'].transform('min')
df.to_markdown()
|
cycle |
dateTime |
deltaT |
0 |
0 |
2021-12-01 10:00:01 |
0 days 00:00:04 |
1 |
0 |
2021-12-01 10:00:02 |
0 days 00:00:04 |
2 |
0 |
2021-12-01 10:00:03 |
0 days 00:00:04 |
3 |
0 |
2021-12-01 10:00:04 |
0 days 00:00:04 |
4 |
0 |
2021-12-01 10:00:05 |
0 days 00:00:04 |
5 |
1 |
2021-12-01 10:00:01 |
0 days 00:01:04 |
6 |
1 |
2021-12-01 10:01:02 |
0 days 00:01:04 |
7 |
1 |
2021-12-01 10:01:03 |
0 days 00:01:04 |
8 |
1 |
2021-12-01 10:01:04 |
0 days 00:01:04 |
9 |
1 |
2021-12-01 10:01:05 |
0 days 00:01:04 |
10 |
2 |
2021-12-01 10:00:01 |
0 days 00:02:04 |
11 |
2 |
2021-12-01 10:02:02 |
0 days 00:02:04 |
12 |
2 |
2021-12-01 10:02:03 |
0 days 00:02:04 |
13 |
2 |
2021-12-01 10:02:04 |
0 days 00:02:04 |
14 |
2 |
2021-12-01 10:02:05 |
0 days 00:02:04 |
15 |
3 |
2021-12-01 10:00:01 |
0 days 00:03:04 |
16 |
3 |
2021-12-01 10:03:02 |
0 days 00:03:04 |
17 |
3 |
2021-12-01 10:03:03 |
0 days 00:03:04 |
18 |
3 |
2021-12-01 10:03:04 |
0 days 00:03:04 |
19 |
3 |
2021-12-01 10:03:05 |
0 days 00:03:04 |
20 |
4 |
2021-12-01 10:00:01 |
0 days 00:04:04 |
21 |
4 |
2021-12-01 10:04:02 |
0 days 00:04:04 |
22 |
4 |
2021-12-01 10:04:03 |
0 days 00:04:04 |
23 |
4 |
2021-12-01 10:04:04 |
0 days 00:04:04 |
24 |
4 |
2021-12-01 10:04:05 |
0 days 00:04:04 |
pd.read_clipboard()
) e o dado esperado como saída?