You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jupyter Notebook报错ValueError: min() arg为空序列的求助

解决ValueError: min() arg is an empty sequence错误

我读取CSV文件作为数据集,用以下代码筛选数据运行正常:

isolated = diff['2012-09-28' <= pd.to_datetime(diff['start_date'])]
isolated

但运行以下代码时触发ValueError: min() arg is an empty sequence错误:

counter = 0
xlim = []
current = ''
for date in isolated['start_date']:
    if date in debates and current != date:
        xlim.append(counter)
        current = date
    counter += 1

x_min = min(xlim)
x_max = max(xlim)
print(str(x_min) +" - " + str(x_max))
xlim.append(max(xlim)-5)
print(xlim)

预期输出应为:

37 - 65
[37, 55, 65, 60]
diff.plot('Start Date','Difference',figsize=(12,4), marker='o', linestyle='-',color='blue',xlim=(x_min, x_max))

问题原因

报错本质是xlim为空列表——循环里的date in debates条件从未成立,导致没有任何元素被添加到xlim中,调用min()和max()时自然报错。

解决步骤

  1. 检查日期格式匹配:确认debates中的日期格式和isolated['start_date']完全一致。比如如果isolated['start_date']是datetime对象,而debates里是字符串,就会导致date in debates永远为False,需要统一两者的格式(比如都转成datetime或字符串)。

  2. 添加空列表判断:在调用min()/max()前先检查xlim是否为空,避免报错:

counter = 0
xlim = []
current = ''
for date in isolated['start_date']:
    if date in debates and current != date:
        xlim.append(counter)
        current = date
    counter += 1

if xlim:
    x_min = min(xlim)
    x_max = max(xlim)
    print(f"{x_min} - {x_max}")
    xlim.append(x_max -5)
    print(xlim)
    # 绘图代码修正xlim参数格式
    diff.plot('Start Date','Difference',figsize=(12,4), marker='o', linestyle='-',color='blue',xlim=(x_min, x_max))
else:
    print("未找到匹配的日期,无法生成x轴范围")
  1. 修正绘图参数:原预期的xlim=(, x_max)是语法错误,正确格式应为xlim=(x_min, x_max)。

内容的提问来源于stack exchange,提问作者No One

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 21:15:44