简体   繁体   English

如何修复'ValueError:零尺寸数组到没有身份的归约运算fmin'

[英]How to fix 'ValueError: zero-size array to reduction operation fmin which has no identity'

I'm trying to plot some simple time Series and usually it works fine, but this special case does not get the results as expected: 我正在尝试绘制一些简单的时间序列,通常可以正常工作,但是这种特殊情况无法获得预期的结果:

I run the code in Visual Studio and CLI where I got the same Error Message. 我在获得相同错误消息的Visual Studio和CLI中运行代码。 But when I tried to run the same code in a jupyter notebook where I got three cells (CELL1, CELL2 and CELL3 separated), the whole code works fine. 但是,当我尝试在jupyter笔记本中运行相同的代码时,我得到了三个单元格(CELL1,CELL2和CELL3分开),整个代码可以正常工作。 Only when I put CELL2 and CELL3 in one single CELL, it produced the typical error again. 仅当我将CELL2和CELL3放在一个单独的CELL中时,它才再次产生典型错误。

# CELL 1
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import datetime
from pandas import Series
import sys 

input_array = np.array([[0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.]])

date_list = [datetime.date(2019, 1, 2), datetime.date(2019, 1, 9), datetime.date(2019, 1, 16), datetime.date(2019, 1, 23), datetime.date(2019, 1, 30), datetime.date(2019, 2, 6), datetime.date(2019, 2, 13), datetime.date(2019, 2, 20), datetime.date(2019, 2, 27), datetime.date(2019, 3, 6), datetime.date(2019, 3, 13), datetime.date(2019, 3, 20), datetime.date(2019, 3, 27), datetime.date(2019, 4, 3), datetime.date(2019, 4, 10)]

def get_indiv_series(table, index):
    out_series = []
    for i in table:
        out_series.append(i[index])
    return out_series

def make_indiv_category_plot(times, table, index, axis):
    print(get_indiv_series(table, index))
    series = Series(get_indiv_series(table, index), index=times)
    try:
        series.plot(style='-', ax=axis)    
    except ValueError as err:
        print(' A value Error ocurred')
        print(index)
        print(series)
        print(get_indiv_series(table, index))
        print(sys.exc_info())
        raise err
    line_i, = plt.plot([])
    return line_i
# CELL 2
fig = plt.figure(figsize=(10, 10))
ax = plt.gca()
# CELL 3
line_0 = make_indiv_category_plot(date_list, input_array, 0, ax)
line_1 = make_indiv_category_plot(date_list, input_array, 1, ax)
line_2 = make_indiv_category_plot(date_list, input_array, 2, ax)
line_3 = make_indiv_category_plot(date_list, input_array, 3, ax)
line_4 = make_indiv_category_plot(date_list, input_array, 4, ax)

RESULTS (merging CELL2 & CELL 3): 结果(合并单元2和单元3):

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
2019-01-02    0.0
2019-01-09    0.0
2019-01-16    0.0
2019-01-23    0.0
2019-01-30    0.0
2019-02-06    0.0
2019-02-13    0.0
2019-02-20    0.0
2019-02-27    0.0
2019-03-06    0.0
2019-03-13    0.0
2019-03-20    0.0
2019-03-27    0.0
2019-04-03    0.0
2019-04-10    0.0
dtype: float64
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
2019-01-02    0.0
2019-01-09    0.0
2019-01-16    0.0
2019-01-23    0.0
2019-01-30    0.0
2019-02-06    0.0
2019-02-13    0.0
2019-02-20    0.0
2019-02-27    0.0
2019-03-06    0.0
2019-03-13    0.0
2019-03-20    0.0
2019-03-27    0.0
2019-04-03    0.0
2019-04-10    0.0
dtype: float64
 A value Error ocurred
1
2019-01-02    0.0
2019-01-09    0.0
2019-01-16    0.0
2019-01-23    0.0
2019-01-30    0.0
2019-02-06    0.0
2019-02-13    0.0
2019-02-20    0.0
2019-02-27    0.0
2019-03-06    0.0
2019-03-13    0.0
2019-03-20    0.0
2019-03-27    0.0
2019-04-03    0.0
2019-04-10    0.0
dtype: float64
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
(<class 'ValueError'>, ValueError('zero-size array to reduction operation fmin which has no identity',), <traceback object at 0x000001DB4550C208>)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-814d54a9ca4b> in <module>()
      5 line_0 = make_indiv_category_plot(date_list, input_array, 0, ax)
      6 
----> 7 line_1 = make_indiv_category_plot(date_list, input_array, 1, ax)
      8 
      9 line_2 = make_indiv_category_plot(date_list, input_array, 2, ax)

<ipython-input-3-ddbd896b3735> in make_indiv_category_plot(times, table, index, axis)
     57         print(get_indiv_series(table, index))
     58         print(sys.exc_info())
---> 59         raise err
     60     # create a empty line with the same properties as the time series for legends
     61     line_i, = plt.plot([])#, color='%s'%db_fplive.Get_color_table()[index+1], label=db_fplive.Get_label_table()[index+1])

<ipython-input-3-ddbd896b3735> in make_indiv_category_plot(times, table, index, axis)
     48     # plot the series with the color and label from the category dictionaries
     49     try:
---> 50         series.plot()#style='-', ax=axis, color='%s'%db_fplive.Get_color_table()[index+1], label=db_fplive.Get_label_table()[index+1])
     51     except ValueError as err:
     52         print(' A value Error ocurred')

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pandas\plotting\_core.py in __call__(self, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds)
   2740                            colormap=colormap, table=table, yerr=yerr,
   2741                            xerr=xerr, label=label, secondary_y=secondary_y,
