| 
12 | 12 | from pvlib.location import Location  | 
13 | 13 | from pvlib import solarposition, spa  | 
14 | 14 | 
 
  | 
15 |  | -from .conftest import requires_ephem, requires_spa_c, requires_numba  | 
16 |  | - | 
 | 15 | +from .conftest import (  | 
 | 16 | +    requires_ephem, requires_spa_c, requires_numba, requires_pandas_2_0  | 
 | 17 | +)  | 
17 | 18 | 
 
  | 
18 | 19 | # setup times and locations to be tested.  | 
19 | 20 | times = pd.date_range(start=datetime.datetime(2014, 6, 24),  | 
@@ -717,6 +718,119 @@ def test_sun_rise_set_transit_geometric(expected_rise_set_spa, golden_mst):  | 
717 | 718 |                        atol=np.abs(expected_transit_error).max())  | 
718 | 719 | 
 
  | 
719 | 720 | 
 
  | 
 | 721 | +@pytest.mark.parametrize('tz', [None, 'utc', 'US/Eastern'])  | 
 | 722 | +def test__datetime_to_unixtime(tz):  | 
 | 723 | +    # for pandas < 2.0 where "unit" doesn't exist in pd.date_range. note that  | 
 | 724 | +    # unit of ns is the only option in pandas<2, and the default in pandas 2.x  | 
 | 725 | +    times = pd.date_range(start='2019-01-01', freq='h', periods=3, tz=tz)  | 
 | 726 | +    expected = times.view(np.int64)/10**9  | 
 | 727 | +    actual = solarposition._datetime_to_unixtime(times)  | 
 | 728 | +    np.testing.assert_equal(expected, actual)  | 
 | 729 | + | 
 | 730 | + | 
 | 731 | +@requires_pandas_2_0  | 
 | 732 | +@pytest.mark.parametrize('unit', ['ns', 'us', 's'])  | 
 | 733 | +@pytest.mark.parametrize('tz', [None, 'utc', 'US/Eastern'])  | 
 | 734 | +def test__datetime_to_unixtime_units(unit, tz):  | 
 | 735 | +    kwargs = dict(start='2019-01-01', freq='h', periods=3)  | 
 | 736 | +    times = pd.date_range(**kwargs, unit='ns', tz='UTC')  | 
 | 737 | +    expected = times.view(np.int64)/10**9  | 
 | 738 | + | 
 | 739 | +    times = pd.date_range(**kwargs, unit=unit, tz='UTC').tz_convert(tz)  | 
 | 740 | +    actual = solarposition._datetime_to_unixtime(times)  | 
 | 741 | +    np.testing.assert_equal(expected, actual)  | 
 | 742 | + | 
 | 743 | + | 
 | 744 | +@requires_pandas_2_0  | 
 | 745 | +@pytest.mark.parametrize('method', [  | 
 | 746 | +    'nrel_numpy',  | 
 | 747 | +    'ephemeris',  | 
 | 748 | +    pytest.param('pyephem', marks=requires_ephem),  | 
 | 749 | +    pytest.param('nrel_numba', marks=requires_numba),  | 
 | 750 | +    pytest.param('nrel_c', marks=requires_spa_c),  | 
 | 751 | +])  | 
 | 752 | +@pytest.mark.parametrize('tz', [None, 'utc', 'US/Eastern'])  | 
 | 753 | +def test_get_solarposition_microsecond_index(method, tz):  | 
 | 754 | +    # https://github.com/pvlib/pvlib-python/issues/1932  | 
 | 755 | + | 
 | 756 | +    kwargs = dict(start='2019-01-01', freq='H', periods=24, tz=tz)  | 
 | 757 | + | 
 | 758 | +    index_ns = pd.date_range(unit='ns', **kwargs)  | 
 | 759 | +    index_us = pd.date_range(unit='us', **kwargs)  | 
 | 760 | + | 
 | 761 | +    sp_ns = solarposition.get_solarposition(index_ns, 40, -80, method=method)  | 
 | 762 | +    sp_us = solarposition.get_solarposition(index_us, 40, -80, method=method)  | 
 | 763 | + | 
 | 764 | +    assert_frame_equal(sp_ns, sp_us, check_index_type=False)  | 
 | 765 | + | 
 | 766 | + | 
 | 767 | +@requires_pandas_2_0  | 
 | 768 | +@pytest.mark.parametrize('tz', [None, 'utc', 'US/Eastern'])  | 
 | 769 | +def test_nrel_earthsun_distance_microsecond_index(tz):  | 
 | 770 | +    # https://github.com/pvlib/pvlib-python/issues/1932  | 
 | 771 | + | 
 | 772 | +    kwargs = dict(start='2019-01-01', freq='H', periods=24, tz=tz)  | 
 | 773 | + | 
 | 774 | +    index_ns = pd.date_range(unit='ns', **kwargs)  | 
 | 775 | +    index_us = pd.date_range(unit='us', **kwargs)  | 
 | 776 | + | 
 | 777 | +    esd_ns = solarposition.nrel_earthsun_distance(index_ns)  | 
 | 778 | +    esd_us = solarposition.nrel_earthsun_distance(index_us)  | 
 | 779 | + | 
 | 780 | +    assert_series_equal(esd_ns, esd_us, check_index_type=False)  | 
 | 781 | + | 
 | 782 | + | 
 | 783 | +@requires_pandas_2_0  | 
 | 784 | +@pytest.mark.parametrize('tz', [None, 'utc', 'US/Eastern'])  | 
 | 785 | +def test_hour_angle_microsecond_index(tz):  | 
 | 786 | +    # https://github.com/pvlib/pvlib-python/issues/1932  | 
 | 787 | + | 
 | 788 | +    kwargs = dict(start='2019-01-01', freq='H', periods=24, tz=tz)  | 
 | 789 | + | 
 | 790 | +    index_ns = pd.date_range(unit='ns', **kwargs)  | 
 | 791 | +    index_us = pd.date_range(unit='us', **kwargs)  | 
 | 792 | + | 
 | 793 | +    ha_ns = solarposition.hour_angle(index_ns, -80, 0)  | 
 | 794 | +    ha_us = solarposition.hour_angle(index_us, -80, 0)  | 
 | 795 | + | 
 | 796 | +    np.testing.assert_equal(ha_ns, ha_us)  | 
 | 797 | + | 
 | 798 | + | 
 | 799 | +@requires_pandas_2_0  | 
 | 800 | +@pytest.mark.parametrize('tz', ['utc', 'US/Eastern'])  | 
 | 801 | +def test_rise_set_transit_spa_microsecond_index(tz):  | 
 | 802 | +    # https://github.com/pvlib/pvlib-python/issues/1932  | 
 | 803 | + | 
 | 804 | +    kwargs = dict(start='2019-01-01', freq='H', periods=24, tz=tz)  | 
 | 805 | + | 
 | 806 | +    index_ns = pd.date_range(unit='ns', **kwargs)  | 
 | 807 | +    index_us = pd.date_range(unit='us', **kwargs)  | 
 | 808 | + | 
 | 809 | +    rst_ns = solarposition.sun_rise_set_transit_spa(index_ns, 40, -80)  | 
 | 810 | +    rst_us = solarposition.sun_rise_set_transit_spa(index_us, 40, -80)  | 
 | 811 | + | 
 | 812 | +    assert_frame_equal(rst_ns, rst_us, check_index_type=False)  | 
 | 813 | + | 
 | 814 | + | 
 | 815 | +@requires_pandas_2_0  | 
 | 816 | +@pytest.mark.parametrize('tz', [None, 'utc', 'US/Eastern'])  | 
 | 817 | +def test_rise_set_transit_geometric_microsecond_index(tz):  | 
 | 818 | +    # https://github.com/pvlib/pvlib-python/issues/1932  | 
 | 819 | + | 
 | 820 | +    kwargs = dict(start='2019-01-01', freq='H', periods=24, tz=tz)  | 
 | 821 | + | 
 | 822 | +    index_ns = pd.date_range(unit='ns', **kwargs)  | 
 | 823 | +    index_us = pd.date_range(unit='us', **kwargs)  | 
 | 824 | + | 
 | 825 | +    args = (40, -80, 0, 0)  | 
 | 826 | +    rst_ns = solarposition.sun_rise_set_transit_geometric(index_ns, *args)  | 
 | 827 | +    rst_us = solarposition.sun_rise_set_transit_geometric(index_us, *args)  | 
 | 828 | + | 
 | 829 | +    for times_ns, times_us in zip(rst_ns, rst_us):  | 
 | 830 | +        # can't use a fancy assert function here since the units are different  | 
 | 831 | +        assert all(times_ns == times_us)  | 
 | 832 | + | 
 | 833 | + | 
720 | 834 | # put numba tests at end of file to minimize reloading  | 
721 | 835 | 
 
  | 
722 | 836 | @requires_numba  | 
 | 
0 commit comments