Exemple #1
0
    def test_distributed(self):
        df = pd.DataFrame([5] + [np.nan] * 8 + [45], list(range(6, 16)))

        convex = df.pipe(distributed_interpolation, direction="convex")
        assert list(convex[0]) == [5, 9, 13, 17, 21, 25, 30, 35, 40, 45]
        concave = df.pipe(distributed_interpolation, direction="concave")
        assert list(concave[0]) == [5, 10, 15, 20, 25, 29, 33, 37, 41, 45]

        df = pd.DataFrame([5] + [np.nan] * 6 + [45], [i / 4 for i in range(8)])
        concave = df.pipe(distributed_interpolation, direction="concave")
        assert len(concave) == 8 and not np.isnan(concave[0]).any()

        df = pd.DataFrame([5] + [np.nan] * 6 + [45] + [np.nan] * 6 + [100],
                          [i / 4 for i in range(15)])
        concave = df.pipe(distributed_interpolation, direction="concave")
        assert len(concave) == 15 and not np.isnan(concave[0]).any()

        with pytest.raises(ValueError) as err:
            df = pd.DataFrame([5] + [np.nan] * 8 + [45],
                              list(range(6, 15)) + [31])
            df.pipe(distributed_interpolation, direction="convex")
        assert str(err.value) == "The DataFrame must have regular steps"

        with pytest.raises(ValueError) as err:
            df = pd.DataFrame([5] + [np.nan] * 9, list(range(6, 16)))
            df.pipe(distributed_interpolation, direction="convex")
        assert str(err.value
                   ) == "The DataFrame must start and end with non nan values"

        with pytest.raises(ValueError) as err:
            df.pipe(distributed_interpolation, direction="unknown")
        assert "unknown" in str(err.value)

        from kanon.units import Sexagesimal

        df = pd.DataFrame([Sexagesimal(5)] + [np.nan] * 8 + [Sexagesimal(45)],
                          list(range(6, 16)))

        convex = df.pipe(distributed_interpolation, direction="convex")
        assert list(convex[0]) == [5, 9, 13, 17, 21, 25, 30, 35, 40, 45]

        df = pd.DataFrame(
            [Sexagesimal("0;0,5")] + [np.nan] * 8 + [Sexagesimal("0;0,45")],
            list(range(6, 16)),
        )

        convex = df.pipe(distributed_interpolation, direction="convex")
        assert list(convex[0]) == [
            Sexagesimal.from_int(x).shift(2)
            for x in [5, 9, 13, 17, 21, 25, 30, 35, 40, 45]
        ]
        assert convex.index[0].dtype == "int64"
Exemple #2
0
def test_options_working():
    table: HTable = HTable.read(180, with_units=False, freeze=True)
    assert table["Entries"].unit is None
    table.loc[18]["Entries"] = Sexagesimal.from_int(500)
    assert table.loc[18]["Entries"] == 500
    assert table.get(18) != 500
Exemple #3
0
def test_init():
    assert (Sexagesimal(
        (1, 2, 31), (6, ), sign=-1,
        remainder=Decimal("0.3")).__repr__() == "-01,02,31 ; 06 |r0.3")

    # From float
    assert Sexagesimal.from_float(-0.016666666666666666, 2) == -Sexagesimal(
        (0, ), (1, ))
    assert Sexagesimal.from_float(0.5, 4).equals(Sexagesimal("0; 30, 0, 0, 0"))
    with pytest.raises(TypeError):
        Sexagesimal.from_float("s", 1)

    # From int
    assert Sexagesimal.from_int(5, 2) == Sexagesimal(5)
    with pytest.raises(TypeError):
        Sexagesimal.from_int("s")

    # From Decimal
    assert Sexagesimal.from_decimal(Decimal(5), 2) == Sexagesimal(5)
    with pytest.raises(TypeError):
        Sexagesimal.from_decimal(5, 1)

    # From Fraction
    assert Sexagesimal.from_fraction(Fraction(5, 1)).equals(Sexagesimal(5))
    assert Sexagesimal.from_fraction(Fraction(5, 2)) == Sexagesimal("2;30")
    with pytest.raises(TypeError):
        Sexagesimal.from_fraction(5)

    # From str
    assert Sexagesimal("21,1,6,3;34") == Sexagesimal((21, 1, 6, 3), (34, ))
    assert Sexagesimal("0,0,0,0;").equals(Sexagesimal(0))
    with pytest.raises(TypeError):
        Sexagesimal._from_string(5)
    with pytest.raises(EmptyStringException):
        Sexagesimal("")
    with pytest.raises(TooManySeparators):
        Sexagesimal("1;2;3")

    # From Sequence
    with pytest.raises(IllegalBaseValueError) as err:
        Sexagesimal((-6, 3), ())
    assert "should be in the range" in str(err.value)
    with pytest.raises(IllegalFloatError) as err:
        Sexagesimal((0.3, 5), (6, 8))
    assert "An illegal float" in str(err.value)

    # From BasedReal

    assert Sexagesimal(Historical("3;15"), 1).equals(Sexagesimal("3;15"))

    class MNumber(BasedReal, base=([5, 2], [3, 4])):
        pass

    n = MNumber.from_float(234.6777, 0)
    assert Sexagesimal(n, 3) == Sexagesimal.from_float(float(n), 3)

    # From multiple ints
    with pytest.raises(ValueError):
        Sexagesimal(3, 5, remainder=Decimal(-5))
    with pytest.raises(ValueError):
        Sexagesimal(3, 5, remainder=0.6)

    # Incorrect parameters
    with pytest.raises(TypeError):
        BasedReal()
    with pytest.raises(ValueError):
        Sexagesimal(Decimal(5))
    with pytest.raises(ValueError):
        Sexagesimal("", "")
    with pytest.raises(ValueError):
        Sexagesimal("", "", "", "")
    with pytest.raises(TypeError):
        Sexagesimal(("a", 2), (1, 2))
    with pytest.raises(ValueError):
        Sexagesimal(1, sign=2)