【问题标题】:ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')ufunc 'add' 不包含签名匹配类型的循环 (dtype('<U32'), dtype('<U32')) -> dtype('<U32')
【发布时间】:2021-11-23 19:57:12
【问题描述】:

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

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)

                                

【问题讨论】:

  • 发布完整的回溯消息,这样我们就不必猜测了。

标签: python numpy-ufunc pvlib


【解决方案1】:

TLDR

将您的经度(以及一致性的纬度)转换为浮动,它将起作用:

    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即可解决问题。

【讨论】:

    猜你喜欢
    • 2018-07-25
    • 2021-07-28
    • 2023-04-03
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 2021-12-15
    • 2019-03-27
    • 1970-01-01
    相关资源
    最近更新 更多