Exemplo n.º 1
0
    def test_ax_plot(self):
        import matplotlib.pyplot as plt

        x = DatetimeIndex(start='2012-01-02', periods=10, freq='D')
        y = lrange(len(x))
        fig = plt.figure()
        ax = fig.add_subplot(111)
        lines = ax.plot(x, y, label='Y')
        tm.assert_index_equal(DatetimeIndex(lines[0].get_xdata()), x)
Exemplo n.º 2
0
    def test_frame_inferred(self):
        # inferred freq
        import matplotlib.pyplot as plt  # noqa
        idx = date_range('1/1/1987', freq='MS', periods=100)
        idx = DatetimeIndex(idx.values, freq=None)

        df = DataFrame(np.random.randn(len(idx), 3), index=idx)
        _check_plot_works(df.plot)

        # axes freq
        idx = idx[0:40].union(idx[45:99])
        df2 = DataFrame(np.random.randn(len(idx), 3), index=idx)
        _check_plot_works(df2.plot)

        # N > 1
        idx = date_range('2008-1-1 00:15:00', freq='15T', periods=10)
        idx = DatetimeIndex(idx.values, freq=None)
        df = DataFrame(np.random.randn(len(idx), 3), index=idx)
        _check_plot_works(df.plot)
Exemplo n.º 3
0
    def test_resample_weekly_bug_1726(self):
        # 8/6/12 is a Monday
        ind = DatetimeIndex(start="8/6/2012", end="8/26/2012", freq="D")
        n = len(ind)
        data = [[x] * 5 for x in range(n)]
        df = DataFrame(data,
                       columns=['open', 'high', 'low', 'close', 'vol'],
                       index=ind)

        # it works!
        df.resample('W-MON', closed='left', label='left').first()
Exemplo n.º 4
0
    def test_default_left_closed_label(self):
        others = ['MS', 'AS', 'QS', 'D', 'H']
        others_freq = ['D', 'Q', 'M', 'H', 'T']

        for from_freq, to_freq in zip(others_freq, others):
            idx = DatetimeIndex(start='8/15/2012', periods=100, freq=from_freq)
            df = DataFrame(np.random.randn(len(idx), 2), idx)

            resampled = df.resample(to_freq).mean()
            assert_frame_equal(
                resampled,
                df.resample(to_freq, closed='left', label='left').mean())
Exemplo n.º 5
0
    def test_default_right_closed_label(self):
        end_freq = ['D', 'Q', 'M', 'D']
        end_types = ['M', 'A', 'Q', 'W']

        for from_freq, to_freq in zip(end_freq, end_types):
            idx = DatetimeIndex(start='8/15/2012', periods=100, freq=from_freq)
            df = DataFrame(np.random.randn(len(idx), 2), idx)

            resampled = df.resample(to_freq).mean()
            assert_frame_equal(
                resampled,
                df.resample(to_freq, closed='right', label='right').mean())
Exemplo n.º 6
0
    def test_nat_handling(self):

        fig = self.plt.gcf()
        # self.plt.clf()
        ax = fig.add_subplot(111)

        dti = DatetimeIndex(['2015-01-01', NaT, '2015-01-03'])
        s = Series(range(len(dti)), dti)
        s.plot(ax=ax)
        xdata = ax.get_lines()[0].get_xdata()
        # plot x data is bounded by index values
        assert s.index.min() <= Series(xdata).min()
        assert Series(xdata).max() <= s.index.max()
Exemplo n.º 7
0
def test_resample_with_nat():
    # GH 13020
    index = DatetimeIndex([
        pd.NaT, '1970-01-01 00:00:00', pd.NaT, '1970-01-01 00:00:01',
        '1970-01-01 00:00:02'
    ])
    frame = DataFrame([2, 3, 5, 7, 11], index=index)

    index_1s = DatetimeIndex(
        ['1970-01-01 00:00:00', '1970-01-01 00:00:01', '1970-01-01 00:00:02'])
    frame_1s = DataFrame([3, 7, 11], index=index_1s)
    assert_frame_equal(frame.resample('1s').mean(), frame_1s)

    index_2s = DatetimeIndex(['1970-01-01 00:00:00', '1970-01-01 00:00:02'])
    frame_2s = DataFrame([5, 11], index=index_2s)
    assert_frame_equal(frame.resample('2s').mean(), frame_2s)

    index_3s = DatetimeIndex(['1970-01-01 00:00:00'])
    frame_3s = DataFrame([7], index=index_3s)
    assert_frame_equal(frame.resample('3s').mean(), frame_3s)

    assert_frame_equal(frame.resample('60s').mean(), frame_3s)
Exemplo n.º 8
0
def test_resample_apply_product():
    # GH 5586
    index = date_range(start="2012-01-31", freq="M", periods=12)

    ts = Series(range(12), index=index)
    df = DataFrame(dict(A=ts, B=ts + 2))
    result = df.resample("Q").apply(np.product)
    expected = DataFrame(
        np.array([[0, 24], [60, 210], [336, 720], [990, 1716]], dtype=np.int64),
        index=DatetimeIndex(
            ["2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31"], freq="Q-DEC"
        ),
        columns=["A", "B"],
    )
    tm.assert_frame_equal(result, expected)
Exemplo n.º 9
0
def test_downsample_across_dst_weekly():
    # GH 9119, GH 21459
    df = DataFrame(index=DatetimeIndex([
        '2017-03-25', '2017-03-26', '2017-03-27',
        '2017-03-28', '2017-03-29'
    ], tz='Europe/Amsterdam'),
        data=[11, 12, 13, 14, 15])
    result = df.resample('1W').sum()
    expected = DataFrame([23, 42], index=pd.DatetimeIndex([
        '2017-03-26', '2017-04-02'
    ], tz='Europe/Amsterdam'))
    tm.assert_frame_equal(result, expected)

    idx = pd.date_range("2013-04-01", "2013-05-01", tz='Europe/London',
                        freq='H')
    s = Series(index=idx)
    result = s.resample('W').mean()
    expected = Series(index=pd.date_range(
        '2013-04-07', freq='W', periods=5, tz='Europe/London'
    ))
    tm.assert_series_equal(result, expected)
Exemplo n.º 10
0
def test_downsample_across_dst_weekly():
    # GH 9119, GH 21459
    df = DataFrame(
        index=DatetimeIndex(
            ["2017-03-25", "2017-03-26", "2017-03-27", "2017-03-28", "2017-03-29"],
            tz="Europe/Amsterdam",
        ),
        data=[11, 12, 13, 14, 15],
    )
    result = df.resample("1W").sum()
    expected = DataFrame(
        [23, 42],
        index=pd.DatetimeIndex(["2017-03-26", "2017-04-02"], tz="Europe/Amsterdam"),
    )
    tm.assert_frame_equal(result, expected)

    idx = pd.date_range("2013-04-01", "2013-05-01", tz="Europe/London", freq="H")
    s = Series(index=idx)
    result = s.resample("W").mean()
    expected = Series(
        index=pd.date_range("2013-04-07", freq="W", periods=5, tz="Europe/London")
    )
    tm.assert_series_equal(result, expected)
Exemplo n.º 11
0
def test_resample_dst_anchor():
    # 5172
    dti = DatetimeIndex([datetime(2012, 11, 4, 23)], tz='US/Eastern')
    df = DataFrame([5], index=dti)
    assert_frame_equal(df.resample(rule='D').sum(),
                       DataFrame([5], index=df.index.normalize()))
    df.resample(rule='MS').sum()
    assert_frame_equal(
        df.resample(rule='MS').sum(),
        DataFrame([5], index=DatetimeIndex([datetime(2012, 11, 1)],
                                           tz='US/Eastern')))

    dti = date_range('2013-09-30', '2013-11-02', freq='30Min',
                     tz='Europe/Paris')
    values = range(dti.size)
    df = DataFrame({"a": values,
                    "b": values,
                    "c": values}, index=dti, dtype='int64')
    how = {"a": "min", "b": "max", "c": "count"}

    assert_frame_equal(
        df.resample("W-MON").agg(how)[["a", "b", "c"]],
        DataFrame({"a": [0, 48, 384, 720, 1056, 1394],
                   "b": [47, 383, 719, 1055, 1393, 1586],
                   "c": [48, 336, 336, 336, 338, 193]},
                  index=date_range('9/30/2013', '11/4/2013',
                                   freq='W-MON', tz='Europe/Paris')),
        'W-MON Frequency')

    assert_frame_equal(
        df.resample("2W-MON").agg(how)[["a", "b", "c"]],
        DataFrame({"a": [0, 48, 720, 1394],
                   "b": [47, 719, 1393, 1586],
                   "c": [48, 672, 674, 193]},
                  index=date_range('9/30/2013', '11/11/2013',
                                   freq='2W-MON', tz='Europe/Paris')),
        '2W-MON Frequency')

    assert_frame_equal(
        df.resample("MS").agg(how)[["a", "b", "c"]],
        DataFrame({"a": [0, 48, 1538],
                   "b": [47, 1537, 1586],
                   "c": [48, 1490, 49]},
                  index=date_range('9/1/2013', '11/1/2013',
                                   freq='MS', tz='Europe/Paris')),
        'MS Frequency')

    assert_frame_equal(
        df.resample("2MS").agg(how)[["a", "b", "c"]],
        DataFrame({"a": [0, 1538],
                   "b": [1537, 1586],
                   "c": [1538, 49]},
                  index=date_range('9/1/2013', '11/1/2013',
                                   freq='2MS', tz='Europe/Paris')),
        '2MS Frequency')

    df_daily = df['10/26/2013':'10/29/2013']
    assert_frame_equal(
        df_daily.resample("D").agg({"a": "min", "b": "max", "c": "count"})
        [["a", "b", "c"]],
        DataFrame({"a": [1248, 1296, 1346, 1394],
                   "b": [1295, 1345, 1393, 1441],
                   "c": [48, 50, 48, 48]},
                  index=date_range('10/26/2013', '10/29/2013',
                                   freq='D', tz='Europe/Paris')),
        'D Frequency')
