Wie man pandas-/matplotlib-Fehler behebt: raise ValueError(f'Date ordinal {x} converts to {dt}..
Problem:
Sie versuchen, einen pandas-DataFrame oder eine Series zu plotten mit Code wie
plot_workaround.py
df.plot()aber Sie sehen eine Fehlermeldung wie
matplotlib_date_ordinal_error.txt
Traceback (most recent call last):
...
File "/usr/local/lib/python3.11/site-packages/matplotlib/figure.py", line 3390, in savefig
self.canvas.print_figure(fname, **kwargs)
File "/usr/local/lib/python3.11/site-packages/matplotlib/backend_bases.py", line 2164, in print_figure
self.figure.draw(renderer)
File "/usr/local/lib/python3.11/site-packages/matplotlib/artist.py", line 95, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/matplotlib/figure.py", line 3154, in draw
mimage._draw_list_compositing_images(
File "/usr/local/lib/python3.11/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 3034, in draw
self._update_title_position(renderer)
File "/usr/local/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 2969, in _update_title_position
bb = ax.xaxis.get_tightbbox(renderer)
File "/usr/local/lib/python3.11/site-packages/matplotlib/axis.py", line 1334, in get_tightbbox
ticks_to_draw = self._update_ticks()
File "/usr/local/lib/python3.11/site-packages/matplotlib/axis.py", line 1276, in _update_ticks
major_labels = self.major.formatter.format_ticks(major_locs)
File "/usr/local/lib/python3.11/site-packages/matplotlib/ticker.py", line 216, in format_ticks
return [self(value, i) for i, value in enumerate(values)]
File "/usr/local/lib/python3.11/site-packages/matplotlib/ticker.py", line 216, in <listcomp>
return [self(value, i) for i, value in enumerate(values)]
File "/usr/local/lib/python3.11/site-packages/matplotlib/dates.py", line 649, in __call__
result = num2date(x, self.tz).strftime(self.fmt)
File "/usr/local/lib/python3.11/site-packages/matplotlib/dates.py", line 543, in num2date
return _from_ordinalf_np_vectorized(x, tz).tolist()
File "/usr/local/lib/python3.11/site-packages/numpy/lib/function_base.py", line 2372, in __call__
return self._call_as_normal(*args, **kwargs)
File "/usr/local/lib/python3.11/site-packages/numpy/lib/function_base.py", line 2365, in _call_as_normal
return self._vectorize_call(func=func, args=vargs)
File "/usr/local/lib/python3.11/site-packages/numpy/lib/function_base.py", line 2455, in _vectorize_call
outputs = ufunc(*inputs)
File "/usr/local/lib/python3.11/site-packages/matplotlib/dates.py", line 362, in _from_ordinalf
raise ValueError(f'Date ordinal {x} converts to {dt} (usingLösung
Dieses Problem wurde auf Matplotlib Github und auch pandas Github diskutiert. Dieses Problem tritt auf, wenn Sie df.plot() verwenden, aber dann benutzerdefinierte Formatierungsoptionen für die x-Achse setzen, für DataFrames mit Datetime-Index.
Aktuell können Sie diesen Fehler umgehen, indem Sie die Daten manuell plotten:
manual_plot_workaround.py
for column in df.columns:
plt.plot(df.index.values, df[column].values, label=column)stattdessen, aber beachten Sie, dass Sie möglicherweise einige matplotlib-Optionen selbst setzen müssen (die pandas andernfalls automatisch setzen würde), wenn Sie alles manuell plotten.
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow