pandas ValueError:制作风玫瑰时不能将float NaN转换为整型

xzv2uavs  于 2023-04-28  发布在  其他
关注(0)|答案(1)|浏览(131)

我写了一个代码,为加州州的一个县创建每月的污染风玫瑰。污染玫瑰图类似于风玫瑰图,因为它们显示了风向的分布,但不是显示风速的大小,而是绘制PM2的浓度。5.我已经将此代码用于加州空气资源委员会的许多数据集,但现在我正在使用来自本地监测网络的数据,并在运行代码时获得以下错误消息:

Traceback (most recent call last):

  File "C:\***.py", line 341, in __call__
    return printer(obj)

  File "C:\***.py", line 253, in <lambda>
    png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))

  File "C:\***.py", line 137, in print_figure
    fig.canvas.print_figure(bytes_io, **kw)

  File "C:\***.py", line 2230, in print_figure
    self.figure.draw(renderer)

  File "C:\***.py", line 74, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)

  File "C:\***.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "C:\***.py", line 2780, in draw
    mimage._draw_list_compositing_images(

  File "C:\***.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)

  File "C:\***.py", line 431, in wrapper
    return func(*inner_args, **inner_kwargs)

  File "C:\***.py", line 431, in wrapper
    return func(*inner_args, **inner_kwargs)

  File "C:\***.py", line 960, in draw
    center = self.transWedge.transform((0.5, 0.5))

  File "C:\***.py", line 1765, in transform
    return self.transform_affine(values)

  File "C:\***.py", line 1830, in transform_affine
    mtx = self.get_matrix()

  File "C:\***.py", line 2619, in get_matrix
    inl, inb, inw, inh = self._boxin.bounds

  File "C:\***.py", line 395, in bounds
    (x0, y0), (x1, y1) = self.get_points()

  File "C:\***.py", line 759, in get_points
    wedge = mpatches.Wedge(self._center, points[1, 1],

  File "C:\***.py", line 1167, in __init__
    self._recompute_path()

  File "C:\***.py", line 1179, in _recompute_path
    arc = Path.arc(theta1, theta2)

  File "C:\***.py", line 950, in arc
    n = int(2 ** np.ceil((eta2 - eta1) / halfpi))

ValueError: cannot convert float NaN to integer

Here is a link to my csv file
下面是我的代码:

import pandas as pd
from windrose import WindroseAxes
import matplotlib.pyplot as plt
import matplotlib.cm as cm

wr = pd.read_csv('IVANCALEX_forSO.csv')
wr = wr.set_index('date')
wr.index = pd.to_datetime(wr.index)

wr["Month"] = wr.index.month
wr['Hour'] = wr.index.hour

month_dict = {1: "January", 2: "February", 3: "March", 4: "April",
               5: "May", 6: "June", 7: "July", 8: "August", 9: "September",
               10: "October", 11: "November", 12: "December"}

xval = ["dir_3135"]
yval = ['Calexico, 604 Kubler Rd', 'Calexico, Alvarez', 'Calexico, Encinas Ave and Ethel St', 'Calexico, Ethel',
       'Calexico, Housing Authority', 'Calexico, Housing Authority West', 'Calexico, Residence', 
       'Holtville, 1015 Miller Rd', 'Holtville, South', '1201 West Hwy 98']

months = [v for k,v in month_dict.items()]
nrows, ncols = 2,6

#bins=np.logspace(0, 4, num=5) #pm10
#bins=np.arange(0, 1, .2) #pm2.5/pm10

for x,y in zip(xval,yval):
    fig = plt.figure(figsize=(15, 10))
    plt.subplots_adjust(hspace=0.5)
    site_name = y.split(",")[0].replace(" ", "_")
    fname = f"pollutionrose_{site_name}.png"
    bins=[-60,-10,0,10,40] #ozone deviations
    fig.tight_layout()
    for i, month in enumerate(months):
        d =  wr[wr["Month"].eq(month)].reset_index(drop=True)
        ax = fig.add_subplot(nrows, ncols, i + 1, projection="windrose")
        ax.set_title(month.capitalize(),fontsize=20, weight='bold')
        ax.bar(d[x], d[y],
           normed=True, opening=0.8,
           bins=bins, cmap=cm.rainbow,
           nsector=8)
        ax.set_xticklabels(['E', 'N-E', 'N', 'N-W', 'W', 'S-W', 'S', 'S-E'],fontsize=18)
        ax.tick_params(axis="y", labelsize=12.5)
        #ax.set_legend(decimal_places=1,fontsize='x-large', loc='best')
        #ax.set_yticklabels(np.arange(11, 77, step=11), fontsize=18)
    ax.figure.savefig(fname, dpi=400) #(8, 56, step=8)

我不确定为什么我会收到这个错误消息,因为我过去处理过具有许多NaN值的数据,并且没有问题。NaN值是否可能太多而无法执行此分析?
我试着做了这样的修改:

for i, month in enumerate(months):
    d =  wr[wr["Month"].eq(month)].reset_index(drop=True)
    ax = fig.add_subplot(nrows, ncols, i + 1, projection="windrose")
    ax.set_title(month.capitalize(),fontsize=20, weight='bold')

    # Drop rows with NaN values in d[x] or d[y]
    if d[x].isna().any() or d[y].isna().any():
        d = d.dropna(subset=[x, y])
    
    ax.bar(d[x], d[y],
           normed=True, opening=0.8,
           bins=bins, cmap=cm.rainbow,
           nsector=8)
    ax.set_xticklabels(['E', 'N-E', 'N', 'N-W', 'W', 'S-W', 'S', 'S-E'],fontsize=18)
    ax.tick_params(axis="y", labelsize=12.5)

但似乎并没有解决问题。最后,我希望它看起来像所附的图片。

更新:我尝试将NaN值替换为-250,因为我不需要这些值,并且仍然得到相同的错误消息。错误来自代码的这一部分:

ax.bar(d[x], d[y],
           normed=True, opening=0.8,
           bins=bins, cmap=cm.rainbow,
           nsector=8)

当我查看d变量时,没有写入任何数据,因此代码试图创建一个没有数据的windrose。我不知道为什么会发生这种情况。
救命啊!

zphenhs4

zphenhs41#

循环访问月份名称,然后将Month列值(整数)与该名称进行比较。显然,这行不通。
你可以简化你的代码:删除months = [v for k,v in month_dict.items()]行,并在嵌套的for循环中调整以下行:

...
    for i, month in month_dict.items():
...
        ax = fig.add_subplot(nrows, ncols, i, projection="windrose")

相关问题