def test_where_uint64(self): idx = UInt64Index([0, 6, 2]) mask = np.array([False, True, False]) other = np.array([1], dtype=np.int64) expected = UInt64Index([1, 6, 1]) result = idx.where(mask, other) tm.assert_index_equal(result, expected) result = idx.putmask(~mask, other) tm.assert_index_equal(result, expected)
def test_get_indexer_uint64(self, index_large): target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2**63) indexer = index_large.get_indexer(target) expected = np.array([0, -1, 1, 2, 3, 4, -1, -1, -1, -1], dtype=np.intp) tm.assert_numpy_array_equal(indexer, expected) target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2**63) indexer = index_large.get_indexer(target, method="pad") expected = np.array([0, 0, 1, 2, 3, 4, 4, 4, 4, 4], dtype=np.intp) tm.assert_numpy_array_equal(indexer, expected) target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2**63) indexer = index_large.get_indexer(target, method="backfill") expected = np.array([0, 1, 1, 2, 3, 4, -1, -1, -1, -1], dtype=np.intp) tm.assert_numpy_array_equal(indexer, expected)
def test_astype_uint(self): arr = period_range("2000", periods=2, name="idx") expected = UInt64Index(np.array([10957, 10958], dtype="uint64"), name="idx") with tm.assert_produces_warning(FutureWarning): tm.assert_index_equal(arr.astype("uint64"), expected) tm.assert_index_equal(arr.astype("uint32"), expected)
class TestWhere: @pytest.mark.parametrize( "index", [ Float64Index(np.arange(5, dtype="float64")), Int64Index(range(0, 20, 2)), UInt64Index(np.arange(5, dtype="uint64")), ], ) def test_where(self, listlike_box, index): cond = [True] * len(index) expected = index result = index.where(listlike_box(cond)) cond = [False] + [True] * (len(index) - 1) expected = Float64Index([index._na_value] + index[1:].tolist()) result = index.where(listlike_box(cond)) tm.assert_index_equal(result, expected) def test_where_uint64(self): idx = UInt64Index([0, 6, 2]) mask = np.array([False, True, False]) other = np.array([1], dtype=np.int64) expected = UInt64Index([1, 6, 1]) result = idx.where(mask, other) tm.assert_index_equal(result, expected) result = idx.putmask(~mask, other) tm.assert_index_equal(result, expected)
def test_astype_uint(self): arr = period_range("2000", periods=2, name="idx") expected = UInt64Index(np.array([10957, 10958], dtype="uint64"), name="idx") tm.assert_index_equal(arr.astype("uint64"), expected) msg = "will return exactly the specified dtype instead of uint64" with tm.assert_produces_warning(FutureWarning, match=msg): res = arr.astype("uint32") tm.assert_index_equal(res, expected)
def test_join_right(self, index_large): other = UInt64Index(2**63 + np.array([7, 12, 25, 1, 2, 10], dtype="uint64")) other_mono = UInt64Index( 2**63 + np.array([1, 2, 7, 10, 12, 25], dtype="uint64")) # not monotonic res, lidx, ridx = index_large.join(other, how="right", return_indexers=True) eres = other elidx = np.array([-1, -1, 4, -1, -1, 1], dtype=np.intp) tm.assert_numpy_array_equal(lidx, elidx) assert isinstance(other, UInt64Index) tm.assert_index_equal(res, eres) assert ridx is None # monotonic res, lidx, ridx = index_large.join(other_mono, how="right", return_indexers=True) eres = other_mono elidx = np.array([-1, -1, -1, 1, -1, 4], dtype=np.intp) assert isinstance(other, UInt64Index) tm.assert_numpy_array_equal(lidx, elidx) tm.assert_index_equal(res, eres) assert ridx is None # non-unique idx = UInt64Index(2**63 + np.array([1, 1, 2, 5], dtype="uint64")) idx2 = UInt64Index(2**63 + np.array([1, 2, 5, 7, 9], dtype="uint64")) res, lidx, ridx = idx.join(idx2, how="right", return_indexers=True) # 1 is in idx2, so it should be x2 eres = UInt64Index(2**63 + np.array([1, 1, 2, 5, 7, 9], dtype="uint64")) elidx = np.array([0, 1, 2, 3, -1, -1], dtype=np.intp) eridx = np.array([0, 0, 1, 2, 3, 4], dtype=np.intp) tm.assert_index_equal(res, eres) tm.assert_numpy_array_equal(lidx, elidx) tm.assert_numpy_array_equal(ridx, eridx)
def test_astype_float64_to_uint64(self): # GH#45309 used to incorrectly return Int64Index idx = Float64Index([0.0, 5.0, 10.0, 15.0, 20.0]) result = idx.astype("u8") expected = UInt64Index([0, 5, 10, 15, 20]) tm.assert_index_equal(result, expected) idx_with_negatives = idx - 10 with pytest.raises(ValueError, match="losslessly"): idx_with_negatives.astype(np.uint64)
def test_join_inner(self, index_large): other = UInt64Index(2**63 + np.array([7, 12, 25, 1, 2, 10], dtype="uint64")) other_mono = UInt64Index( 2**63 + np.array([1, 2, 7, 10, 12, 25], dtype="uint64")) # not monotonic res, lidx, ridx = index_large.join(other, how="inner", return_indexers=True) # no guarantee of sortedness, so sort for comparison purposes ind = res.argsort() res = res.take(ind) lidx = lidx.take(ind) ridx = ridx.take(ind) eres = UInt64Index(2**63 + np.array([10, 25], dtype="uint64")) elidx = np.array([1, 4], dtype=np.intp) eridx = np.array([5, 2], dtype=np.intp) assert isinstance(res, UInt64Index) tm.assert_index_equal(res, eres) tm.assert_numpy_array_equal(lidx, elidx) tm.assert_numpy_array_equal(ridx, eridx) # monotonic res, lidx, ridx = index_large.join(other_mono, how="inner", return_indexers=True) res2 = index_large.intersection(other_mono) tm.assert_index_equal(res, res2) elidx = np.array([1, 4], dtype=np.intp) eridx = np.array([3, 5], dtype=np.intp) assert isinstance(res, UInt64Index) tm.assert_index_equal(res, eres) tm.assert_numpy_array_equal(lidx, elidx) tm.assert_numpy_array_equal(ridx, eridx)
def test_join_outer(self, index_large): other = UInt64Index(2**63 + np.array([7, 12, 25, 1, 2, 10], dtype="uint64")) other_mono = UInt64Index( 2**63 + np.array([1, 2, 7, 10, 12, 25], dtype="uint64")) # not monotonic # guarantee of sortedness res, lidx, ridx = index_large.join(other, how="outer", return_indexers=True) noidx_res = index_large.join(other, how="outer") tm.assert_index_equal(res, noidx_res) eres = UInt64Index( 2**63 + np.array([0, 1, 2, 7, 10, 12, 15, 20, 25], dtype="uint64")) elidx = np.array([0, -1, -1, -1, 1, -1, 2, 3, 4], dtype=np.intp) eridx = np.array([-1, 3, 4, 0, 5, 1, -1, -1, 2], dtype=np.intp) assert isinstance(res, UInt64Index) tm.assert_index_equal(res, eres) tm.assert_numpy_array_equal(lidx, elidx) tm.assert_numpy_array_equal(ridx, eridx) # monotonic res, lidx, ridx = index_large.join(other_mono, how="outer", return_indexers=True) noidx_res = index_large.join(other_mono, how="outer") tm.assert_index_equal(res, noidx_res) elidx = np.array([0, -1, -1, -1, 1, -1, 2, 3, 4], dtype=np.intp) eridx = np.array([-1, 0, 1, 2, 3, 4, -1, -1, 5], dtype=np.intp) assert isinstance(res, UInt64Index) tm.assert_index_equal(res, eres) tm.assert_numpy_array_equal(lidx, elidx) tm.assert_numpy_array_equal(ridx, eridx)
class TestWhere: @pytest.mark.parametrize( "index", [ Float64Index(np.arange(5, dtype="float64")), Int64Index(range(0, 20, 2)), UInt64Index(np.arange(5, dtype="uint64")), ], ) def test_where(self, listlike_box, index): cond = [True] * len(index) expected = index result = index.where(listlike_box(cond)) cond = [False] + [True] * (len(index) - 1) expected = Float64Index([index._na_value] + index[1:].tolist()) result = index.where(listlike_box(cond)) tm.assert_index_equal(result, expected) def test_where_uint64(self): idx = UInt64Index([0, 6, 2]) mask = np.array([False, True, False]) other = np.array([1], dtype=np.int64) expected = UInt64Index([1, 6, 1]) result = idx.where(mask, other) tm.assert_index_equal(result, expected) result = idx.putmask(~mask, other) tm.assert_index_equal(result, expected) def test_where_infers_type_instead_of_trying_to_convert_string_to_float(self): # GH 32413 index = Index([1, np.nan]) cond = index.notna() other = Index(["a", "b"], dtype="string") expected = Index([1.0, "b"]) result = index.where(cond, other) tm.assert_index_equal(result, expected)
def test_uint_index_does_not_convert_to_float64(box): # https://github.com/pandas-dev/pandas/issues/28279 # https://github.com/pandas-dev/pandas/issues/28023 series = Series( [0, 1, 2, 3, 4, 5], index=[ 7606741985629028552, 17876870360202815256, 17876870360202815256, 13106359306506049338, 8991270399732411471, 8991270399732411472, ], ) result = series.loc[box([7606741985629028552, 17876870360202815256])] expected = UInt64Index( [7606741985629028552, 17876870360202815256, 17876870360202815256], dtype="uint64", ) tm.assert_index_equal(result.index, expected) tm.assert_equal(result, series[:3])
def test_map_dtype_inference_unsigned_to_signed(): # GH#44609 cases where we don't retain dtype idx = UInt64Index([1, 2, 3]) result = idx.map(lambda x: -x) expected = Int64Index([-1, -2, -3]) tm.assert_index_equal(result, expected)
def test_constructor_does_not_cast_to_float(self): # https://github.com/numpy/numpy/issues/19146 values = [0, np.iinfo(np.uint64).max] result = UInt64Index(values) assert list(result) == values
def index_large(): # large values used in UInt64Index tests where no compat needed with Int64/Float64 large = [2**63, 2**63 + 10, 2**63 + 15, 2**63 + 20, 2**63 + 25] return UInt64Index(large)
def test_constructor_np_unsigned(self, any_unsigned_int_numpy_dtype): # GH#47475 scalar = np.dtype(any_unsigned_int_numpy_dtype).type(1) result = Index([scalar]) expected = UInt64Index([1]) tm.assert_index_equal(result, expected)