python
type_error
ai_generated
true
TypeError: float() 参数必须是字符串或实数,而不是 'datetime.datetime'
TypeError: float() argument must be a string or a real number, not 'datetime.datetime'
ID: python/matplotlib-date-converter-error
80%修复率
85%置信度
0证据数
2024-04-09首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
在未注册日期转换器的情况下将 naive datetime 对象直接传给 plot(),或在同一轴上混用 datetime 与数值。
English
Passing naive datetime objects directly to plot() without registering a date converter, or mixing datetime with numeric on the same axis.
解决方案
-
94% 成功率
import matplotlib.dates as mdates dates = mdates.date2num(dt_list) ax.plot(dates, values) ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) -
90% 成功率
import pandas as pd pd.plotting.register_matplotlib_converters() ax.plot(df['date'], df['value'])
无效尝试
常见但无效的做法:
-
75% 失败
Strings become categorical labels, destroying the time axis ordering and tick formatting.
-
60% 失败
Mixing converted and unconverted series still triggers the float() error on the unconverted one.