我知道我做错了,但我不知道怎么做才对。在我的执行代码中,我所使用的序列来自于一个大型数据帧,我在这个数据帧上执行了一个groupby。在实际代码中,赋值是在循环中进行的,从序列中选择不同的片段。
下面的代码导致类型错误:输入类型不支持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)
第一个stackplot工作得很好。第二次失败了。因此,我知道问题在于获取价值观。结果列表看起来都是相同的(具有相同数量值的整数),但是第二个列表失败了。
我看过切片、深度复制等,但它们仍然失败,所以根本的问题是我缺乏理解。我也相信这是许多其他的复制品,但我只是不明白这个问题。
更新
这是追踪器
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''
下面的代码做同样的事情,它从数据框中选择,然后在stackplot中显示选择。 我认为这个错误相当隐晦,很难理解,但它确实让我编写了更好的代码
以下是工作代码:
相关问题 更多 >
编程相关推荐