Skip to content

Fix delta_kt_prime halving single valid neighbor difference (gh-1847)#2821

Open
adi-IL wants to merge 1 commit into
pvlib:mainfrom
adi-IL:fix/delta-kt-prime-interior-nan
Open

Fix delta_kt_prime halving single valid neighbor difference (gh-1847)#2821
adi-IL wants to merge 1 commit into
pvlib:mainfrom
adi-IL:fix/delta-kt-prime-interior-nan

Conversation

@adi-IL

@adi-IL adi-IL commented Jul 12, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes a logic error in irradiance._delta_kt_prime_dirint (used by dirint) when the input series contains a missing value in the interior.

Background

The DIRINT model computes the stability index delta_kt_prime from the zenith independent clearness index kt_prime using Perez eqn 2 for interior points and Perez eqn 3 for boundary points. Before this change the code patched only the very first and very last positions to supply a missing neighbor, and for every other point it did:

delta_kt_prime = 0.5 * ((kt - kt_next).abs() + (kt - kt_prev).abs())

When an interior point has only one valid neighbor (because a gap exists elsewhere in the series), the missing neighbor difference was treated as 0 and the remaining valid difference was then multiplied by 0.5. That halved a single valid difference, which is not what Perez eqn 3 specifies. The correct value is the absolute difference to the one available neighbor.

The fix

Replace the manual endpoint patching with a row wise mean of the next and previous absolute differences:

delta_kt_prime = pd.DataFrame({ next : (kt - kt_next).abs(), prev : (kt - kt_prev).abs() }).mean(axis=1)

pandas skips NaN values when taking the mean, so the computation automatically handles zero, one, or two valid neighbors. This covers interior points (eqn 2), the endpoints (eqn 3), and interior gaps where exactly one neighbor is present, all with the same expression.

Example

For kt_prime = [0.5, 0.6, NaN, 0.7, 0.4] the neighbors of the gap now read:

  • old: [0.10, 0.05, NaN, 0.15, 0.30] (the 0.05 and 0.15 are halved and wrong)
  • new: [0.10, 0.10, NaN, 0.30, 0.30] (correct single neighbor differences)

The NaN at the gap position itself is unchanged, since kt_prime is undefined there.

Testing

Added a regression test test_delta_kt_prime_interior_nan. The full tests/test_irradiance.py suite passes (124 passed, 9 remote data tests skipped, which need network access).

Closes #1847

…1847)

In irradiance._delta_kt_prime_dirint, when an interior point has only one valid neighbor (a missing value elsewhere in the series), the previous code substituted 0 for the missing neighbor difference and then multiplied by 0.5, halving the remaining valid difference. This produced an incorrect stability index delta_kt_prime and, downstream, incorrect DIRINT beam irradiance.

Replace the manual endpoint patching with a row-wise mean of the next and previous absolute differences. pandas skips NaN values, so Perez eqn 2 (two neighbors), eqn 3 for endpoints, and interior gaps with one missing neighbor are all handled correctly.

Add a regression test and a whatsnew entry.
@github-actions

Copy link
Copy Markdown

Hey @adi-IL! 🎉

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.

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.

Calculation of delta kt'

1 participant