Exemplo n.º 1
0
    def test_from_weekly_resampling(self):
        idxh = date_range('1/1/1999', periods=52, freq='W')
        idxl = date_range('1/1/1999', periods=12, freq='M')
        high = Series(np.random.randn(len(idxh)), idxh)
        low = Series(np.random.randn(len(idxl)), idxl)
        low.plot()
        ax = high.plot()

        expected_h = idxh.to_period().asi8.astype(np.float64)
        expected_l = np.array([1514, 1519, 1523, 1527, 1531, 1536, 1540, 1544,
                               1549, 1553, 1558, 1562], dtype=np.float64)
        for l in ax.get_lines():
            self.assertTrue(PeriodIndex(data=l.get_xdata()).freq, idxh.freq)
            xdata = l.get_xdata(orig=False)
            if len(xdata) == 12:  # idxl lines
                tm.assert_numpy_array_equal(xdata, expected_l)
            else:
                tm.assert_numpy_array_equal(xdata, expected_h)
        tm.close()

        # tsplot
        from pandas.tseries.plotting import tsplot
        import matplotlib.pyplot as plt

        tsplot(low, plt.Axes.plot)
        lines = tsplot(high, plt.Axes.plot)
        for l in lines:
            self.assertTrue(PeriodIndex(data=l.get_xdata()).freq, idxh.freq)
            xdata = l.get_xdata(orig=False)
            if len(xdata) == 12:  # idxl lines
                tm.assert_numpy_array_equal(xdata, expected_l)
            else:
                tm.assert_numpy_array_equal(xdata, expected_h)
Exemplo n.º 2
0
def test_period_index_construction_from_strings(klass):
    # https://github.com/pandas-dev/pandas/issues/26109
    strings = ["2020Q1", "2020Q2"] * 2
    data = klass(strings)
    result = PeriodIndex(data, freq="Q")
    expected = PeriodIndex([Period(s) for s in strings])
    tm.assert_index_equal(result, expected)
Exemplo n.º 3
0
 def test_resample_with_only_nat(self):
     # GH 13224
     pi = PeriodIndex([pd.NaT] * 3, freq="S")
     frame = DataFrame([2, 3, 5], index=pi)
     expected_index = PeriodIndex(data=[], freq=pi.freq)
     expected = DataFrame(index=expected_index)
     result = frame.resample("1s").mean()
     assert_frame_equal(result, expected)
Exemplo n.º 4
0
 def test_resample_with_only_nat(self):
     # GH 13224
     pi = PeriodIndex([pd.NaT] * 3, freq="S")
     frame = DataFrame([2, 3, 5], index=pi, columns=["a"])
     expected_index = PeriodIndex(data=[], freq=pi.freq)
     expected = DataFrame(index=expected_index, columns=["a"], dtype="float64")
     result = frame.resample("1s").mean()
     tm.assert_frame_equal(result, expected)
Exemplo n.º 5
0
 def test_secondary_upsample(self):
     idxh = date_range('1/1/1999', periods=365, freq='D')
     idxl = date_range('1/1/1999', periods=12, freq='M')
     high = Series(np.random.randn(len(idxh)), idxh)
     low = Series(np.random.randn(len(idxl)), idxl)
     low.plot()
     ax = high.plot(secondary_y=True)
     for l in ax.get_lines():
         self.assertEqual(PeriodIndex(l.get_xdata()).freq, 'D')
     self.assertTrue(hasattr(ax, 'left_ax'))
     self.assertFalse(hasattr(ax, 'right_ax'))
     for l in ax.left_ax.get_lines():
         self.assertEqual(PeriodIndex(l.get_xdata()).freq, 'D')
Exemplo n.º 6
0
    def test_upsampling_ohlc(self, freq, period_mult, kind):
        # GH 13083
        pi = PeriodIndex(start='2000', freq='D', periods=10)
        s = Series(range(len(pi)), index=pi)
        expected = s.to_timestamp().resample(freq).ohlc().to_period(freq)

        # timestamp-based resampling doesn't include all sub-periods
        # of the last original period, so extend accordingly:
        new_index = PeriodIndex(start='2000',
                                freq=freq,
                                periods=period_mult * len(pi))
        expected = expected.reindex(new_index)
        result = s.resample(freq, kind=kind).ohlc()
        assert_frame_equal(result, expected)
 def test_business_freq(self):
     import matplotlib.pyplot as plt  # noqa
     bts = tm.makePeriodSeries()
     ax = bts.plot()
     assert ax.get_lines()[0].get_xydata()[0, 0] == bts.index[0].ordinal
     idx = ax.get_lines()[0].get_xdata()
     assert PeriodIndex(data=idx).freqstr == 'B'
