How to work with multiple `item_id`'s of varying length for timeseries prediction in autogluon?

126 views Asked by At

I'm currently building a model to predict daily stock price based on daily data for thousands of stocks. In the data, I've got the daily data for all stocks, however they are for different lengths. Eg: for some stocks I have daily data from 2000 to 2022, and for others I have data from 2010 to 2022.

Many dates are also obviously repeated for all stocks.

While I was learning autogluon, I used the following function to format timeseries data so it can work with .fit():

def forward_fill_missing(ts_dataframe: TimeSeriesDataFrame, freq="D") -> TimeSeriesDataFrame:
    original_index = ts_dataframe.index.get_level_values("timestamp")
    start = original_index[0]
    end = original_index[-1]
    filled_index = pd.date_range(start=start, end=end, freq=freq, name="timestamp")
    return ts_dataframe.droplevel("item_id").reindex(filled_index, method="ffill")

ts_dataframe = ts_dataframe.groupby("item_id").apply(forward_fill_missing)

This worked, however I was trying this for data for just one item_id and now I have thousands.

When I use this now, I get the following error: ValueError: cannot reindex from a duplicate axis

It's important to note that I have already foreward filled my data with pandas, and the ts_dataframe shouldn't have any missing dates or values, but when I try to use it with .fit() I get the following error:

ValueError: Frequency not provided and cannot be inferred. This is often due to the time index of the data being irregularly sampled. Please ensure that the data set used has a uniform time index, or create the TimeSeriesPredictorsettingignore_time_index=True.

I assume that this is because I have only filled in missing data and dates, but not taken into account the varying number of days available for every stock individually.

For reference, here's how I have formatted the data with pandas:

df = pd.read_csv(
    "/content/drive/MyDrive/stock_data/training_data.csv",
    parse_dates=["Date"],  
)
df["Date"] = pd.to_datetime(df["Date"], errors="coerce", dayfirst=True)
df.fillna(method='ffill', inplace=True)
df = df.drop("Unnamed: 0", axis=1)
df[:11]

How can I format the data so I can use it with .fit()?

Thanks!

0

There are 0 answers