ValueError: Dataframe must have columns 'ds' and 'y' - Using Prophet in Google Colab

181 views Asked by At

I a new to Python and attempting to generate forecast using prophet. This code is run on Colab and I am getting the error: ValueError: Dataframe must have columns "ds" and "y" with the dates and values respectively. I have varified that the data and the column names are loaded correctly.

This code was generated by ChatGPT and it cannot resolve the issue.

import pandas as pd from prophet import Prophet import matplotlib.pyplot as plt

Read sales data from an external CSV file

data = pd.read_csv('/content/sale01.txt') # Replace with the actual path to your file

Assuming your CSV file has columns named 'date', 'sales_item1', 'sales_item2', etc.

You may need to adjust column names.

data = data.rename(columns={'date': 'ds', 'sales_item1': 'y_item1', 'sales_item2': 'y_item2'})

Initialize an empty list to store forecast DataFrames

forecasts = []

Define items

items = ['item1', 'item2', 'item3'] # Add more items as needed

Iterate over items

for item in items: # Initialize and fit the Prophet model for the current item model = Prophet() model.fit(data[['ds', f'y_{item}']])

# Create a DataFrame for future dates
future_dates = model.make_future_dataframe(periods=12, freq='M')  # Forecasting for 12 months

# Generate forecasts
forecast = model.predict(future_dates)

# Extract relevant columns (ds: dates, yhat: forecasted values)
forecast = forecast[['ds', 'yhat']]

# Append the forecast DataFrame to the list
forecasts.append({'item': item, 'forecast': forecast})

# Plot forecast
fig = plt.figure(figsize=(10, 6))
plt.plot(forecast['ds'], forecast['yhat'], label='Forecasted Sales', color='b')
plt.scatter(data['ds'], data[f'y_{item}'], label='Actual Sales', color='r')
plt.xlabel('Date')
plt.ylabel('Sales')
plt.title(f'Sales Forecast - {item}')
plt.legend()
plt.show()

Access individual forecasts by item name

for result in forecasts: print(f"Forecast for {result['item']}:") print(result['forecast'].tail(12)) # Display the last 12 forecasted values

You help is much appreciated.

0

There are 0 answers