Spaces:
Sleeping
Sleeping
File size: 2,225 Bytes
a55ae21 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import pandas as pd
from datetime import datetime, timedelta
class DataSplitter:
def __init__(self, data, train_months, test_months=None, test_weeks=None):
if not isinstance(data, pd.DataFrame):
raise ValueError("data should be a pandas DataFrame")
if not 'Date' in data.columns:
raise ValueError("DataFrame must contain a 'Date' column")
if not 'SKU' in data.columns:
raise ValueError("DataFrame must contain a 'SKU' column")
# Convert 'Date' column to datetime
data['Date'] = pd.to_datetime(data['Date'])
# Store the data and settings
self.data = data
self.train_months = train_months
self.test_months = test_months
self.test_weeks = test_weeks
# Log information about the dataset
min_date = self.data['Date'].min()
max_date = self.data['Date'].max()
total_months = (max_date.year - min_date.year) * 12 + max_date.month - min_date.month
print(f"Min Date: {min_date}, Max Date: {max_date}, Total Months: {total_months}")
def split_data(self):
# Calculate the split date
max_date = self.data['Date'].max()
split_date = max_date - pd.DateOffset(months=self.train_months)
# Split the data into training and testing sets
train_data = self.data[self.data['Date'] <= split_date].copy()
test_data = self.data[self.data['Date'] > split_date].copy()
# Get the set of SKUs that are present in both the training and test sets
common_skus = set(train_data['SKU']).intersection(set(test_data['SKU']))
# Filter the training and test sets to only include the common SKUs
train_data = train_data[train_data['SKU'].isin(common_skus)]
test_data = test_data[test_data['SKU'].isin(common_skus)]
#TODO add a warning message if there are no common SKUs or how many SKUs were removed
# Log data types info
print("Train data types:")
print(train_data.dtypes)
print("\nTest data types:")
print(test_data.dtypes)
# Return the training and testing datasets
return train_data, test_data
|