为什么我将"python int太大而无法转换为C long"?使用matplotlib的DateFormatter在x轴上格式化日期时出现错误? [英] 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?

查看:95
本文介绍了为什么我将"python int太大而无法转换为C long"?使用matplotlib的DateFormatter在x轴上格式化日期时出现错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案使用DateFormatter 之后,我试图绘制时间序列,并使用熊猫将其x轴标记为年份. 0.15.0和matplotlib 1.4.2:

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:

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')

最后一行抛出此错误:OverflowError: Python int too large to convert to C long ,且具有以下追溯:

The final line throws an error: OverflowError: Python int too large to convert to C long with this traceback:

C:\ Anaconda3 \ lib \ site-packages \ IPython \ core \ formatters.py:239: FormatterWarning:image/png格式化程序中的异常:Python int也是如此 较大,可以转换为C长的FormatterWarning, 追溯(最近通话 最后):

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):

文件",第1行,在 runfile('D:/username/latex_template/new_pandas_example.py',wdir ='D:/username/latex_template')

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(文件名,命名空间)

File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 580, in runfile execfile(filename, namespace)

文件 "C:\ Anaconda3 \ lib \ site-packages \ spyderlib \ widgets \ externalshell \ sitecustomize.py", 第48行,在execfile中 exec(compile(open(文件名,'rb').read(),文件名,'exec'),命名空间)

File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 48, in execfile exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

文件"D:/用户名/latex_template/new_pandas_example.py",第18行,在 fig.savefig('test.eps')

File "D:/username/latex_template/new_pandas_example.py", line 18, in fig.savefig('test.eps')

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ figure.py",行 1470,在savefig中 self.canvas.print_figure(* args,** kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1470, in savefig self.canvas.print_figure(*args, **kwargs)

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ backend_bases.py", 第2194行,在print_figure中 ** kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 2194, in print_figure **kwargs)

文件 "C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ backends \ backend_ps.py", 第992行,在print_eps中 返回self._print_ps(outfile,'eps',* args,** 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)

文件 "C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ backends \ backend_ps.py", 第1020行,在_print_ps中 ** kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1020, in _print_ps **kwargs)

文件 "C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ backends \ backend_ps.py", _print_figure中的第1110行 self.figure.draw(renderer)

File "C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1110, in _print_figure self.figure.draw(renderer)

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ artist.py",第59行, 在draw_wrapper中 绘制(艺术家,渲染器,* args,** kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper draw(artist, renderer, *args, **kwargs)

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ figure.py",行 1079,开奖 func(* args)

File "C:\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1079, in draw func(*args)

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ artist.py",第59行, 在draw_wrapper中 绘制(艺术家,渲染器,* args,** kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper draw(artist, renderer, *args, **kwargs)

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ axes_base.py",行 2092年开奖 a.draw(renderer)

File "C:\Anaconda3\lib\site-packages\matplotlib\axes_base.py", line 2092, in draw a.draw(renderer)

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ artist.py",第59行, 在draw_wrapper中 绘制(艺术家,渲染器,* args,** kwargs)

File "C:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper draw(artist, renderer, *args, **kwargs)

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ axis.py",行1114, 平局 ticks_to_draw = self._update_ticks(渲染器)

File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1114, in draw ticks_to_draw = self._update_ticks(renderer)

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ axis.py",行957, 在_update_ticks中 tick_tups = [t for self.iter_ticks()中的t]

File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 957, in _update_ticks tick_tups = [t for t in self.iter_ticks()]

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ axis.py",行957, 在 tick_tups = [t for self.iter_ticks()中的t]

File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 957, in tick_tups = [t for t in self.iter_ticks()]

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ axis.py",第905行, 在iter_ticks 对于我来说,枚举中的val(majorLocs)]

File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 905, in iter_ticks for i, val in enumerate(majorLocs)]

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ axis.py",第905行, 在 对于我来说,枚举中的val(majorLocs)]

File "C:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 905, in for i, val in enumerate(majorLocs)]

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ dates.py",第411行, 在致电中 dt = num2date(x,self.tz)

File "C:\Anaconda3\lib\site-packages\matplotlib\dates.py", line 411, in call dt = num2date(x, self.tz)

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ dates.py",第345行, 在num2date 返回_from_ordinalf(x,tz)

File "C:\Anaconda3\lib\site-packages\matplotlib\dates.py", line 345, in num2date return _from_ordinalf(x, tz)

文件"C:\ Anaconda3 \ lib \ site-packages \ matplotlib \ dates.py",第225行, 在_from_ordinalf中 dt = datetime.datetime.fromordinal(ix)

File "C:\Anaconda3\lib\site-packages\matplotlib\dates.py", line 225, in _from_ordinalf dt = datetime.datetime.fromordinal(ix)

OverflowError:Python int太大,无法转换为C long

OverflowError: Python int too large to convert to C long

我在这里错误地使用了DateFormatter吗?如何轻松地将年(或任何时间格式,因为我的时间序列可能不同)放在matplotlib图形的a轴上?

Am I using DateFormatter incorrectly 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?

推荐答案

这是熊猫0.15中的回归"(由于Index的重构),请参见

This is a 'regression' in pandas 0.15 (due to the refactor of Index), see https://github.com/matplotlib/matplotlib/issues/3727 and https://github.com/pydata/pandas/issues/8614, but is fixed in 0.15.1.

简短的故事:matplotlib现在将pandas索引视为datetime64[ns]值的数组(实际上是非常大的int64s),而不是Timestamps的数组(后者是datetime.datetime的子类,并且可以由matplotlib处理) ).因此,潜在的原因是matplotlib不会将datetime64当作日期值处理,而是将它们处理为整数.

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.

对于熊猫0.15.0(但最好将其升级到较新版本),有两种可能的解决方法:

For pandas 0.15.0 (but better upgrade to a newer version), there are two possible workarounds:

  • 注册datetime64类型,因此matplotlib也将其作为日期处理:

  • Register the datetime64 type, so it will also be handled as a date by matplotlib:

units.registry[np.datetime64] = pd.tseries.converter.DatetimeConverter()

  • 或使用to_pydatetime方法将DatetimeIndex(具有datetime64值)转换为datetime.datetime值的数组,并绘制以下内容:

  • Or convert the DatetimeIndex (with datetime64 values) to an array of datetime.datetime values with the to_pydatetime method, and plot this:

    ax1.plot(data.index.to_pydatetime(), data.GS10)
    

  • 相关问题: 查看全文

    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