在python中导入大 pandas 会改变matplotlib如何处理datetime对象? [英] Importing pandas in python changes how matplotlib handles datetime objects?

查看:145
本文介绍了在python中导入大 pandas 会改变matplotlib如何处理datetime对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的debian挤压系统上,我遇到了一个可以解释为以下的python问题:

  import numpy 
import datetime
from matplotlib import pyplot
x = [datetime.datetime.utcfromtimestamp(i)for i in numpy.arange(100000,200000,3600)]
y = range(len( x))

#看到matplotlib处理一系列datetimes只是罚款..
pyplot.plot(x,y)
#[< matplotlib.lines.Line2D对象在0xad10f4c>]

import pandas

#现在我们正好尝试了以前的工作..
pyplot.plot(x,y)
Traceback(最近的电话最后):
文件< stdin>,第1行,< module>
文件/usr/lib/pymodules/python2.6/matplotlib/pyplot.py,第2141行,绘图
ret = ax.plot(* args,** kwargs)
文件/usr/lib/pymodules/python2.6/matplotlib/axes.py,第3432行,在
中,对于self._get_lines(* args,** kwargs)中的行:
文件 /usr/lib/pymodules/python2.6/matplotlib/axes.py,第311行,_grab_next_args
for self._plot_args(remaining,kwargs):
文件/ usr / lib / pymodules / python2.6 / matplotlib / axes.py,第288行,_plot_args
x,y = self._xy_from_xy(x,y)
文件/usr/lib/pymodules/python2.6/ matplotlib / axes.py,第204行,_xy_from_xy
bx = self.axes.xaxis.update_units(x)
文件/usr/lib/pymodules/python2.6/matplotlib/axis.py ,line 982,in update_units
self._update_axisinfo()
文件/usr/lib/pymodules/python2.6/matplotlib/axis.py,第994行,在_update_axisinfo
信息中= self.converter.axisinfo(self.unit,self)
文件/usr/local/lib/python2.6/dist-packages/pandas /tseries/converter.py,第184行,在axisinfo
majfmt = PandasAutoDateFormatter(majloc,tz = tz)
文件/usr/local/lib/python2.6/dist-packages/pandas/ $ __(self,locator,tz,defaultfmt)
TypeError:__init __()最多需要3个参数(4个给定)

我对所显示的特定错误的原因不感兴趣,很明显,大熊猫预计会有不同的版本的matplotlib - 这是一个公平的风险,从标准的debian存储库和另一个通过pip获得一个包,我已经通过允许pip升级matplotlib'解决了这一部分问题。



真正的问题是 - 现在有三个问题:如何只是导入大熊猫破坏了matplotlib处理日期时间对象的能力,当两行之前的熊猫显然是甚至没有参与同样的操作?导入后的大熊猫会静静地改变顶级命名空间中的其他模块,以强制他们使用熊猫方法吗?这是python模块的可接受的行为吗?因为我需要能够依赖它导入一个随机数字模块,不会默默地改变,比如说,腌汁模块对它写的所有东西应用随机盐。



更新信息



python是2.6.6(当前debian从包2.6.6-3 +这是一个debian的0.99.3-1(目前的debian稳定从包python-matplotlib)



>熊猫版本是0.9.0(安装pip install pandas,前一段 - 不是今天)



平台是一个i386运行debian Squeeze



复制步骤


  1. (显而易见)引导一个干净的debian压缩i386安装和chroot进入它。

  2. apt-get更新

  3. apt-get install python python-matplotlib

  4. apt-get install python-pip build-essential python-dev

  5. pip install --upgrade numpy

  6. pip安装大熊猫

现在开始一个交互式python会话

  import numpy 
import datetime
#下一行添加到原始示例,以避免chroot中的DISPLAY麻烦
import matplotlib
matplotlib .use('agg')
从matplotlib import pyplot

x = [datetime.datetime.utcfromtimestamp(i)for i in numpy.arange(100000,200000,3600)]
y = range(len(x))

pyplot.plot(x,y)

import pandas

pyplot.plot(x,y )


解决方案

当您导入 它使用 matplotlib 注册了一堆单元转换器。这是从两个库的更新版本,但我认为整体行为是一样的。

 在[4]中:将matplotlib.units作为muints导入

在[5]中:muints。注册表
Out [5]:
{datetime.date:< matplotlib.dates.DateConverter instance at 0x2ab8908>
datetime.datetime:< matplotlib.dates.DateConverter instance at 0x2ab8ab8>


在[6]中:import pandas

在[7]中:muints.registry
输出[7]:
{ pandas.tseries.period.Period:< pandas.tseries.converter.PeriodConverter instance at 0x2627e60>,
pandas.tslib.Timestamp:< pandas.tseries.converter.DatetimeConverter instance at 0x264ea28>
datetime.date:< pandas.tseries.converter.DatetimeConverter instance at 0x2532fc8>
datetime.datetime:< pandas.tseries.converter.DatetimeConverter instance at 0x2627ab8>
datetime.time: pandas.tseries.converter.TimeConverter实例在0x2532f38>}

此注册表由(有几层重新定向)来确定ine如何格式化不是数字的信息,并且它匹配它试图标记的事物的类(因此,字典中的条目键入到 datetime。* )。



我怀疑您可以通过更换 dict


中的违规条目来解决此问题

On my debian squeeze system, I ran into a python problem that can be distilled to the following:

import numpy
import datetime
from matplotlib import pyplot
x = [datetime.datetime.utcfromtimestamp(i) for i in numpy.arange(100000,200000,3600)]
y = range(len(x))

# See matplotlib handle a series of datetimes just fine..
pyplot.plot(x, y)
# [<matplotlib.lines.Line2D object at 0xad10f4c>]

import pandas

# Now we try exactly what we did before..
pyplot.plot(x, y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.6/matplotlib/pyplot.py", line 2141, in plot
    ret = ax.plot(*args, **kwargs)
  File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 3432, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 311, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 288, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 204, in _xy_from_xy
    bx = self.axes.xaxis.update_units(x)
  File "/usr/lib/pymodules/python2.6/matplotlib/axis.py", line 982, in update_units
    self._update_axisinfo()
  File "/usr/lib/pymodules/python2.6/matplotlib/axis.py", line 994, in _update_axisinfo
    info = self.converter.axisinfo(self.units, self)
  File "/usr/local/lib/python2.6/dist-packages/pandas/tseries/converter.py", line 184, in axisinfo
    majfmt = PandasAutoDateFormatter(majloc, tz=tz)
  File "/usr/local/lib/python2.6/dist-packages/pandas/tseries/converter.py", line 195, in __init__
    dates.AutoDateFormatter.__init__(self, locator, tz, defaultfmt)
TypeError: __init__() takes at most 3 arguments (4 given)

I'm not interested in the cause of the particular error shown, it's obvious enough that pandas expected a different version of matplotlib -- that's a fair risk of getting one package from the standard debian repository and the other through pip, and I already 'solved' that part of the problem by allowing pip to upgrade matplotlib.

The real issue is -- and now comes a threefold question: how can it be that just importing pandas broke matplotlib's ability to handle datetime objects, when just two lines earlier pandas was clearly not even involved in that same operation? Does pandas upon import silently alter other modules in the top level namespace to force them to make use of pandas methods? And is this acceptable behavour for a python module? Because I need to be able to rely on it that importing, say, a random number module, won't silently change, say, the pickle module to apply a random salt to everything it writes..

Update with further information

python is 2.6.6 (current debian stable from package 2.6.6-3+squeeze7)

matplotlib version was debian's 0.99.3-1 (current debian stable from package python-matplotlib)

pandas version was 0.9.0 (installed with 'pip install pandas', a while ago -- not today)

Platform is an i386 running debian Squeeze

Steps to replicate

  1. (obvious) Bootstrap a clean debian squeeze i386 installation and chroot into it.
  2. apt-get update
  3. apt-get install python python-matplotlib
  4. apt-get install python-pip build-essential python-dev
  5. pip install --upgrade numpy
  6. pip install pandas

Now start an interactive python session

import numpy
import datetime
# Next two lines added to original example to avoid hassle with DISPLAY in chroot
import matplotlib
matplotlib.use('agg')
from matplotlib import pyplot

x = [datetime.datetime.utcfromtimestamp(i) for i in numpy.arange(100000,200000,3600)]
y = range(len(x))

pyplot.plot(x, y)

import pandas

pyplot.plot(x, y)

解决方案

When you import pandas it registers a bunch of unit converters with matplotlib. This is from more updated versions of both libraries, but I assume that the overall behavior is the same.

In [4]: import matplotlib.units as muints

In [5]: muints.registry
Out[5]: 
  {datetime.date: <matplotlib.dates.DateConverter instance at 0x2ab8908>,
   datetime.datetime: <matplotlib.dates.DateConverter instance at 0x2ab8ab8>}


In [6]: import pandas

In [7]: muints.registry
Out[7]: 
{pandas.tseries.period.Period: <pandas.tseries.converter.PeriodConverter instance at 0x2627e60>,
 pandas.tslib.Timestamp: <pandas.tseries.converter.DatetimeConverter instance at 0x264ea28>,
 datetime.date: <pandas.tseries.converter.DatetimeConverter instance at 0x2532fc8>,
 datetime.datetime: <pandas.tseries.converter.DatetimeConverter instance at 0x2627ab8>,
 datetime.time: <pandas.tseries.converter.TimeConverter instance at 0x2532f38>}

This registry is used by axis (with a few layers of re-direction) to determine how to format information that is not numbers and it matches on the class of the thing it is trying to label with (hence, the entries in the dictionary keyed to datetime.*).

I suspect you can fix this by replacing the offending entries in dict

这篇关于在python中导入大 pandas 会改变matplotlib如何处理datetime对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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