Skip to content

BUG: fix dirint KeyError with scalar inputs on pandas >= 2.0#2752

Open
Omesh37 wants to merge 6 commits intopvlib:mainfrom
Omesh37:fix/dirint-scalar-keyerror-pandas2
Open

BUG: fix dirint KeyError with scalar inputs on pandas >= 2.0#2752
Omesh37 wants to merge 6 commits intopvlib:mainfrom
Omesh37:fix/dirint-scalar-keyerror-pandas2

Conversation

@Omesh37
Copy link
Copy Markdown
Contributor

@Omesh37 Omesh37 commented May 8, 2026

Closes #2751

Description

pvlib.irradiance.dirint crashed with KeyError: 'cannot use a single bool to index into setitem' when called with scalar inputs on pandas >= 2.0.

In _dirint_bins, scalar inputs like solar_zenith=90 cause comparisons such as (zenith >= 0) & (zenith < 25) to return a plain Python bool. pandas >= 2.0 treats scalar bools as label lookups on setitem, raising KeyError.

Fix: Convert all four scalar-or-array inputs to pd.Series at the top of _dirint_bins, ensuring boolean masks are always a boolean Series.

Checklist

  • Closes BUG: irradiance.dirint raises KeyError with scalar inputs on pandas >= 2.0 #2751
  • 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
  • New code is fully documented
  • Pull request is nearly complete and ready for detailed review

@Omesh37
Copy link
Copy Markdown
Contributor Author

Omesh37 commented May 9, 2026

Updated the PR based on your feedback:

  1. Added scalar_input = np.isscalar(solar_zenith) at the top of
    dirint before any computation
  2. Returns float(dni.iloc[0]) if scalar input, pd.Series otherwise
  3. Updated docstring: array-likenumeric for ghi and
    solar_zenith, return type updated to numeric or pd.Series
  4. Fix in _dirint_bins: scalar inputs cast to pd.Series using
    times index so boolean masks always produce a boolean Series
  5. Test added covering:
    • scalar int → float output
    • scalar float with use_delta_kt_prime=False → positive float
    • Series input → Series output (regression)
  6. Whatsnew entry added in v0.15.2.rst

All 9 dirint tests pass locally. Please let me know if anything needs adjusting.

Copy link
Copy Markdown
Member

@RDaxini RDaxini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the test failure is related to the changes in this PR

Comment thread pvlib/irradiance.py Outdated
Comment thread docs/sphinx/source/whatsnew/v0.15.2.rst Outdated
Omesh37 and others added 2 commits May 10, 2026 21:07
Co-authored-by: Rajiv Daxini <143435106+RDaxini@users.noreply.github.com>
Co-authored-by: Rajiv Daxini <143435106+RDaxini@users.noreply.github.com>
@RDaxini RDaxini added this to the v0.15.2 milestone May 10, 2026
@RDaxini RDaxini added the bug label May 10, 2026
@Omesh37
Copy link
Copy Markdown
Contributor Author

Omesh37 commented May 10, 2026

I don't think the test failure is related to the changes in this PR

Ya it is related to failing test of test_singlediode_precision function in test_singlediode.py.

Thank You :)

Copy link
Copy Markdown
Member

@echedey-ls echedey-ls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice PR @Omesh37 . Two minor comments down below.

Comment thread pvlib/irradiance.py
dni : array-like
The modeled direct normal irradiance, as provided by the
DIRINT model. [Wm⁻²]
dni : numeric or pd.Series
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dni : numeric or pd.Series
dni : numeric

pd.Series is considered numeric
https://pvlib-python.readthedocs.io/en/stable/contributing/style_guide.html#documentation-style

Comment on lines +17 to +18
* Fixed :py:func:`pvlib.irradiance.dirint` raising ``KeyError`` with
scalar inputs on pandas >= 2.0. (:pull:`2752`, :issue:`2751`)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Fixed :py:func:`pvlib.irradiance.dirint` raising ``KeyError`` with
scalar inputs on pandas >= 2.0. (:pull:`2752`, :issue:`2751`)
* Fix :py:func:`pvlib.irradiance.dirint` raises ``KeyError`` with
scalar inputs on ``pandas>=2.0``. (:issue:`2751`, :pull:`2752`)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: irradiance.dirint raises KeyError with scalar inputs on pandas >= 2.0

3 participants