예제 #1
0
    def test_take_fill_value_float64(self):
        # GH 12631
        idx = Float64Index([1.0, 2.0, 3.0], name="xxx")
        result = idx.take(np.array([1, 0, -1]))
        expected = Float64Index([2.0, 1.0, 3.0], name="xxx")
        tm.assert_index_equal(result, expected)

        # fill_value
        result = idx.take(np.array([1, 0, -1]), fill_value=True)
        expected = Float64Index([2.0, 1.0, np.nan], name="xxx")
        tm.assert_index_equal(result, expected)

        # allow_fill=False
        result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
        expected = Float64Index([2.0, 1.0, 3.0], name="xxx")
        tm.assert_index_equal(result, expected)

        msg = (
            "When allow_fill=True and fill_value is not None, "
            "all indices must be >= -1"
        )
        with pytest.raises(ValueError, match=msg):
            idx.take(np.array([1, 0, -2]), fill_value=True)
        with pytest.raises(ValueError, match=msg):
            idx.take(np.array([1, 0, -5]), fill_value=True)

        msg = "index -5 is out of bounds for (axis 0 with )?size 3"
        with pytest.raises(IndexError, match=msg):
            idx.take(np.array([1, -5]))
예제 #2
0
    def test_range_float_union_dtype(self):
        # https://github.com/pandas-dev/pandas/issues/26778
        index = RangeIndex(start=0, stop=3)
        other = Float64Index([0.5, 1.5])
        result = index.union(other)
        expected = Float64Index([0.0, 0.5, 1, 1.5, 2.0])
        tm.assert_index_equal(result, expected)

        result = other.union(index)
        tm.assert_index_equal(result, expected)
예제 #3
0
    def test_int_float_union_dtype(self, dtype):
        # https://github.com/pandas-dev/pandas/issues/26778
        # [u]int | float -> float
        index = Index([0, 2, 3], dtype=dtype)
        other = Float64Index([0.5, 1.5])
        expected = Float64Index([0.0, 0.5, 1.5, 2.0, 3.0])
        result = index.union(other)
        tm.assert_index_equal(result, expected)

        result = other.union(index)
        tm.assert_index_equal(result, expected)
예제 #4
0
    def test_astype_float64_to_float_dtype(self, dtype):
        # GH#12881
        # a float astype int
        idx = Float64Index([0, 1, 2])
        result = idx.astype(dtype)
        expected = idx
        tm.assert_index_equal(result, expected)

        idx = Float64Index([0, 1.1, 2])
        result = idx.astype(dtype)
        expected = Index(idx.values.astype(dtype))
        tm.assert_index_equal(result, expected)
예제 #5
0
    def test_cannot_cast_to_datetimelike(self, dtype):
        idx = Float64Index([0, 1.1, 2])

        msg = (f"Cannot convert Float64Index to dtype {pandas_dtype(dtype)}; "
               f"integer values are required for conversion")
        with pytest.raises(TypeError, match=re.escape(msg)):
            idx.astype(dtype)
예제 #6
0
 def test_astype_float64_to_object(self):
     float_index = Float64Index([0.0, 2.5, 5.0, 7.5, 10.0])
     result = float_index.astype(object)
     assert result.equals(float_index)
     assert float_index.equals(result)
     assert isinstance(result,
                       Index) and not isinstance(result, Float64Index)
예제 #7
0
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)
예제 #8
0
    def test_cannot_cast_inf_to_int(self, non_finite, dtype):
        # GH#13149
        idx = Float64Index([1, 2, non_finite])

        msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
        with pytest.raises(ValueError, match=msg):
            idx.astype(dtype)
예제 #9
0
    def test_get_loc_float64(self):
        idx = Float64Index([0.0, 1.0, 2.0])
        for method in [None, "pad", "backfill", "nearest"]:
            assert idx.get_loc(1, method) == 1
            if method is not None:
                assert idx.get_loc(1, method, tolerance=0) == 1

        for method, loc in [("pad", 1), ("backfill", 2), ("nearest", 1)]:
            assert idx.get_loc(1.1, method) == loc
            assert idx.get_loc(1.1, method, tolerance=0.9) == loc

        with pytest.raises(KeyError, match="^'foo'$"):
            idx.get_loc("foo")
        with pytest.raises(KeyError, match=r"^1\.5$"):
            idx.get_loc(1.5)
        with pytest.raises(KeyError, match=r"^1\.5$"):
            idx.get_loc(1.5, method="pad", tolerance=0.1)
        with pytest.raises(KeyError, match="^True$"):
            idx.get_loc(True)
        with pytest.raises(KeyError, match="^False$"):
            idx.get_loc(False)

        with pytest.raises(ValueError, match="must be numeric"):
            idx.get_loc(1.4, method="nearest", tolerance="foo")

        with pytest.raises(ValueError, match="must contain numeric elements"):
            idx.get_loc(1.4, method="nearest", tolerance=np.array(["foo"]))

        with pytest.raises(
            ValueError, match="tolerance size must match target index size"
        ):
            idx.get_loc(1.4, method="nearest", tolerance=np.array([1, 2]))
예제 #10
0
 def test_astype_float64_mixed_to_object(self):
     # mixed int-float
     idx = Float64Index([1.5, 2, 3, 4, 5])
     idx.name = "foo"
     result = idx.astype(object)
     assert result.equals(idx)
     assert idx.equals(result)
     assert isinstance(result, Index) and not isinstance(result, Float64Index)
예제 #11
0
    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)
예제 #12
0
    def test_get_loc_na(self):
        idx = Float64Index([np.nan, 1, 2])
        assert idx.get_loc(1) == 1
        assert idx.get_loc(np.nan) == 0

        idx = Float64Index([np.nan, 1, np.nan])
        assert idx.get_loc(1) == 1

        # representable by slice [0:2:2]
        msg = "'Cannot get left slice bound for non-unique label: nan'"
        with pytest.raises(KeyError, match=msg):
            idx.slice_locs(np.nan)
        # not representable by slice
        idx = Float64Index([np.nan, 1, np.nan, np.nan])
        assert idx.get_loc(1) == 1
        msg = "'Cannot get left slice bound for non-unique label: nan"
        with pytest.raises(KeyError, match=msg):
            idx.slice_locs(np.nan)
예제 #13
0
    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)
예제 #14
0
 def test_get_loc_missing_nan(self):
     # GH#8569
     idx = Float64Index([1, 2])
     assert idx.get_loc(1) == 0
     with pytest.raises(KeyError, match=r"^3$"):
         idx.get_loc(3)
     with pytest.raises(KeyError, match="^nan$"):
         idx.get_loc(np.nan)
     with pytest.raises(InvalidIndexError, match=r"\[nan\]"):
         # listlike/non-hashable raises TypeError
         idx.get_loc([np.nan])
예제 #15
0
    def test_get_indexer_float64(self):
        idx = Float64Index([0.0, 1.0, 2.0])
        tm.assert_numpy_array_equal(idx.get_indexer(idx),
                                    np.array([0, 1, 2], dtype=np.intp))

        target = [-0.1, 0.5, 1.1]
        tm.assert_numpy_array_equal(idx.get_indexer(target, "pad"),
                                    np.array([-1, 0, 1], dtype=np.intp))
        tm.assert_numpy_array_equal(idx.get_indexer(target, "backfill"),
                                    np.array([0, 1, 2], dtype=np.intp))
        tm.assert_numpy_array_equal(idx.get_indexer(target, "nearest"),
                                    np.array([0, 1, 1], dtype=np.intp))
예제 #16
0
    def test_insert(self):

        idx = RangeIndex(5, name="Foo")
        result = idx[1:4]

        # test 0th element
        tm.assert_index_equal(idx[0:4], result.insert(0, idx[0]), exact="equiv")

        # GH 18295 (test missing)
        expected = Float64Index([0, np.nan, 1, 2, 3, 4])
        for na in [np.nan, None, pd.NA]:
            result = RangeIndex(5).insert(1, na)
            tm.assert_index_equal(result, expected)

        result = RangeIndex(5).insert(1, pd.NaT)
        expected = Index([0, pd.NaT, 1, 2, 3, 4], dtype=object)
        tm.assert_index_equal(result, expected)
예제 #17
0
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)
예제 #18
0
 def test_contains_float64_not_nans(self):
     index = Float64Index([1.0, 2.0, np.nan])
     assert 1.0 in index
예제 #19
0
 def test_get_indexer_nan(self):
     # GH#7820
     result = Float64Index([1, 2, np.nan]).get_indexer([np.nan])
     expected = np.array([2], dtype=np.intp)
     tm.assert_numpy_array_equal(result, expected)
예제 #20
0
 def test_astype_from_object(self):
     index = Index([1.0, np.nan, 0.2], dtype="object")
     result = index.astype(float)
     expected = Float64Index([1.0, np.nan, 0.2])
     assert result.dtype == expected.dtype
     tm.assert_index_equal(result, expected)