我刚刚将pandas从0.17.1更新到0.21.0以利用一些新的功能,并遇到matplotlib的兼容性问题(我也更新到最新的2.1.0).特别是,Timestamp对象似乎发生了显着变化.
我碰巧有另一台机器仍在运行旧版本的pandas(0.17.1)/ matplotlib(1.5.1),我曾用它来比较差异:
两个版本都显示我的DataFrame索引 dtype='datetime64[ns]
DatetimeIndex(['2017-03-13', '2017-03-14', ... '2017-11-17'], type='datetime64[ns]', name='dates', length=170, freq=None)
Run Code Online (Sandbox Code Playgroud)
但是在打电话时type(df.index[0]),0.17.1给出pandas.tslib.Timestamp,0.21.0给出pandas._libs.tslib.Timestamp.
当用df.indexx轴绘图时:
plt.plot(df.index, df['data'])
Run Code Online (Sandbox Code Playgroud)
默认情况下,matplotlibs将x轴标签格式化为pandas 0.17.1的日期,但无法识别pandas 0.21.0并简单地给出原始数字1.5e18(以纳秒为单位的纪元时间).
我还有一个自定义游标,通过使用matplotlib.dates.DateFormatter对于0.21.0失败的x值来报告图表上的点击位置:
OverflowError: signed integer is greater than maximum
Run Code Online (Sandbox Code Playgroud)
我可以在调试中看到,报告的x值约为736500(即自0年以来的日数)为0.17.1但约为1.5e18(即纳秒时间段)为0.21.0.
我对matplotlib和熊猫之间的兼容性中断感到惊讶,因为它们显然是大多数人一起使用的.我是否错过了上面为新版本调用上述情节功能的方式?
更新如上所述,我更喜欢直接plot使用给定的轴对象调用,但仅仅是为了它,我尝试调用DataFrame本身的plot方法df.plot().完成此操作后,所有后续绘图都会正确识别同一python会话中的时间戳.就像设置了一个环境变量一样,因为我可以重新加载另一个DataFrame或创建另一个轴,subplots而不1.5e18显示在哪里.这真的闻起来像一个臭虫,因为最新的熊猫医生说熊猫:
The plot method on Series and DataFrame is just a simple wrapper around plt.plot()
Run Code Online (Sandbox Code Playgroud)
但很明显它对python会话做了一些事情,以便后续的图正确地处理Timestamp索引.
实际上,只需在上面的pandas链接中运行示例:
import pandas as pd
import numpy as np …Run Code Online (Sandbox Code Playgroud) 我有 HH:MM:SS 格式的时间戳列表,并希望使用 datetime.time 绘制某些值。看来 python 不喜欢我这样做的方式。有人可以帮忙吗?
import datetime
import matplotlib.pyplot as plt
# random data
x = [datetime.time(12,10,10), datetime.time(12, 11, 10)]
y = [1,5]
# plot
plt.plot(x,y)
plt.show()
*TypeError: float() argument must be a string or a number*
Run Code Online (Sandbox Code Playgroud)