Skip to content

Fix windspeed_powerlaw raising TypeError for scalar negative heights - #2952

Closed
Dukotah wants to merge 2 commits into
pvlib:mainfrom
Dukotah:fix-windspeed-powerlaw-scalar-negative-height
Closed

Dukotah wants to merge 2 commits into
pvlib:mainfrom
Dukotah:fix-windspeed-powerlaw-scalar-negative-height

Conversation

@Dukotah

@Dukotah Dukotah commented Nov 20, 2026 •

Copy link
Copy Markdown
  • I am familiar with the contributing guidelines
  • I attest that all AI-generated material has been vetted for accuracy and is in compliance with the pvlib license
  • Tests added
  • Adds description and name entries in the appropriate "what's new" file in docs/sphinx/source/whatsnew for all changes. Includes link to the GitHub Issue with :issue:`num` or this Pull Request with :pull:`num`. Includes contributor name and/or GitHub username (link with :ghuser:`user`).
  • Pull request is nearly complete and ready for detailed review.

The windspeed_powerlaw docstring says it returns NaN when the reference height or wind speed is negative. That works for arrays and Series, but with plain scalars it raises instead:

>>> from pvlib.atmosphere import windspeed_powerlaw
>>> windspeed_powerlaw(5.0, -10.0, 2.0, exponent=0.2)
TypeError: '<' not supported between instances of 'complex' and 'int'

For Python floats, (-0.2) ** 0.2 gives a complex number rather than NaN, and the wind_speed < 0 check right after can't compare a complex number.

The fix computes the ratio with np.power, which gives NaN for a negative base no matter the input type. I put it inside np.errstate(invalid='ignore') because NaN is the documented result there, so the warning isn't useful.

Tested with a new parametrized scalar test (negative height, negative wind speed, normal case). The negative-height case fails on main with the TypeError above and passes with the change. All of tests/test_atmosphere.py passes.

Dukotah

With scalar inputs, Python's ** operator returns a complex number for a
negative base, and the following comparison raises TypeError instead of
returning NaN as documented. Use np.power, which returns NaN.
@github-actions

Copy link
Copy Markdown

Hey @Dukotah! 🎉

Thanks for opening your first pull request! We appreciate your
contribution. Please ensure you have reviewed and understood the
contributing guidelines.

If AI is used for any portion of this PR, you must vet the content
for technical accuracy.

Finally, be sure the PR description includes the PR
checklist,
and complete the items you are able to.

@wholmgren

Copy link
Copy Markdown
Member

closing because there's no linked issue with an agreed upon solution... nor is it a real world problem.

@wholmgren wholmgren closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants