python data_error ai_generated true

ValueError: 日期序数737791不是公历中的有效日期。

ValueError: Date ordinal 737791 is not a valid date for the Gregorian calendar.

ID: python/matplotlib-date-parser-error

其他格式: JSON · Markdown 中文 · English
80%修复率
83%置信度
0证据数
2024-05-15首次发现

版本兼容性

版本状态引入弃用备注
3.8 active
3.9 active

根因分析

使用matplotlib的date2num或num2date时,传入的序数超出范围,通常是由于转换了公元1年之前或9999年之后的日期。

English

Using matplotlib's date2num or num2date with an out-of-range ordinal, often due to incorrect conversion of dates before year 1 or after year 9999.

generic

解决方案

  1. 90% 成功率 Ensure dates are within the Gregorian calendar range (year 1 to 9999)
    from datetime import datetime; date = datetime(2024, 1, 1); ordinal = matplotlib.dates.date2num(date)
  2. 85% 成功率 Use pandas date handling before plotting
    import pandas as pd; dates = pd.date_range('2024-01-01', periods=10); plt.plot_date(dates, values)

无效尝试

常见但无效的做法:

  1. Ignoring the error and continuing 80% 失败

    The plot may be incorrect or missing data points.

  2. Using a different date library like datetime 50% 失败

    The error is in matplotlib's conversion; datetime may still produce valid ordinals but the issue is with the range.