Skip to content

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

Description

@Omesh37

Bug description

pvlib.irradiance.dirint raises KeyError: 'cannot use a single bool to index into setitem' when called with scalar solar_zenith (or other
scalar inputs) on pandas >= 2.0.

To reproduce

import pvlib
import pandas as pd

pvlib.irradiance.dirint(
    ghi=0,
    solar_zenith=90,
    times=pd.DatetimeIndex(['2023-06-21'], tz='UTC')
)

Traceback

KeyError: 'cannot use a single bool to index into setitem'
File "pvlib/irradiance.py", in _dirint_bins
zenith_bin[(zenith >= 0) & (zenith < 25)] = 1

Root cause

In _dirint_bins, when scalar values are passed, boolean expressions like
(zenith >= 0) & (zenith < 25) evaluate to a plain Python bool (e.g.
False). In pandas >= 2.0, series[False] = value raises KeyError
because pandas now treats scalar bools as label lookups, not boolean masks.

Expected behavior

Returns NaN or 0.0 cleanly without crashing.

Fix

Convert all scalar inputs to pd.Series at the top of _dirint_bins:

kt_prime = pd.Series(kt_prime, index=times, dtype=float)
zenith = pd.Series(zenith, index=times, dtype=float)
w = pd.Series(w, index=times, dtype=float)
delta_kt_prime = pd.Series(delta_kt_prime, index=times, dtype=float)

Environment

  • pvlib: 0.15.2.dev2+g200de89ca
  • pandas: 2.3.3
  • numpy: 2.3.5
  • Python: 3.13.9
  • OS: Windows

I would like to submit a PR for this fix.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions