2016-03-16 41 views
1

我知道我做错了,但我没有看到如何做到这一点。在我执行的代码中,我所使用的系列来自一个大型的数据框,我已经完成了一个小组。在真实代码中,分配发生在循环中,从系列中选择不同的切片。Numpy isfinite typeerror

下面的代码导致TypeError:ufunc'isfinite'不支持输入类型。我确信这是由于引用,而不是副本或者某行的内容,但是我仅仅在Python中不够好,无法理解如何修复它。

import sys 
import numpy as np 
import pandas as pd 
import random 
import matplotlib.pyplot as plt 

plt.ion() 
period = 36 
ward_dates = pd.date_range(start=pd.datetime.today(), freq='M', periods=period) 
list_size = 6 
ward_counts = [[] for i in range(list_size)] 
for idx in range(list_size): 
    current_list = [np.random.randint(0, 500000) for r in range(period)] 
    ward_counts[idx] = current_list 
plt.stackplot(ward_dates, ward_counts) 
plt.close('all') 

units = np.random.choice(['U1', 'U2', 'U3'], size=period) 
index = pd.MultiIndex.from_arrays([units, ward_dates]) 
s1 = pd.Series([np.random.randint(0, 5000) for r in range(period)], index=index) 

ward_counts = [[] for i in range(3)] 
l1 = [int(x) for x in s1.loc['U1'].values] 
ward_counts[0] = l1 
l1 = [int(x) for x in s1.loc['U2'].values] 
ward_counts[1] = l1 
l1 = [int(x) for x in s1.loc['U3'].values] 
ward_counts[2] = l1 
plt.stackplot(ward_dates, ward_counts) 

第一个堆栈图工作得很好。第二次失败。因此,我知道问题在于抓住价值。结果列表都看起来是相同的(整数值相同),但第二次失败。

我看过切片,deepcopy等,但他们继续失败,所以根本问题是我缺乏理解。我也确定这是其他许多人的重复,但我只是没有把握这个问题。

更新

下面是引用

File "C:/Users/dgrawroc/PycharmProjects/fs_admin_reports/fs_admin_reports/oldstuff.py", line 47, in main plt.stackplot(ward_dates, ward_counts) File "C:\Users\dgrawroc\Documents\Anaconda3\envs\m2\lib\site-packages\matplotlib\pyplot.py", line 3326, in stackplot ret = ax.stackplot(x, *args, **kwargs) File "C:\Users\dgrawroc\Documents\Anaconda3\envs\m2\lib\site-packages\matplotlib__init__.py", line 1811, in inner return func(ax, *args, **kwargs) File "C:\Users\dgrawroc\Documents\Anaconda3\envs\m2\lib\site-packages\matplotlib\axes_axes.py", line 4443, in stackplot return mstack.stackplot(self, x, *args, **kwargs) File "C:\Users\dgrawroc\Documents\Anaconda3\envs\m2\lib\site-packages\matplotlib\stackplot.py", line 113, in stackplot **kwargs)) File "C:\Users\dgrawroc\Documents\Anaconda3\envs\m2\lib\site-packages\matplotlib__init__.py", line 1811, in inner return func(ax, *args, **kwargs) File "C:\Users\dgrawroc\Documents\Anaconda3\envs\m2\lib\site-packages\matplotlib\axes_axes.py", line 4608, in fill_between y2 = ma.masked_invalid(self.convert_yunits(y2)) File "C:\Users\dgrawroc\Documents\Anaconda3\envs\m2\lib\site-packages\numpy\ma\core.py", line 2293, in masked_invalid condition = ~(np.isfinite(a)) TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

+0

忘了补充我是对NumPy的1.10.4和蟒蛇3.5如果这有所作为 – cryptoref

+0

我没有看到任何'isfinite'调用...你能提供完整的Traceback吗?还建议编辑问题,而不是提供额外的细节作为评论。 – MSeifert

回答

1

下面的代码做同样的事情,它从数据帧中选择,然后显示在stackplot选择。 我认为错误是相当晦涩,难以弄清楚,但它也使我写出更好的代码

这里是工作代码:

period = 36 
    first_month = datetime.datetime(2013, 1,1) 
    last_month = datetime.datetime(2015, 12, 1) 
    ward_dates1 = pd.date_range(start=first_month, freq='M', periods=period) 

    units = np.random.choice(['U1', 'U2', 'U3'], size=period) 
    index = pd.MultiIndex.from_arrays([units, ward_dates1]) 
    s1 = pd.Series([np.random.randint(0, 5000) for r in range(period)], index=index) 

    t1 = s1.loc['U1'] 
    t2 = s1.loc['U2'] 
    t3 = s1.loc['U3'] 

    t1r = t1.reindex(ward_dates1, fill_value=0) 
    t2r = t2.reindex(ward_dates1, fill_value=0) 
    t3r = t3.reindex(ward_dates1, fill_value=0) 

    tall = np.vstack((t1r, t2r, t3r)) 
    plt.stackplot(ward_dates1, tall) 
相关问题