Pandas类型错误试图绘图

Tom*_*son 18 python matplotlib pandas

我正在尝试基于Pandas数据帧创建基本散点图.但是当我调用分散例程时,我收到错误"TypeError:invalid type promotion".重现问题的示例代码如下所示:

t1 = pd.to_datetime('2015-11-01 00:00:00')
t2 = pd.to_datetime('2015-11-02 00:00:00')

Time = pd.Series([t1, t2])
r = pd.Series([-1, 1])

df = pd.DataFrame({'Time': Time, 'Value': r})
print(df)

print(type(df.Time))
print(type(df.Time[0]))

fig = plt.figure(figsize=(x_size,y_size))
ax = fig.add_subplot(111)
ax.scatter(df.Time, y=df.Value, marker='o')
Run Code Online (Sandbox Code Playgroud)

结果输出是

        Time  Value
0 2015-11-01     -1
1 2015-11-02      1
<class 'pandas.core.series.Series'>
<class 'pandas.tslib.Timestamp'>

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-285-f4ed0443bf4d> in <module>()
     15 fig = plt.figure(figsize=(x_size,y_size))
     16 ax = fig.add_subplot(111)
---> 17 ax.scatter(df.Time, y=df.Value, marker='o')

C:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x,    y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, **kwargs)
   3635             edgecolors = 'face'
   3636 
-> 3637         offsets = np.dstack((x, y))
   3638 
   3639         collection = mcoll.PathCollection(

C:\Anaconda3\lib\site-packages\numpy\lib\shape_base.py in dstack(tup)
    365 
    366     """
--> 367     return _nx.concatenate([atleast_3d(_m) for _m in tup], 2)
    368 
    369 def _replace_zero_by_x_arrays(sub_arys):

TypeError: invalid type promotion
Run Code Online (Sandbox Code Playgroud)

在使用datetime时,我发现了类似的帖子Pandas Series TypeError和ValueError,这表明错误是由于系列中有多种数据类型引起的.但这似乎不是我的例子中的问题,正如我正在打印的类型信息所证明的那样.

请注意,如果我停止使用pandas datetime对象并使'Time'成为一个浮点数,那么这可以正常工作,例如

t1 = 1.1 #
t2 = 1.2

Time = pd.Series([t1, t2])
r = pd.Series([-1, 1])

df = pd.DataFrame({'Time': Time, 'Value': r})
print(df)

print(type(df.Time))
print(type(df.Time[0]))

fig = plt.figure(figsize=(x_size,y_size))
ax = fig.add_subplot(111)
ax.scatter(df.Time, y=df.Value, marker='o')
Run Code Online (Sandbox Code Playgroud)

与输出

   Time  Value
0   1.1     -1
1   1.2      1
<class 'pandas.core.series.Series'>
<class 'numpy.float64'>
Run Code Online (Sandbox Code Playgroud)

并且图表看起来很好.我不知道为什么使用datetime会导致无效的类型提升错误?我使用的是Python 3.4.3和pandas 0.16.2.

Tom*_*son 10

谢谢@martinvseticka.我认为根据您指出的numpy代码,您的评估是正确的.我能够简化你的调整(并添加第三个样本点)来获得

t1 = pd.to_datetime('2015-11-01 00:00:00')
t2 = pd.to_datetime('2015-11-02 00:00:00')
t3 = pd.to_datetime('2015-11-03 00:00:00')

Time = pd.Series([t1, t2, t3])
r = pd.Series([-1, 1, 0.5])

df = pd.DataFrame({'Time': Time, 'Value': r})

fig = plt.figure(figsize=(x_size,y_size))
ax = fig.add_subplot(111)
ax.plot_date(x=df.Time, y=df.Value, marker='o')
Run Code Online (Sandbox Code Playgroud)

关键似乎是调用'plot_date'而不是'plot'.这似乎告诉mapplotlib不要尝试连接数组.


Jef*_*eff 7

还有另一种方法,我们应该放弃使用系列.只需使用列表的时间.

t1 = pd.to_datetime('2015-11-01 00:00:00')
t2 = pd.to_datetime('2015-11-02 00:00:00')

Time = pd.Series([t1, t2])
r = pd.Series([-1, 1])

df = pd.DataFrame({'Time': Time, 'Value': r})
print(df)

print(type(df.Time))
print(type(df.Time[0]))
x_size = 800
y_size = 600
fig = plt.figure(figsize=(x_size,y_size))
ax = fig.add_subplot(111)
ax.scatter(list(df.Time.values), list(df.Value.values), marker='o')
Run Code Online (Sandbox Code Playgroud)


Mar*_*cka 5

这是你想要的?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt 
import matplotlib.dates as dates

t1 = pd.to_datetime('2015-11-01 00:00:00')
t2 = pd.to_datetime('2015-11-02 00:00:00')

idx = pd.Series([t1, t2])
s = pd.Series([-1, 1], index=idx)

fig, ax = plt.subplots()
ax.plot_date(idx, s, 'v-')
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

我是Python的新手,所以希望我没有错。基本上,我尝试根据/sf/answers/957200051/调整您的示例。

您的脚本的问题是numpy 尝试连接df.Timedf.Value序列化,并且找不到适合新数组的类型,因为一个数组是数字数组,而第二个数组则由Timestamp实例组成。


小智 5

scatter图具有一些无法在plotor 中模拟的属性plot_date(作为绘制具有不同大小的标记的能力)。

将时间序列类型转换为类型pandas.tslib.Timestamp列表:datetime.datetime在绘制散点图之前对我有用:

times = [d.to_pydatetime() for d in df.Time]]
ax.scatter(times, y=df.Value, marker='o')
Run Code Online (Sandbox Code Playgroud)