Skip to content

Potential bug with solarposition.py #2502

@dtwidrick

Description

@dtwidrick

Describe the bug
I am upgrading from pvlib version 0.9.5 and have noticed an issue when using the sun_rise_set_transit_spa() function to label nighttime points. I have attached three screenshot, one demonstrating when the function is working correctly using pvlib0.9.5 (labeled 'correctly labeled nighttime points.png'), one showing the issue using pvlib0.13.0 (labeled 'incorrectly_labeled_nighttime_points.png'), along with a zoomed in screenshot of the issue ('incorrectly_labeled_nighttime_points_zoomed_in.png'). I am trying to use the function to label the nighttime points for later filtering.

I am able to edit two lines of the code from pvlib0.13.0 to get the correct behavior. I don't fully understand the issue, but if I comment out these two lines:
(lines 448-450 in solarposition.py)

    # must convert to midnight UTC on day of interest
    # times_utc = times.tz_convert('UTC')
    # unixtime = _datetime_to_unixtime(times_utc.normalize())

and instead use the unixtime conversion used in pvlib 0.9.5:

    # use pvlib095 conversion
    times_utc = pd.DatetimeIndex(times.date).tz_localize('UTC')
    unixtime = np.array(times_utc.view(np.int64)/10**9)"

I end up with the plots that look correct.

To Reproduce
Steps to reproduce the behavior:
I think you should be able to reproduce the problem by creating a nighttime filter on a dataset and visualizing the points labeled as nighttime points and see if it looks like the function properly flagged nighttime points.

Expected behavior
I expect the sun_rise_set_transit_spa() function to properly create a flag that label points that occur during the nighttime as nighttime points.

Function call I am using to generate the flag:

def night_flag(data, meta):
    '''
    Goal: flag all points that are nighttime points (before sunrise and after sunset). Returns a flag of True or False
    values for each timestamp in the index where True means it is a nighttime point and False means it is not.
    
    Parameters
        - data: pandas dataframe where the index is the timestamp
        - meta: pandas array (column) of site specific information
    Returns
        - boolean flag
    '''    
    times=pvlib.solarposition.sun_rise_set_transit_spa(data.index, meta['latitude'], meta['longitude'])
    
    return ((data.index < times['sunrise']) | (data.index > times['sunset']))

Screenshots
If applicable, add screenshots to help explain your problem.
See attached screenshots.
Zoomed in view of issue.
Image
Zoomed out view of issue.
Image
Zoomed out view of correct performance that is achieved by editing the above two lines to the old 0.9.5 version.
Image

Versions:

  • pvlib.__version__: 0.13.0
  • pandas.__version__: 2.3.1
  • python: 3.13.5

Additional context
It seems that just adjusting a couple lines to the old version fixes the issue for me. I am not sure what exactly about the new code for determining the unixtime variable causes the problem. I'll be copying the solarposition code to my local code and making the edit and just calling the modified function for the time being, but figured it would be good to create an issue. Thanks!

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