Example #1
0
def test_timedelta2num(pd):
    import timple.timedelta as tmpldelta

    cases = (
        (1, datetime.timedelta(days=1)),
        (0.25, datetime.timedelta(hours=6)),
        (3 / 86400 / 1000, datetime.timedelta(milliseconds=3)),
        ([1, 1.5], [datetime.timedelta(days=1),
                    datetime.timedelta(days=1.5)]),
        (np.nan, np.timedelta64('nat')),
        (2, np.timedelta64(2, 'D')),
        (0.25, np.timedelta64(6, 'h')),
        (3 / 86400 / 1000, np.timedelta64(3, 'ms')),
        ([1, 2], [np.timedelta64(1, 'D'),
                  np.timedelta64(2, 'D')]),
        (2, pd.Timedelta(days=2)),
        (0.25, pd.Timedelta(hours=6)),
        (3 / 86400 / 1000, pd.Timedelta(milliseconds=3)),
        ([1, 1.5], [pd.Timedelta(days=1),
                    pd.Timedelta(days=1.5)]),
        ([], [])  # test
    )

    for expected, tdelta in cases:
        np.testing.assert_equal(tmpldelta.timedelta2num(tdelta), expected)
Example #2
0
def test_timedelta_formatter_usetex():
    import timple.timedelta as tmpldelta

    formatter = tmpldelta.TimedeltaFormatter("%h:%m",
                                             offset_on='days',
                                             offset_fmt="%d %day",
                                             usetex=True)
    values = [
        datetime.timedelta(days=0, hours=12),
        datetime.timedelta(days=1, hours=0),
        datetime.timedelta(days=1, hours=12),
        datetime.timedelta(days=2, hours=0)
    ]

    labels = formatter.format_ticks(tmpldelta.timedelta2num(values))

    start = '$\\mathdefault{'
    i_start = len(start)
    end = '}$'
    i_end = -len(end)

    def verify(string):
        assert string[:i_start] == start
        assert string[i_end:] == end

    # assert ticks are tex formatted
    for lbl in labels:
        verify(lbl)

    # assert offset is tex formatted
    assert re.match(r"\$\\mathdefault\{[\d ]+(\\;)?\}\$days",
                    formatter.get_offset())
Example #3
0
def test_auto_modified_intervald():
    import matplotlib.dates as mdates
    import timple.timedelta as tmpldelta

    locator = tmpldelta.AutoTimedeltaLocator()
    locator.intervald['hours'] = [3]
    locator.create_dummy_axis()
    dt1 = datetime.timedelta(days=1)
    dt2 = datetime.timedelta(days=3)
    locator.axis.set_view_interval(tmpldelta.timedelta2num(dt1),
                                   tmpldelta.timedelta2num(dt2))
    expected = [
        '21:00:00', '1 day, 0:00:00', '1 day, 3:00:00', '1 day, 6:00:00',
        '1 day, 9:00:00', '1 day, 12:00:00', '1 day, 15:00:00',
        '1 day, 18:00:00', '1 day, 21:00:00', '2 days, 0:00:00',
        '2 days, 3:00:00', '2 days, 6:00:00', '2 days, 9:00:00',
        '2 days, 12:00:00', '2 days, 15:00:00', '2 days, 18:00:00',
        '2 days, 21:00:00', '3 days, 0:00:00', '3 days, 3:00:00'
    ]
    # auto would usually be using longer intervals for 2 days
    assert list(map(str, mdates.num2timedelta(locator()))) == expected
Example #4
0
    def _create_timedelta_locator(td1, td2, fmt, kwargs):
        fig, ax = plt.subplots()

        locator = tmpldelta.AutoTimedeltaLocator()
        formatter = tmpldelta.TimedeltaFormatter(fmt, **kwargs)
        ax.yaxis.set_major_locator(locator)
        ax.yaxis.set_major_formatter(formatter)
        ax.set_ylim(tmpldelta.timedelta2num((td1, td2)))
        fig.canvas.draw()
        sts = [st.get_text() for st in ax.get_yticklabels()]
        offset_text = ax.yaxis.get_offset_text().get_text()
        return sts, offset_text
Example #5
0
def test_fixed_timedelta_locator():
    import matplotlib.dates as mdates
    import timple.timedelta as tmpldelta

    results = [
        ('days', 0.5, 0.5,
         ['12:00:00', '1 day, 0:00:00', '1 day, 12:00:00', '2 days, 0:00:00']),
        ('minutes', 20, 1 / mdates.HOURS_PER_DAY, [
            '23:40:00', '1 day, 0:00:00', '1 day, 0:20:00', '1 day, 0:40:00',
            '1 day, 1:00:00', '1 day, 1:20:00'
        ])
    ]
    for base, interval, tdelta, expected in results:
        dt0 = datetime.timedelta(days=1)
        dt1 = dt0 + datetime.timedelta(days=tdelta)
        locator = tmpldelta.FixedTimedeltaLocator(base, interval)
        locator.create_dummy_axis()
        locator.axis.set_view_interval(*tmpldelta.timedelta2num([dt0, dt1]))
        assert list(map(str, mdates.num2timedelta(locator()))) == expected
Example #6
0
 def _create_auto_timedelta_locator(delta1, delta2):
     locator = tmpldelta.AutoTimedeltaLocator()
     locator.create_dummy_axis()
     locator.axis.set_view_interval(tmpldelta.timedelta2num(delta1),
                                    tmpldelta.timedelta2num(delta2))
     return locator
Example #7
0
def test_timedelta2num_pandas_nat(pd):
    import timple.timedelta as tmpldelta

    cases = ((pd.NaT, np.nan), ([pd.NaT, pd.Timedelta(days=1)], [np.nan, 1.0]))
    for x, expected in cases:
        np.testing.assert_equal(tmpldelta.timedelta2num(x), expected)