我一直在尝试创建这个机器学习工具来预测明年每月的订单量,但我一直收到此错误:
ValueError: to assemble mappings requires at least that [year, month, day] be specified: [month] is missing
这是我的代码。我正在传递月份,应该为它分配一个代表相应月份的数字,但出于某种原因,这似乎没有发生。我还知道月份并非全部大写,但这不应该是问题,因为它们都转为小写。
import pandas as pd
# Example DataFrame creation from CSV (replace this with your actual CSV upload logic)
data = {
'Year': [2021, 2021, 2021, 2022, 2022, 2023, 2023],
'Month': ['january', 'february', 'march', 'january', 'february', 'march', 'april'],
'OrderCount': [60, 55, 70, 64, 56, 76, 70]
}
df = pd.DataFrame(data)
# Convert 'Month' to numerical values (January = 1, February = 2, etc.)
month_map = {
'january': 1, 'february': 2, 'march': 3, 'april': 4, 'may': 5, 'june': 6,
'july': 7, 'august': 8, 'september': 9, 'october': 10, 'november': 11, 'december': 12
}
# Map month names to numbers
df['Month'] = df['Month'].str.lower()
df['MonthNum'] = df['Month'].map(month_map)
# Convert Year and MonthNum to integers
df['Year'] = df['Year'].astype(int)
df['MonthNum'] = df['MonthNum'].astype(int)
# Combine Year and Month into a DateTimeIndex
# The next line is where the issue is likely occurring
df['Date'] = pd.to_datetime(df[['Year', 'MonthNum']].assign(DAY=1))
# Print the resulting DataFrame to see if 'Date' was successfully created
print(df)
如果您检查 to_datetime 文档,您会发现它需要名为
month
的列。您的 month
列包含月份名称。
您应该在使用
to_datetime
之前重命名列,如下所示:
df=df.rename(columns={"Month": "MonthName", "MonthNum": "Month"})
。这样,pandas 就会寻找 month
数字列并找到它。