-> 2742                            **kwds)
   2743     __call__.__doc__ = plot_series.__doc__
   2744 

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pandas\plotting\_core.py in plot_series(data, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds)
   1996                  yerr=yerr, xerr=xerr,
   1997                  label=label, secondary_y=secondary_y,
-> 1998                  **kwds)
   1999 
   2000 

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pandas\plotting\_core.py in _plot(data, x, y, subplots, ax, kind, **kwds)
   1799         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
   1800 
-> 1801     plot_obj.generate()
   1802     plot_obj.draw()
   1803     return plot_obj.result

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pandas\plotting\_core.py in generate(self)
    249         self._compute_plot_data()
    250         self._setup_subplots()
--> 251         self._make_plot()
    252         self._add_table()
    253         self._make_legend()

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pandas\plotting\_core.py in _make_plot(self)
    998 
    999             lines = _get_all_lines(ax)
-> 1000             left, right = _get_xlim(lines)
   1001             ax.set_xlim(left, right)
   1002 

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pandas\plotting\_tools.py in _get_xlim(lines)
    362     for l in lines:
    363         x = l.get_xdata(orig=False)
--> 364         left = min(np.nanmin(x), left)
    365         right = max(np.nanmax(x), right)
    366     return left, right

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\numpy\lib\nanfunctions.py in nanmin(a, axis, out, keepdims)
    278         # Fast, but not safe for subclasses of ndarray, or object arrays,
    279         # which do not implement isnan (gh-9009), or fmin correctly (gh-8975)
--> 280         res = np.fmin.reduce(a, axis=axis, out=out, **kwargs)
    281         if np.isnan(res).any():
    282             warnings.warn("All-NaN slice encountered", RuntimeWarning, stacklevel=2)

ValueError: zero-size array to reduction operation fmin which has no identity

Does anyone know how to deal with the problem or is familiar with that error message? 有谁知道如何处理该问题或熟悉该错误消息?

Okay, meanwhile I found a solution to get the code work: 好的,与此同时,我找到了使代码正常工作的解决方案:

turn the created Series object into a DataFrame object with one column(whose internal representation should still be a Series object) 将创建的Series对象转换为具有一列的DataFrame对象(其内部表示形式仍应为Series对象)

To summarize, the following code magically works: 总而言之,以下代码神奇地起作用:

# CELL 1
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import datetime
from pandas import Series
import sys 

input_array = np.array([[0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.]])

date_list = [datetime.date(2019, 1, 2), datetime.date(2019, 1, 9), datetime.date(2019, 1, 16), datetime.date(2019, 1, 23), datetime.date(2019, 1, 30), datetime.date(2019, 2, 6), datetime.date(2019, 2, 13), datetime.date(2019, 2, 20), datetime.date(2019, 2, 27), datetime.date(2019, 3, 6), datetime.date(2019, 3, 13), datetime.date(2019, 3, 20), datetime.date(2019, 3, 27), datetime.date(2019, 4, 3), datetime.date(2019, 4, 10)]

def get_indiv_series(table, index):
    out_series = []
    for i in table:
        out_series.append(i[index])
    return out_series

def make_indiv_category_plot(times, table, index, axis):
    print(get_indiv_series(table, index))
    series = pd.DataFrame(get_indiv_series(table, index), index=times)
    try:
        series.plot(style='-', use_index = True)    
    except ValueError as err:
        print(' A value Error ocurred')
        print(index)
        print(series)
        print(get_indiv_series(table, index))
        print(sys.exc_info())
        raise err
    line_i, = plt.plot([])
    return line_i

fig = plt.figure(figsize=(10, 10))
ax = plt.gca()

line_0 = make_indiv_category_plot(date_list, input_array, 0, ax)
line_1 = make_indiv_category_plot(date_list, input_array, 1, ax)
line_2 = make_indiv_category_plot(date_list, input_array, 2, ax)
line_3 = make_indiv_category_plot(date_list, input_array, 3, ax)
line_4 = make_indiv_category_plot(date_list, input_array, 4, ax)

Got anyone an explanation for that curious behavior ?? 得到任何人对这种奇怪行为的解释?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 零大小数组到没有标识的缩减操作 fmin - zero-size array to reduction operation fmin which has no identity SciPy optimize.fmin ValueError:零大小数组到减少操作最大值,没有标识 - SciPy optimize.fmin ValueError: zero-size array to reduction operation maximum which has no identity 如何修复 keras pad_sequences 中的“零大小数组到没有标识的最大缩减操作” - How to fix "zero-size array to reduction operation maximum which has no identity" in keras pad_sequences 零大小数组到没有标识的最大归约操作 - zero-size array to reduction operation maximum which has no identity ValueError:零大小数组到没有标识的最小化操作 - ValueError: zero-size array to reduction operation minimum which has no identity 具有Statsmodel ValueError的多个OLS回归:零大小数组到减少操作最大值,没有标识 - Multiple OLS Regression with Statsmodel ValueError: zero-size array to reduction operation maximum which has no identity Seaborn ValueError:零大小数组到减少操作最小值,没有标识 - Seaborn ValueError: zero-size array to reduction operation minimum which has no identity 如何将 np.max 用于没有 ValueError 的空 numpy 数组:零大小数组到没有标识的缩减操作最大值 - how to use np.max for empty numpy array without ValueError: zero-size array to reduction operation maximum which has no identity 为什么我会得到以及如何解决 ValueError: zero-size array to reduction operation maximum which has no identity - Why am I getting and how to solve ValueError: zero-size array to reduction operation maximum which has no identity 使用metpy cape_cin function时获取“ValueError:零大小数组以减少操作最小值,没有身份” - Getting a "ValueError: zero-size array to reduction operation minimum which has no identity" when using metpy cape_cin function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM