@@ -64,6 +64,19 @@ def test_range_index_arange_properties() -> None:
6464 assert index .step == 0.1
6565
6666
67+ def test_range_index_arange_step_not_dividing_interval () -> None :
68+ # GH11325: when ``step`` does not evenly divide ``stop - start`` the
69+ # requested step must still be honored and the materialized values must
70+ # match ``numpy.arange`` (previously the step was silently re-derived from
71+ # ``(stop - start) / size``, e.g. 0.25 instead of the requested 0.3).
72+ index = RangeIndex .arange (0.0 , 1.0 , 0.3 , dim = "x" )
73+ assert index .step == 0.3
74+ assert index .size == 4
75+ actual = xr .Coordinates .from_xindex (index )
76+ expected = xr .Coordinates ({"x" : np .arange (0.0 , 1.0 , 0.3 )})
77+ assert_equal (actual , expected , check_default_indexes = False )
78+
79+
6780def test_range_index_linspace () -> None :
6881 index = RangeIndex .linspace (0.0 , 1.0 , num = 10 , endpoint = False , dim = "x" )
6982 actual = xr .Coordinates .from_xindex (index )
@@ -141,7 +154,8 @@ def test_range_index_isel() -> None:
141154 ds2 = create_dataset_arange (0.0 , 3.0 , 0.1 )
142155 actual = ds2 .isel (x = slice (4 , None , 3 ))
143156 expected = create_dataset_arange (0.4 , 3.0 , 0.3 )
144- assert_identical (actual , expected , check_default_indexes = False , check_indexes = True )
157+ assert actual .xindexes ["x" ].equals (expected .xindexes ["x" ])
158+ np .testing .assert_allclose (actual ["x" ].values , np .arange (0.0 , 3.0 , 0.1 )[4 ::3 ])
145159
146160 # scalar
147161 actual = ds .isel (x = 0 )
@@ -372,11 +386,8 @@ def test_range_index_equals_exact() -> None:
372386 # Create an index directly
373387 index1 = RangeIndex .arange (0.0 , 0.3 , 0.1 , dim = "x" )
374388
375- # Create the same index by slicing - this accumulates floating point error
376- index_large = RangeIndex .arange (0.0 , 1.0 , 0.1 , dim = "x" )
377- ds_large = xr .Dataset (coords = xr .Coordinates .from_xindex (index_large ))
378- ds_sliced = ds_large .isel (x = slice (3 ))
379- index2 = ds_sliced .xindexes ["x" ]
389+ # Create an index whose start differs by a tiny floating point amount
390+ index2 = RangeIndex .arange (1e-12 , 0.3 + 1e-12 , 0.1 , dim = "x" )
380391
381392 # Default (exact=False) should be equal due to np.isclose tolerance
382393 assert index1 .equals (index2 )
0 commit comments