ufunc“add”不包含具有签名匹配类型(dtype(“<U32”)、dtype(“<U32”)>dtype(“<U32”)的循环

2024-10-06 15:24:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试运行此脚本,但它显示生成的错误:

UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')

以下是我正在尝试运行的代码:

if __name__ == '__main__':
    app = Nominatim(user_agent="test_solar")

    loc_raw = app.geocode('Postintaival 7, 00230 Helsinki, Finland').raw

    latitude = loc_raw['lat']
    longitude = loc_raw['lon']
    altitude = get_elevation(latitude, longitude)

    location_object = Location(latitude, longitude, 'Europe/Helsinki', altitude,
                               'relex_solutions')

    weather = pvlib.iotools.get_pvgis_tmy(latitude, longitude, map_variables=True)[0]

    times = weather.index

    solpos = location_object.get_solarposition(times)

    clearsky_values = location_object.get_clearsky(times, model='ineichen',
                                                   solar_position=solpos,
                                                   dni_extra=None)

                                

Tags: appgetrawobjectlocationlocweatherdtype
1条回答
网友
1楼 · 发布于 2024-10-06 15:24:42

TLDR

将经度(以及一致性所需的纬度)转换为float,它将起作用:

    latitude = float(loc_raw['lat'])
    longitude = float(loc_raw['lon'])

更长的解释

添加所有导入和缺失函数需要一些时间,以获得最小的可复制示例。我猜get_elevation函数来自this question

import requests
from geopy.geocoders import Nominatim
from pvlib.location import Location
import pvlib
import pandas as pd


def get_elevation(lat, long):
    query = ('https://api.open-elevation.com/api/v1/lookup'
             f'?locations={lat},{long}')
    r = requests.get(query).json()  # json object, various ways you can extract value
    # one approach is to use pandas json functionality:
    elevation = pd.io.json.json_normalize(r, 'results')['elevation'].values[0]
    return elevation


if __name__ == '__main__':
    app = Nominatim(user_agent="test_solar")

    loc_raw = app.geocode('Postintaival 7, 00230 Helsinki, Finland').raw

    latitude = loc_raw['lat']
    longitude = loc_raw['lon']
    altitude = get_elevation(latitude, longitude)

    location_object = Location(latitude, longitude, 'Europe/Helsinki',
                               altitude, 'relex_solutions')
    weather = pvlib.iotools.get_pvgis_tmy(latitude, longitude,
                                          map_variables=True)[0]

    times = weather.index

    solpos = location_object.get_solarposition(times)
    clearsky_values = location_object.get_clearsky(times, model='ineichen',
                                                   solar_position=solpos,)

这里是pvlib.spa模块中出现问题的地方:

@jcompile('float64(float64, float64, float64)', nopython=True)
def local_hour_angle(apparent_sidereal_time, observer_longitude,
                     sun_right_ascension):
    """Measured westward from south"""
    H = apparent_sidereal_time + observer_longitude - sun_right_ascension
    return H % 360

我添加了一些打印以查看变量(调试点也应该可以工作):

@jcompile('float64(float64, float64, float64)', nopython=True)
def local_hour_angle(apparent_sidereal_time, observer_longitude,
                     sun_right_ascension):
    """Measured westward from south"""
    print(f'{apparent_sidereal_time!r}')
    print(f'{observer_longitude!r}')
    print(f'{sun_right_ascension!r}')
    H = apparent_sidereal_time + observer_longitude - sun_right_ascension
    return H % 360

我们得到:

array([100.06452967, 115.10559772, 130.14666594, ...,  55.20775781,
        70.24882694,  85.28989625])
'24.9181469'
array([280.83336392, 280.87939039, 280.92541462, ..., 280.99011537,
       281.03612276, 281.0821279 ])

observer_longitude参数是在numpy sum操作中不受管理的字符串。经过一些探索,我发现这个经度直接来自Location物体,因此将它简单地转换成float就解决了这个问题

相关问题 更多 >