Exemplo n.º 8
0
 def test_mixed_freq_regular_first_df(self):
     # GH 9852
     import matplotlib.pyplot as plt  # noqa
     s1 = tm.makeTimeSeries().to_frame()
     s2 = s1.iloc[[0, 5, 10, 11, 12, 13, 14, 15], :]
     ax = s1.plot()
     ax2 = s2.plot(style='g', ax=ax)
     lines = ax2.get_lines()
     idx1 = PeriodIndex(lines[0].get_xdata())
     idx2 = PeriodIndex(lines[1].get_xdata())
     self.assertTrue(idx1.equals(s1.index.to_period('B')))
     self.assertTrue(idx2.equals(s2.index.to_period('B')))
     left, right = ax2.get_xlim()
     pidx = s1.index.to_period()
     self.assertEqual(left, pidx[0].ordinal)
     self.assertEqual(right, pidx[-1].ordinal)
Exemplo n.º 9
0
    def test_strftime_nat(self):
        # GH 29578
        arr = PeriodArray(PeriodIndex(["2019-01-01", pd.NaT], dtype="period[D]"))

        result = arr.strftime("%Y-%m-%d")
        expected = np.array(["2019-01-01", np.nan], dtype=object)
        tm.assert_numpy_array_equal(result, expected)
Exemplo n.º 10
0
 def test_mixed_freq_regular_first_df(self):
     # GH 9852
     s1 = tm.makeTimeSeries().to_frame()
     s2 = s1.iloc[[0, 5, 10, 11, 12, 13, 14, 15], :]
     _, ax = self.plt.subplots()
     s1.plot(ax=ax)
     ax2 = s2.plot(style='g', ax=ax)
     lines = ax2.get_lines()
     idx1 = PeriodIndex(lines[0].get_xdata())
     idx2 = PeriodIndex(lines[1].get_xdata())
     assert idx1.equals(s1.index.to_period('B'))
     assert idx2.equals(s2.index.to_period('B'))
     left, right = ax2.get_xlim()
     pidx = s1.index.to_period()
     assert left == pidx[0].ordinal
     assert right == pidx[-1].ordinal
Exemplo n.º 11
0
    def _get_period_bins(self, ax):
        if not isinstance(ax, PeriodIndex):
            raise TypeError('axis must be a PeriodIndex, but got '
                            'an instance of %r' % type(ax).__name__)

        memb = ax.asfreq(self.freq, how=self.convention)

        # NaT handling as in pandas._lib.lib.generate_bins_dt64()
        nat_count = 0
        if memb.hasnans:
            nat_count = np.sum(memb._isnan)
            memb = memb[~memb._isnan]

        # if index contains no valid (non-NaT) values, return empty index
        if not len(memb):
            binner = labels = PeriodIndex(data=[],
                                          freq=self.freq,
                                          name=ax.name)
            return binner, [], labels

        start = ax.min().asfreq(self.freq, how=self.convention)
        end = ax.max().asfreq(self.freq, how='end')

        labels = binner = PeriodIndex(start=start,
                                      end=end,
                                      freq=self.freq,
                                      name=ax.name)

        i8 = memb.asi8
        freq_mult = self.freq.n

        # when upsampling to subperiods, we need to generate enough bins
        expected_bins_count = len(binner) * freq_mult
        i8_extend = expected_bins_count - (i8[-1] - i8[0])
        rng = np.arange(i8[0], i8[-1] + i8_extend, freq_mult)
        rng += freq_mult
        bins = memb.searchsorted(rng, side='left')

        if nat_count > 0:
            # NaT handling as in pandas._lib.lib.generate_bins_dt64()
            # shift bins by the number of NaT
            bins += nat_count
            bins = np.insert(bins, 0, nat_count)
            binner = binner.insert(0, tslib.NaT)
            labels = labels.insert(0, tslib.NaT)

        return binner, bins, labels
Exemplo n.º 12
0
def maybe_to_datetimelike(data, copy=False):
    """
    return a DelegatedClass of a Series that is datetimelike
      (e.g. datetime64[ns],timedelta64[ns] dtype or a Series of Periods)
    raise TypeError if this is not possible.

    Parameters
    ----------
    data : Series
    copy : boolean, default False
           copy the input data

    Returns
    -------
    DelegatedClass

    """
    from pandas import Series

    if not isinstance(data, Series):
        raise TypeError("cannot convert an object of type {0} to a "
                        "datetimelike index".format(type(data)))

    index = data.index
    name = data.name
    orig = data if is_categorical_dtype(data) else None
    if orig is not None:
        data = orig.values.categories

    if is_datetime64_dtype(data.dtype):
        return DatetimeProperties(DatetimeIndex(data, copy=copy),
                                  index,
                                  name=name,
                                  orig=orig)
    elif is_datetime64tz_dtype(data.dtype):
        return DatetimeProperties(DatetimeIndex(data, copy=copy),
                                  index,
                                  data.name,
                                  orig=orig)
    elif is_timedelta64_dtype(data.dtype):
        return TimedeltaProperties(TimedeltaIndex(data, copy=copy),
                                   index,
                                   name=name,
                                   orig=orig)
    else:
        if is_period_arraylike(data):
            return PeriodProperties(PeriodIndex(data, copy=copy),
                                    index,
                                    name=name,
                                    orig=orig)
        if is_datetime_arraylike(data):
            return DatetimeProperties(DatetimeIndex(data, copy=copy),
                                      index,
                                      name=name,
                                      orig=orig)

    raise TypeError("cannot convert an object of type {0} to a "
                    "datetimelike index".format(type(data)))
Exemplo n.º 13
0
    def test_to_weekly_resampling(self):
        idxh = date_range('1/1/1999', periods=52, freq='W')
        idxl = date_range('1/1/1999', periods=12, freq='M')
        high = Series(np.random.randn(len(idxh)), idxh)
        low = Series(np.random.randn(len(idxl)), idxl)
        high.plot()
        ax = low.plot()
        for l in ax.get_lines():
            self.assertEqual(PeriodIndex(data=l.get_xdata()).freq, idxh.freq)

        # tsplot
        from pandas.tseries.plotting import tsplot
        import matplotlib.pyplot as plt

        tsplot(high, plt.Axes.plot)
        lines = tsplot(low, plt.Axes.plot)
        for l in lines:
            self.assertTrue(PeriodIndex(data=l.get_xdata()).freq, idxh.freq)
Exemplo n.º 14
0
    def test_resample_fill_missing(self):
        rng = PeriodIndex([2000, 2005, 2007, 2009], freq="A")

        s = Series(np.random.randn(4), index=rng)

        stamps = s.to_timestamp()
        filled = s.resample("A").ffill()
        expected = stamps.resample("A").ffill().to_period("A")
        assert_series_equal(filled, expected)
Exemplo n.º 15
0
 def test_mixed_freq_hf_first(self):
     idxh = date_range('1/1/1999', periods=365, freq='D')
     idxl = date_range('1/1/1999', periods=12, freq='M')
     high = Series(np.random.randn(len(idxh)), idxh)
     low = Series(np.random.randn(len(idxl)), idxl)
     high.plot()
     ax = low.plot()
     for l in ax.get_lines():
         self.assertEqual(PeriodIndex(data=l.get_xdata()).freq, 'D')
Exemplo n.º 16
0
    def test_mixed_freq_regular_first(self):
        # TODO
        s1 = tm.makeTimeSeries()
        s2 = s1[[0, 5, 10, 11, 12, 13, 14, 15]]

        # it works!
        s1.plot()

        ax2 = s2.plot(style='g')
        lines = ax2.get_lines()
        idx1 = PeriodIndex(lines[0].get_xdata())
        idx2 = PeriodIndex(lines[1].get_xdata())
        assert idx1.equals(s1.index.to_period('B'))
        assert idx2.equals(s2.index.to_period('B'))
        left, right = ax2.get_xlim()
        pidx = s1.index.to_period()
        assert left == pidx[0].ordinal
        assert right == pidx[-1].ordinal
Exemplo n.º 17
0
    def test_resample_with_nat(self, periods, values, freq, expected_values):
        # GH 13224
        index = PeriodIndex(periods, freq='S')
        frame = DataFrame(values, index=index)

        expected_index = period_range('1970-01-01 00:00:00',
                                      periods=len(expected_values), freq=freq)
        expected = DataFrame(expected_values, index=expected_index)
        result = frame.resample(freq).mean()
        assert_frame_equal(result, expected)
Exemplo n.º 18
0
 def test_business_freq_convert(self):
     n = tm.N
     tm.N = 300
     bts = tm.makeTimeSeries().asfreq('BM')
     tm.N = n
     ts = bts.to_period('M')
     ax = bts.plot()
     assert ax.get_lines()[0].get_xydata()[0, 0] == ts.index[0].ordinal
     idx = ax.get_lines()[0].get_xdata()
     assert PeriodIndex(data=idx).freqstr == 'M'
Exemplo n.º 19
0
    def test_mixed_freq_second_millisecond(self):
        # GH 7772, GH 7760
        idxh = date_range('2014-07-01 09:00', freq='S', periods=50)
        idxl = date_range('2014-07-01 09:00', freq='100L', periods=500)
        high = Series(np.random.randn(len(idxh)), idxh)
        low = Series(np.random.randn(len(idxl)), idxl)
        # high to low
        high.plot()
        ax = low.plot()
        self.assertEqual(len(ax.get_lines()), 2)
        for l in ax.get_lines():
            self.assertEqual(PeriodIndex(data=l.get_xdata()).freq, 'L')
        tm.close()

        # low to high
        low.plot()
        ax = high.plot()
        self.assertEqual(len(ax.get_lines()), 2)
        for l in ax.get_lines():
            self.assertEqual(PeriodIndex(data=l.get_xdata()).freq, 'L')
Exemplo n.º 20
0
 def test_sum_min_count(self):
     # GH 19974
     index = date_range(start="2018", freq="M", periods=6)
     data = np.ones(6)
     data[3:6] = np.nan
     s = Series(data, index).to_period()
     result = s.resample("Q").sum(min_count=1)
     expected = Series([3.0, np.nan],
                       index=PeriodIndex(["2018Q1", "2018Q2"],
                                         freq="Q-DEC"))
     tm.assert_series_equal(result, expected)
Exemplo n.º 21
0
    def _get_time_period_bins(self, ax):
        if not isinstance(ax, DatetimeIndex):
            raise TypeError('axis must be a DatetimeIndex, but got '
                            'an instance of %r' % type(ax).__name__)

        if not len(ax):
            binner = labels = PeriodIndex(
                data=[], freq=self.freq, name=ax.name)
            return binner, [], labels

        labels = binner = PeriodIndex(start=ax[0],
                                      end=ax[-1],
                                      freq=self.freq,
                                      name=ax.name)

        end_stamps = (labels + 1).asfreq(self.freq, 's').to_timestamp()
        if ax.tzinfo:
            end_stamps = end_stamps.tz_localize(ax.tzinfo)
        bins = ax.searchsorted(end_stamps, side='left')

        return binner, bins, labels
Exemplo n.º 22
0
 def test_resample_basic(self):
     # GH3609
     s = Series(range(100), index=date_range(
         '20130101', freq='s', periods=100, name='idx'), dtype='float')
     s[10:30] = np.nan
     index = PeriodIndex([
         Period('2013-01-01 00:00', 'T'),
         Period('2013-01-01 00:01', 'T')], name='idx')
     expected = Series([34.5, 79.5], index=index)
     result = s.to_period().resample('T', kind='period').mean()
     assert_series_equal(result, expected)
     result2 = s.resample('T', kind='period').mean()
     assert_series_equal(result2, expected)
Exemplo n.º 23
0
    def test_mixed_freq_lf_first(self):
        import matplotlib.pyplot as plt

        idxh = date_range('1/1/1999', periods=365, freq='D')
        idxl = date_range('1/1/1999', periods=12, freq='M')
        high = Series(np.random.randn(len(idxh)), idxh)
        low = Series(np.random.randn(len(idxl)), idxl)
        low.plot(legend=True)
        ax = high.plot(legend=True)
        for l in ax.get_lines():
            self.assertEqual(PeriodIndex(data=l.get_xdata()).freq, 'D')
        leg = ax.get_legend()
        self.assertEqual(len(leg.texts), 2)
        plt.close(ax.get_figure())

        idxh = date_range('1/1/1999', periods=240, freq='T')
        idxl = date_range('1/1/1999', periods=4, freq='H')
        high = Series(np.random.randn(len(idxh)), idxh)
        low = Series(np.random.randn(len(idxl)), idxl)
        low.plot()
        ax = high.plot()
        for l in ax.get_lines():
            self.assertEqual(PeriodIndex(data=l.get_xdata()).freq, 'T')
Exemplo n.º 24
0
 def _convert_1d(values, units, axis):
     if not hasattr(axis, "freq"):
         raise TypeError("Axis must have `freq` set to convert to Periods")
     valid_types = (str, datetime, Period, pydt.date, pydt.time, np.datetime64)
     if isinstance(values, valid_types) or is_integer(values) or is_float(values):
         return get_datevalue(values, axis.freq)
     elif isinstance(values, PeriodIndex):
         return values.asfreq(axis.freq).asi8
     elif isinstance(values, Index):
         return values.map(lambda x: get_datevalue(x, axis.freq))
     elif lib.infer_dtype(values, skipna=False) == "period":
         # https://github.com/pandas-dev/pandas/issues/24304
         # convert ndarray[period] -> PeriodIndex
         return PeriodIndex(values, freq=axis.freq).asi8
     elif isinstance(values, (list, tuple, np.ndarray, Index)):
         return [get_datevalue(x, axis.freq) for x in values]
     return values
Exemplo n.º 25
0
 def test_resample_basic(self):
     # GH3609
     s = Series(
         range(100),
         index=date_range("20130101", freq="s", periods=100, name="idx"),
         dtype="float",
     )
     s[10:30] = np.nan
     index = PeriodIndex(
         [Period("2013-01-01 00:00", "T"), Period("2013-01-01 00:01", "T")],
         name="idx",
     )
     expected = Series([34.5, 79.5], index=index)
     result = s.to_period().resample("T", kind="period").mean()
     tm.assert_series_equal(result, expected)
     result2 = s.resample("T", kind="period").mean()
     tm.assert_series_equal(result2, expected)
Exemplo n.º 26
0
 def _convert_1d(values, units, axis):
     if not hasattr(axis, 'freq'):
         raise TypeError('Axis must have `freq` set to convert to Periods')
     valid_types = (compat.string_types, datetime, Period, pydt.date,
                    pydt.time, np.datetime64)
     if (isinstance(values, valid_types) or is_integer(values)
             or is_float(values)):
         return get_datevalue(values, axis.freq)
     if isinstance(values, PeriodIndex):
         return values.asfreq(axis.freq)._ndarray_values
     if isinstance(values, Index):
         return values.map(lambda x: get_datevalue(x, axis.freq))
     if is_period_arraylike(values):
         return PeriodIndex(values, freq=axis.freq)._ndarray_values
     if isinstance(values, (list, tuple, np.ndarray, Index)):
         return [get_datevalue(x, axis.freq) for x in values]
     return values
Exemplo n.º 27
0
    def _get_values(self):
        data = self.values
        if is_datetime64_dtype(data.dtype):
            return DatetimeIndex(data, copy=False, name=self.name)

        elif is_datetime64tz_dtype(data.dtype):
            return DatetimeIndex(data, copy=False, name=self.name)

        elif is_timedelta64_dtype(data.dtype):
            return TimedeltaIndex(data, copy=False, name=self.name)

        else:
            if is_period_arraylike(data):
                return PeriodIndex(data, copy=False, name=self.name)
            if is_datetime_arraylike(data):
                return DatetimeIndex(data, copy=False, name=self.name)

        raise TypeError("cannot convert an object of type {0} to a "
                        "datetimelike index".format(type(data)))
Exemplo n.º 28
0
 def test_mixed_freq_regular_first_df(self):
     # GH 9852
     import matplotlib.pyplot as plt  # noqa
     s1 = tm.makeTimeSeries().to_frame()
     s2 = s1.iloc[[0, 5, 10, 11, 12, 13, 14, 15], :]
     ax = s1.plot()
     ax2 = s2.plot(style='g', ax=ax)
     lines = ax2.get_lines()
     idx1 = PeriodIndex(lines[0].get_xdata())
     idx2 = PeriodIndex(lines[1].get_xdata())
     self.assertTrue(idx1.equals(s1.index.to_period('B')))
     self.assertTrue(idx2.equals(s2.index.to_period('B')))
     left, right = ax2.get_xlim()
     pidx = s1.index.to_period()
     self.assertEqual(left, pidx[0].ordinal)
     self.assertEqual(right, pidx[-1].ordinal)
Exemplo n.º 29
0
    def test_mixed_freq_regular_first(self):
        import matplotlib.pyplot as plt  # noqa
        s1 = tm.makeTimeSeries()
        s2 = s1[[0, 5, 10, 11, 12, 13, 14, 15]]

        # it works!
        s1.plot()

        ax2 = s2.plot(style='g')
        lines = ax2.get_lines()
        idx1 = PeriodIndex(lines[0].get_xdata())
        idx2 = PeriodIndex(lines[1].get_xdata())
        assert idx1.equals(s1.index.to_period('B'))
        assert idx2.equals(s2.index.to_period('B'))
        left, right = ax2.get_xlim()
        pidx = s1.index.to_period()
        assert left == pidx[0].ordinal
        assert right == pidx[-1].ordinal
Exemplo n.º 30
0
 def test_raise_if_period_index(self):
     index = PeriodIndex(start="1/1/1990", periods=20, freq="M")
     pytest.raises(TypeError, frequencies.infer_freq, index)
Exemplo n.º 31
0
 def test_cant_fill_missing_dups(self):
     rng = PeriodIndex([2000, 2005, 2005, 2007, 2007], freq="A")
     s = Series(np.random.randn(5), index=rng)
     msg = "Reindexing only valid with uniquely valued Index objects"
     with pytest.raises(InvalidIndexError, match=msg):
         s.resample("A").ffill()
Exemplo n.º 32
0
    def test_from_resampling_area_line_mixed(self):
        idxh = date_range('1/1/1999', periods=52, freq='W')
        idxl = date_range('1/1/1999', periods=12, freq='M')
        high = DataFrame(np.random.rand(len(idxh), 3),
                         index=idxh,
                         columns=[0, 1, 2])
        low = DataFrame(np.random.rand(len(idxl), 3),
                        index=idxl,
                        columns=[0, 1, 2])

        # low to high
        for kind1, kind2 in [('line', 'area'), ('area', 'line')]:
            ax = low.plot(kind=kind1, stacked=True)
            ax = high.plot(kind=kind2, stacked=True, ax=ax)

            # check low dataframe result
            expected_x = np.array([
                1514, 1519, 1523, 1527, 1531, 1536, 1540, 1544, 1549, 1553,
                1558, 1562
            ],
                                  dtype=np.float64)
            expected_y = np.zeros(len(expected_x), dtype=np.float64)
            for i in range(3):
                l = ax.lines[i]
                self.assertEqual(PeriodIndex(l.get_xdata()).freq, idxh.freq)
                self.assert_numpy_array_equal(l.get_xdata(orig=False),
                                              expected_x)
                # check stacked values are correct
                expected_y += low[i].values
                self.assert_numpy_array_equal(l.get_ydata(orig=False),
                                              expected_y)

            # check high dataframe result
            expected_x = idxh.to_period().asi8.astype(np.float64)
            expected_y = np.zeros(len(expected_x), dtype=np.float64)
            for i in range(3):
                l = ax.lines[3 + i]
                self.assertEqual(
                    PeriodIndex(data=l.get_xdata()).freq, idxh.freq)
                self.assert_numpy_array_equal(l.get_xdata(orig=False),
                                              expected_x)
                expected_y += high[i].values
                self.assert_numpy_array_equal(l.get_ydata(orig=False),
                                              expected_y)

        # high to low
        for kind1, kind2 in [('line', 'area'), ('area', 'line')]:
            ax = high.plot(kind=kind1, stacked=True)
            ax = low.plot(kind=kind2, stacked=True, ax=ax)

            # check high dataframe result
            expected_x = idxh.to_period().asi8.astype(np.float64)
            expected_y = np.zeros(len(expected_x), dtype=np.float64)
            for i in range(3):
                l = ax.lines[i]
                self.assertEqual(
                    PeriodIndex(data=l.get_xdata()).freq, idxh.freq)
                self.assert_numpy_array_equal(l.get_xdata(orig=False),
                                              expected_x)
                expected_y += high[i].values
                self.assert_numpy_array_equal(l.get_ydata(orig=False),
                                              expected_y)

            # check low dataframe result
            expected_x = np.array([
                1514, 1519, 1523, 1527, 1531, 1536, 1540, 1544, 1549, 1553,
                1558, 1562
            ],
                                  dtype=np.float64)
            expected_y = np.zeros(len(expected_x), dtype=np.float64)
            for i in range(3):
                l = ax.lines[3 + i]
                self.assertEqual(
                    PeriodIndex(data=l.get_xdata()).freq, idxh.freq)
                self.assert_numpy_array_equal(l.get_xdata(orig=False),
                                              expected_x)
                expected_y += low[i].values
                self.assert_numpy_array_equal(l.get_ydata(orig=False),
                                              expected_y)
Exemplo n.º 33
0
def _daily_finder(vmin, vmax, freq):
    periodsperday = -1

    if freq >= FreqGroup.FR_HR:
        if freq == FreqGroup.FR_NS:
            periodsperday = 24 * 60 * 60 * 1000000000
        elif freq == FreqGroup.FR_US:
            periodsperday = 24 * 60 * 60 * 1000000
        elif freq == FreqGroup.FR_MS:
            periodsperday = 24 * 60 * 60 * 1000
        elif freq == FreqGroup.FR_SEC:
            periodsperday = 24 * 60 * 60
        elif freq == FreqGroup.FR_MIN:
            periodsperday = 24 * 60
        elif freq == FreqGroup.FR_HR:
            periodsperday = 24
        else:  # pragma: no cover
            raise ValueError("unexpected frequency: {freq}".format(freq=freq))
        periodsperyear = 365 * periodsperday
        periodspermonth = 28 * periodsperday

    elif freq == FreqGroup.FR_BUS:
        periodsperyear = 261
        periodspermonth = 19
    elif freq == FreqGroup.FR_DAY:
        periodsperyear = 365
        periodspermonth = 28
    elif resolution.get_freq_group(freq) == FreqGroup.FR_WK:
        periodsperyear = 52
        periodspermonth = 3
    else:  # pragma: no cover
        raise ValueError("unexpected frequency")

    # save this for later usage
    vmin_orig = vmin

    (vmin, vmax) = (Period(ordinal=int(vmin),
                           freq=freq), Period(ordinal=int(vmax), freq=freq))
    span = vmax.ordinal - vmin.ordinal + 1
    dates_ = PeriodIndex(start=vmin, end=vmax, freq=freq)
    # Initialize the output
    info = np.zeros(span,
                    dtype=[('val', np.int64), ('maj', bool), ('min', bool),
                           ('fmt', '|S20')])
    info['val'][:] = dates_._ndarray_values
    info['fmt'][:] = ''
    info['maj'][[0, -1]] = True
    # .. and set some shortcuts
    info_maj = info['maj']
    info_min = info['min']
    info_fmt = info['fmt']

    def first_label(label_flags):
        if (label_flags[0] == 0) and (label_flags.size > 1) and \
                ((vmin_orig % 1) > 0.0):
            return label_flags[1]
        else:
            return label_flags[0]

    # Case 1. Less than a month
    if span <= periodspermonth:
        day_start = period_break(dates_, 'day')
        month_start = period_break(dates_, 'month')

        def _hour_finder(label_interval, force_year_start):
            _hour = dates_.hour
            _prev_hour = (dates_ - 1).hour
            hour_start = (_hour - _prev_hour) != 0
            info_maj[day_start] = True
            info_min[hour_start & (_hour % label_interval == 0)] = True
            year_start = period_break(dates_, 'year')
            info_fmt[hour_start & (_hour % label_interval == 0)] = '%H:%M'
            info_fmt[day_start] = '%H:%M\n%d-%b'
            info_fmt[year_start] = '%H:%M\n%d-%b\n%Y'
            if force_year_start and not has_level_label(year_start, vmin_orig):
                info_fmt[first_label(day_start)] = '%H:%M\n%d-%b\n%Y'

        def _minute_finder(label_interval):
            hour_start = period_break(dates_, 'hour')
            _minute = dates_.minute
            _prev_minute = (dates_ - 1).minute
            minute_start = (_minute - _prev_minute) != 0
            info_maj[hour_start] = True
            info_min[minute_start & (_minute % label_interval == 0)] = True
            year_start = period_break(dates_, 'year')
            info_fmt = info['fmt']
            info_fmt[minute_start & (_minute % label_interval == 0)] = '%H:%M'
            info_fmt[day_start] = '%H:%M\n%d-%b'
            info_fmt[year_start] = '%H:%M\n%d-%b\n%Y'

        def _second_finder(label_interval):
            minute_start = period_break(dates_, 'minute')
            _second = dates_.second
            _prev_second = (dates_ - 1).second
            second_start = (_second - _prev_second) != 0
            info['maj'][minute_start] = True
            info['min'][second_start & (_second % label_interval == 0)] = True
            year_start = period_break(dates_, 'year')
            info_fmt = info['fmt']
            info_fmt[second_start
                     & (_second % label_interval == 0)] = '%H:%M:%S'
            info_fmt[day_start] = '%H:%M:%S\n%d-%b'
            info_fmt[year_start] = '%H:%M:%S\n%d-%b\n%Y'

        if span < periodsperday / 12000.0:
            _second_finder(1)
        elif span < periodsperday / 6000.0:
            _second_finder(2)
        elif span < periodsperday / 2400.0:
            _second_finder(5)
        elif span < periodsperday / 1200.0:
            _second_finder(10)
        elif span < periodsperday / 800.0:
            _second_finder(15)
        elif span < periodsperday / 400.0:
            _second_finder(30)
        elif span < periodsperday / 150.0:
            _minute_finder(1)
        elif span < periodsperday / 70.0:
            _minute_finder(2)
        elif span < periodsperday / 24.0:
            _minute_finder(5)
        elif span < periodsperday / 12.0:
            _minute_finder(15)
        elif span < periodsperday / 6.0:
            _minute_finder(30)
        elif span < periodsperday / 2.5:
            _hour_finder(1, False)
        elif span < periodsperday / 1.5:
            _hour_finder(2, False)
        elif span < periodsperday * 1.25:
            _hour_finder(3, False)
        elif span < periodsperday * 2.5:
            _hour_finder(6, True)
        elif span < periodsperday * 4:
            _hour_finder(12, True)
        else:
            info_maj[month_start] = True
            info_min[day_start] = True
            year_start = period_break(dates_, 'year')
            info_fmt = info['fmt']
            info_fmt[day_start] = '%d'
            info_fmt[month_start] = '%d\n%b'
            info_fmt[year_start] = '%d\n%b\n%Y'
            if not has_level_label(year_start, vmin_orig):
                if not has_level_label(month_start, vmin_orig):
                    info_fmt[first_label(day_start)] = '%d\n%b\n%Y'
                else:
                    info_fmt[first_label(month_start)] = '%d\n%b\n%Y'

    # Case 2. Less than three months
    elif span <= periodsperyear // 4:
        month_start = period_break(dates_, 'month')
        info_maj[month_start] = True
        if freq < FreqGroup.FR_HR:
            info['min'] = True
        else:
            day_start = period_break(dates_, 'day')
            info['min'][day_start] = True
        week_start = period_break(dates_, 'week')
        year_start = period_break(dates_, 'year')
        info_fmt[week_start] = '%d'
        info_fmt[month_start] = '\n\n%b'
        info_fmt[year_start] = '\n\n%b\n%Y'
        if not has_level_label(year_start, vmin_orig):
            if not has_level_label(month_start, vmin_orig):
                info_fmt[first_label(week_start)] = '\n\n%b\n%Y'
            else:
                info_fmt[first_label(month_start)] = '\n\n%b\n%Y'
    # Case 3. Less than 14 months ...............
    elif span <= 1.15 * periodsperyear:
        year_start = period_break(dates_, 'year')
        month_start = period_break(dates_, 'month')
        week_start = period_break(dates_, 'week')
        info_maj[month_start] = True
        info_min[week_start] = True
        info_min[year_start] = False
        info_min[month_start] = False
        info_fmt[month_start] = '%b'
        info_fmt[year_start] = '%b\n%Y'
        if not has_level_label(year_start, vmin_orig):
            info_fmt[first_label(month_start)] = '%b\n%Y'
    # Case 4. Less than 2.5 years ...............
    elif span <= 2.5 * periodsperyear:
        year_start = period_break(dates_, 'year')
        quarter_start = period_break(dates_, 'quarter')
        month_start = period_break(dates_, 'month')
        info_maj[quarter_start] = True
        info_min[month_start] = True
        info_fmt[quarter_start] = '%b'
        info_fmt[year_start] = '%b\n%Y'
    # Case 4. Less than 4 years .................
    elif span <= 4 * periodsperyear:
        year_start = period_break(dates_, 'year')
        month_start = period_break(dates_, 'month')
        info_maj[year_start] = True
        info_min[month_start] = True
        info_min[year_start] = False

        month_break = dates_[month_start].month
        jan_or_jul = month_start[(month_break == 1) | (month_break == 7)]
        info_fmt[jan_or_jul] = '%b'
        info_fmt[year_start] = '%b\n%Y'
    # Case 5. Less than 11 years ................
    elif span <= 11 * periodsperyear:
        year_start = period_break(dates_, 'year')
        quarter_start = period_break(dates_, 'quarter')
        info_maj[year_start] = True
        info_min[quarter_start] = True
        info_min[year_start] = False
        info_fmt[year_start] = '%Y'
    # Case 6. More than 12 years ................
    else:
        year_start = period_break(dates_, 'year')
        year_break = dates_[year_start].year
        nyears = span / periodsperyear
        (min_anndef, maj_anndef) = _get_default_annual_spacing(nyears)
        major_idx = year_start[(year_break % maj_anndef == 0)]
        info_maj[major_idx] = True
        minor_idx = year_start[(year_break % min_anndef == 0)]
        info_min[minor_idx] = True
        info_fmt[major_idx] = '%Y'

    return info