如何修复此错误:切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是有效索引

问题描述 投票:0回答:1

这是我用来研究船舶轨迹的代码中的函数之一。

我昨天确实运行了这段代码,但这种情况从未发生过。一夜之间(似乎没有改变任何东西)这个错误开始出现。这是代码:

def plot_trajectory(mmsi, df_navio, data_hora_inicial, data_hora_final):

    # Create a GeoDataFrame with the columns of latitude and longitude of the ship in a determined space of time.
    gdf_trajetoria = gpd.GeoDataFrame(df_navio, geometry=gpd.points_from_xy(df_navio['Longitude'], df_navio['Latitude']))

    # Load the shapefile base map
    mapa_base = gpd.read_file('D:\\Afonso\\Estágio\\Mapa.sh\\ne_50m_admin_0_countries.shp')

    # Create a rectangle based on the information provided by the user
    retangulo = Polygon([(longitude_1, latitude_1), 
                         (longitude_2, latitude_1), 
                         (longitude_2, latitude_2), 
                         (longitude_1, latitude_2)])
    # Plot the base map
    ax = mapa_base.plot(figsize=(10, 10), color='white', edgecolor='black')

    if escolha_aom.upper() == 'NÃO':
        # Add the rectangle to the GeoDataFrame with the base map
        gpd.GeoDataFrame(geometry=[retangulo]).plot(ax=ax, edgecolor='black', facecolor='None')

    # Define colors for the 3 different datasets
    cores = {'F_SAT': 'red', 'F': 'blue', 'F_PT': 'green'}
    marcadores = {'F_SAT': 'o', 'F': 'x', 'F_PT': '^'}  # Define markers for each one

    # Plot the trajectory of the ship in the determined time chosen by the user
    for fonte_input in fontes_input:
        gdf_fonte = gdf_trajetoria[gdf_trajetoria['Fonte'] == fonte_input.upper()]
        gdf_fonte.plot(ax=ax, color=cores[fonte_input], marker=marcadores[fonte_input], markersize=8, label=f'Trajetória do Navio ({fonte_input.upper()})')
    
    # Add title and other relevant information for the plot
    plt.title(f'Trajetória do Navio com MMSI: {mmsi} no Intervalo de Tempo de {data_hora_inicial} a {data_hora_final}')
    plt.xlabel('Longitude')
    plt.ylabel('Latitude')
    plt.legend()

    # Add a line connecting the dots
    coords = [(x, y) for x, y in zip(gdf_trajetoria.geometry.x, gdf_trajetoria.geometry.y)]
    for i in range(len(coords) - 1):
        plt.plot([coords[i][0], coords[i+1][0]], [coords[i][1], coords[i+1][1]], color=cores[fontes_input[0]])

    # Plot
    plt.show()

该错误在这一行中弹出:gdf_trajetoria = gpd.GeoDataFrame(df_navio, Geometry=gpd.points_from_xy(df_navio['Longitude'], df_navio['Latitude']))

错误: 发生异常:IndexError 只有整数、切片 (

:
)、省略号 (
...
)、numpy.newaxis (
None
) 和整数或布尔数组是有效索引

这似乎是在一夜之间发生的,我不记得对代码的这个特定部分进行过任何更改。难道是我正在使用的某个软件包已更新?如果是这样,这里是我在整个代码中使用的所有包:

  • 将 numpy 导入为 np
  • 将 pandas 导入为 pd
  • 导入 scipy.io
  • 将 geopandas 导入为 gpd
  • 将 matplotlib.pyplot 导入为 plt
  • 从日期时间导入日期时间,时间增量
  • 将 tkinter 导入为 tk
  • 从 shapely.geometry 导入多边形
  • 从 tkinter 导入 simpledialog
  • 导入操作系统
  • 导入时间

这个函数理论上应该根据用户提供的信息创建绘图。它曾经可以工作,但现在不行,而且我不记得对这部分代码进行过任何更改。

python function exception plot index-error
1个回答
0
投票

我认为错误发生在这一行

gdf_fonte = gdf_trajetoria[gdf_trajetoria['Fonte'] == fonte_input.upper()]

您正在做

gdf_fonte = gdf_trajetoria[True]
gdf_fonte = gdf_trajetoria[False]
,这似乎是错误的。

© www.soinside.com 2019 - 2024. All rights reserved.