Exemplo n.º 12
0
def test_resample_dst_anchor():
    # 5172
    dti = DatetimeIndex([datetime(2012, 11, 4, 23)], tz="US/Eastern")
    df = DataFrame([5], index=dti)
    tm.assert_frame_equal(
        df.resample(rule="D").sum(), DataFrame([5], index=df.index.normalize())
    )
    df.resample(rule="MS").sum()
    tm.assert_frame_equal(
        df.resample(rule="MS").sum(),
        DataFrame([5], index=DatetimeIndex([datetime(2012, 11, 1)], tz="US/Eastern")),
    )

    dti = date_range("2013-09-30", "2013-11-02", freq="30Min", tz="Europe/Paris")
    values = range(dti.size)
    df = DataFrame({"a": values, "b": values, "c": values}, index=dti, dtype="int64")
    how = {"a": "min", "b": "max", "c": "count"}

    tm.assert_frame_equal(
        df.resample("W-MON").agg(how)[["a", "b", "c"]],
        DataFrame(
            {
                "a": [0, 48, 384, 720, 1056, 1394],
                "b": [47, 383, 719, 1055, 1393, 1586],
                "c": [48, 336, 336, 336, 338, 193],
            },
            index=date_range("9/30/2013", "11/4/2013", freq="W-MON", tz="Europe/Paris"),
        ),
        "W-MON Frequency",
    )

    tm.assert_frame_equal(
        df.resample("2W-MON").agg(how)[["a", "b", "c"]],
        DataFrame(
            {
                "a": [0, 48, 720, 1394],
                "b": [47, 719, 1393, 1586],
                "c": [48, 672, 674, 193],
            },
            index=date_range(
                "9/30/2013", "11/11/2013", freq="2W-MON", tz="Europe/Paris"
            ),
        ),
        "2W-MON Frequency",
    )

    tm.assert_frame_equal(
        df.resample("MS").agg(how)[["a", "b", "c"]],
        DataFrame(
            {"a": [0, 48, 1538], "b": [47, 1537, 1586], "c": [48, 1490, 49]},
            index=date_range("9/1/2013", "11/1/2013", freq="MS", tz="Europe/Paris"),
        ),
        "MS Frequency",
    )

    tm.assert_frame_equal(
        df.resample("2MS").agg(how)[["a", "b", "c"]],
        DataFrame(
            {"a": [0, 1538], "b": [1537, 1586], "c": [1538, 49]},
            index=date_range("9/1/2013", "11/1/2013", freq="2MS", tz="Europe/Paris"),
        ),
        "2MS Frequency",
    )

    df_daily = df["10/26/2013":"10/29/2013"]
    tm.assert_frame_equal(
        df_daily.resample("D").agg({"a": "min", "b": "max", "c": "count"})[
            ["a", "b", "c"]
        ],
        DataFrame(
            {
                "a": [1248, 1296, 1346, 1394],
                "b": [1295, 1345, 1393, 1441],
                "c": [48, 50, 48, 48],
            },
            index=date_range("10/26/2013", "10/29/2013", freq="D", tz="Europe/Paris"),
        ),
        "D Frequency",
    )
Exemplo n.º 13
0
from datetime import datetime

import numpy as np
import pytest

from pandas.compat import OrderedDict, range

import pandas as pd
from pandas import DataFrame, Series
from pandas.core.indexes.datetimes import date_range
from pandas.core.resample import DatetimeIndex
import pandas.util.testing as tm
from pandas.util.testing import assert_frame_equal, assert_series_equal

dti = DatetimeIndex(start=datetime(2005, 1, 1),
                    end=datetime(2005, 1, 10), freq='Min')

test_series = Series(np.random.rand(len(dti)), dti)
test_frame = DataFrame(
    {'A': test_series, 'B': test_series, 'C': np.arange(len(dti))})


def test_str():

    r = test_series.resample('H')
    assert ('DatetimeIndexResampler [freq=<Hour>, axis=0, closed=left, '
            'label=left, convention=start, base=0]' in str(r))


def test_api():