I want to do a time series cross validation based on group (grp column). In the below sample data, Temperature is my target variable
import numpy as np
import pandas as pd
timeS=pd.date_range(start='1980-01-01 00:00:00', end='1980-01-01 00:00:05',
freq='S')
df = pd.DataFrame(dict(time=timeS, grp=['A']*3 + ['B']*3, material=[1,2,3]*2,
temperature=['2.4','5','9.9']*2))
grp material temperature time
0 A 1 2.4 1980-01-01 00:00:00
1 A 2 5 1980-01-01 00:00:01
2 A 3 9.9 1980-01-01 00:00:02
3 B 1 2.4 1980-01-01 00:00:03
4 B 2 5 1980-01-01 00:00:04
5 B 3 9.9 1980-01-01 00:00:05
i am planing to add some lag features based on grp using this code.
df.groupby("grp")['temperature'].shift(-1)
0 5
1 9.9
2 NaN
3 5
4 9.9
5 NaN
Name: temperature, dtype: object
The problem now i have is when i do cross validation I can using this function from sklearn sklearn.model_selection.TimeSeriesSplit but it does not take into consideration of the group effect. Can anyone tell me how to do the CV split per group (like stratified split)? I am going to use xgboost.cv for cv if that helps.
Edit: Time changes per group. Time increases uniformly (per second) within the group
The following should do it: