File size: 635 Bytes
5520b18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import pandas as pd
from sklearn.model_selection import train_test_split
def split_time_series_data(date_column,X, y, test_size=0.2):
    """Split time series data into training and testing sets."""

    # Sort the data by date
    X_sorted = X.sort_values(by=date_column)
    y_sorted = y.loc[X_sorted.index]
    
    # Calculate the split index
    split_index = int(len(X_sorted) * (1 - test_size))
    
    # Split the data
    X_train, X_test = X_sorted.iloc[:split_index], X_sorted.iloc[split_index:]
    y_train, y_test = y_sorted.iloc[:split_index], y_sorted.iloc[split_index:]
    
    return X_train, X_test, y_train, y_test