pandas 当我使用 matplotlib 的 DateFormatter 在 x 轴上格式化日期时,为什么会出现“python int 太大而无法转换为 C long”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26642133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get "python int too large to convert to C long" errors when I use matplotlib's DateFormatter to format dates on the x axis?
提问by Michael A
Following this answer's use of DateFormatter, I tried to plot a time series and label its x axis with years using pandas 0.15.0 and matplotlib 1.4.2:
在此答案使用 DateFormatter 之后,我尝试绘制时间序列并使用 Pandas 0.15.0 和 matplotlib 1.4.2 用年份标记其 x 轴:
import datetime as dt
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas.io.data as pdio
import scipy as sp
t1 = dt.datetime(1960, 1, 1)
t2 = dt.datetime(2014, 6, 1)
data = pdio.DataReader("GS10", "fred", t1, t2).resample("Q", how=sp.mean)
fig, ax1 = plt.subplots()
ax1.plot(data.index, data.GS10)
ax1.set_xlabel("Year")
ax1.set_ylabel("Rate (%)")
ax1.xaxis.set_major_formatter(mpl.dates.DateFormatter("%Y"))
fig.suptitle("10-yr Treasury Rate", fontsize=14)
fig.savefig('test.eps')
The final line throws an error: OverflowError: Python int too large to convert to C long
with this traceback:
最后一行抛出错误:OverflowError: Python int too large to convert to C long
使用此回溯:
C:\Anaconda3\lib\site-packages\IPython\core\formatters.py:239: FormatterWarning: Exception in image/png formatter: Python int too large to convert to C long FormatterWarning, Traceback (most recent call last):
File "", line 1, in runfile('D:/username/latex_template/new_pandas_example.py', wdir='D:/username/latex_template')
File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 580, in runfile execfile(filename, namespace)
File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 48, in execfile exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "D:/username/latex_template/new_pandas_example.py", line 18, in fig.savefig('test.eps')
File "C:\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1470, in savefig self.canvas.print_figure(*args, **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 2194, in print_figure **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 992, in print_eps return self._print_ps(outfile, 'eps', *args, **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1020, in _print_ps **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1110, in _print_figure self.figure.draw(renderer)
File "C:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper draw(artist, renderer, *args, **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1079, in draw func(*args)
File "C:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper draw(artist, renderer, *args, **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\axes_base.py", line 2092, in draw a.draw(renderer)
File "C:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper draw(artist, renderer, *args, **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1114, in draw ticks_to_draw = self._update_ticks(renderer)
File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 957, in _update_ticks tick_tups = [t for t in self.iter_ticks()]
File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 957, in tick_tups = [t for t in self.iter_ticks()]
File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 905, in iter_ticks for i, val in enumerate(majorLocs)]
File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 905, in for i, val in enumerate(majorLocs)]
File "C:\Anaconda3\lib\site-packages\matplotlib\dates.py", line 411, in calldt = num2date(x, self.tz)
File "C:\Anaconda3\lib\site-packages\matplotlib\dates.py", line 345, in num2date return _from_ordinalf(x, tz)
File "C:\Anaconda3\lib\site-packages\matplotlib\dates.py", line 225, in _from_ordinalf dt = datetime.datetime.fromordinal(ix)
OverflowError: Python int too large to convert to C long
C:\Anaconda3\lib\site-packages\IPython\core\formatters.py:239: FormatterWarning: 图像/png 格式化程序中的异常:Python int 太大而无法转换为 C long FormatterWarning, Traceback(最近一次调用最后一次):
File "", line 1, in runfile('D:/username/latex_template/new_pandas_example.py', wdir='D:/username/latex_template')
文件“C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”,第580行,在运行文件execfile(文件名,命名空间)中
文件“C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”,第 48 行,在 execfile exec(compile(open(filename, 'rb').read(), filename, 'exec '), 命名空间)
文件“D:/username/latex_template/new_pandas_example.py”,第 18 行,在 fig.savefig('test.eps')
文件“C:\Anaconda3\lib\site-packages\matplotlib\figure.py”,第 1470 行,在 savefig self.canvas.print_figure(*args, **kwargs)
文件“C:\Anaconda3\lib\site-packages\matplotlib\backend_bases.py”,第 2194 行,在 print_figure **kwargs 中)
文件“C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py”,第 992 行,在 print_eps 中返回 self._print_ps(outfile, 'eps', *args, **kwargs)
文件“C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py”,第 1020 行,在 _print_ps **kwargs 中)
文件“C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py”,第 1110 行,在 _print_figure self.figure.draw(renderer)
文件“C:\Anaconda3\lib\site-packages\matplotlib\artist.py”,第 59 行,在 draw_wrapper draw(artist, renderer, *args, **kwargs)
文件“C:\Anaconda3\lib\site-packages\matplotlib\figure.py”,第 1079 行,在 draw func(*args) 中
文件“C:\Anaconda3\lib\site-packages\matplotlib\artist.py”,第 59 行,在 draw_wrapper draw(artist, renderer, *args, **kwargs)
文件“C:\Anaconda3\lib\site-packages\matplotlib\axes_base.py”,第 2092 行,绘制 a.draw(renderer)
文件“C:\Anaconda3\lib\site-packages\matplotlib\artist.py”,第 59 行,在 draw_wrapper draw(artist, renderer, *args, **kwargs)
文件“C:\Anaconda3\lib\site-packages\matplotlib\axis.py”,第 1114 行,在绘制中 ticks_to_draw = self._update_ticks(renderer)
文件“C:\Anaconda3\lib\site-packages\matplotlib\axis.py”,第 957 行,在 _update_ticks tick_tups = [t for t in self.iter_ticks()]
文件“C:\Anaconda3\lib\site-packages\matplotlib\axis.py”,第 957 行,在 tick_tups = [t for t in self.iter_ticks()]
文件“C:\Anaconda3\lib\site-packages\matplotlib\axis.py”,第 905 行,在 i 的 iter_ticks 中,枚举中的 val(majorLocs)]
文件“C:\Anaconda3\lib\site-packages\matplotlib\axis.py”,第 905 行,用于 i,枚举中的 val(majorLocs)]
文件“C:\Anaconda3\lib\site-packages\matplotlib\dates.py”,第 411 行,调用dt = num2date(x, self.tz)
文件“C:\Anaconda3\lib\site-packages\matplotlib\dates.py”,第 345 行,在 num2date 中返回 _from_ordinalf(x, tz)
文件“C:\Anaconda3\lib\site-packages\matplotlib\dates.py”,第 225 行,在 _from_ordinalf dt = datetime.datetime.fromordinal(ix)
溢出错误:Python int 太大而无法转换为 C long
Am I using DateFormatterincorrectly here? How can I easily put years (or any time format, since my time series might differ) on the a-axis of a matplotlib figure?
我DateFormatter在这里使用不正确吗?如何轻松地将年份(或任何时间格式,因为我的时间序列可能不同)放在 matplotlib 图形的 a 轴上?
回答by joris
This is a 'regression' in pandas 0.15 (due to the refactor of Index), see https://github.com/matplotlib/matplotlib/issues/3727and https://github.com/pydata/pandas/issues/8614, but is fixed in 0.15.1.
这是Pandas 0.15 中的“回归”(由于索引的重构),请参阅https://github.com/matplotlib/matplotlib/issues/3727和https://github.com/pydata/pandas/issues/8614,但在 0.15.1 中是固定的。
Short story: matplotlib now sees the pandas index as an array of datetime64[ns]values (which are actually very large int64s), instead of an array of Timestamps (which are subclass of datetime.datetime, and can be handled by matplotlib) in previous versions of pandas. So the underlying reason is that matplotlib does not handle datetime64 as date values but as ints.
小故事:matplotlib 现在将 pandas 索引视为一个datetime64[ns]值数组(实际上是非常大的 int64),而不是以前版本的 Pandas 中的时间戳数组(它们是 datetime.datetime 的子类,可以由 matplotlib 处理) . 所以根本原因是 matplotlib 不将 datetime64 作为日期值处理,而是作为整数处理。
For pandas 0.15.0 (but better upgrade to a newer version), there are two possible workarounds:
对于 pandas 0.15.0(但最好升级到新版本),有两种可能的解决方法:
Register the
datetime64type, so it will also be handled as a date by matplotlib:units.registry[np.datetime64] = pd.tseries.converter.DatetimeConverter()Or convert the DatetimeIndex (with datetime64 values) to an array of
datetime.datetimevalues with theto_pydatetimemethod, and plot this:ax1.plot(data.index.to_pydatetime(), data.GS10)
注册
datetime64类型,因此它也将被 matplotlib 处理为日期:units.registry[np.datetime64] = pd.tseries.converter.DatetimeConverter()或者
datetime.datetime使用该to_pydatetime方法将 DatetimeIndex(带有 datetime64 值)转换为一个值数组,并绘制如下:ax1.plot(data.index.to_pydatetime(), data.GS10)
related question: Plotting datetimeindex on x-axis with matplotlib creates wrong ticks in pandas 0.15 in contrast to 0.14
相关问题:使用 matplotlib 在 x 轴上绘制日期时间索引会在 Pandas 0.15 中创建错误的刻度,而 0.14

