Ejemplo n.º 1
1
    def test_with_tz(self):
        tz = self.tz("US/Central")

        # just want it to work
        start = datetime(2011, 3, 12, tzinfo=pytz.utc)
        dr = bdate_range(start, periods=50, freq=datetools.Hour())
        self.assertIs(dr.tz, pytz.utc)

        # DateRange with naive datetimes
        dr = bdate_range("1/1/2005", "1/1/2009", tz=pytz.utc)
        dr = bdate_range("1/1/2005", "1/1/2009", tz=tz)

        # normalized
        central = dr.tz_convert(tz)
        self.assertIs(central.tz, tz)
        comp = self.localize(tz, central[0].to_pydatetime().replace(tzinfo=None)).tzinfo
        self.assertIs(central[0].tz, comp)

        # compare vs a localized tz
        comp = self.localize(tz, dr[0].to_pydatetime().replace(tzinfo=None)).tzinfo
        self.assertIs(central[0].tz, comp)

        # datetimes with tzinfo set
        dr = bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc), "1/1/2009", tz=pytz.utc)

        self.assertRaises(Exception, bdate_range, datetime(2005, 1, 1, tzinfo=pytz.utc), "1/1/2009", tz=tz)
Ejemplo n.º 2
0
 def test_summary(self):
     self.rng.summary()
     self.rng[2:2].summary()
     try:
         bdate_range('1/1/2005', '1/1/2009', tz=pytz.utc).summary()
     except Exception:
         pass
Ejemplo n.º 3
0
    def test_with_tz(self):
        _skip_if_no_pytz()
        tz = pytz.timezone('US/Central')

        # just want it to work
        start = datetime(2011, 3, 12, tzinfo=pytz.utc)
        dr = bdate_range(start, periods=50, freq=datetools.Hour())
        self.assert_(dr.tz is not None)
        self.assert_(dr.tz is start.tzinfo)

        # DateRange with naive datetimes
        dr = bdate_range('1/1/2005', '1/1/2009', tz=pytz.utc)
        dr = bdate_range('1/1/2005', '1/1/2009', tz=tz)

        # normalized
        central = dr.tz_normalize(tz)
        self.assert_(central.tz is tz)
        self.assert_(central[0].tz is tz)

        # datetimes with tzinfo set
        dr = bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc),
                         '1/1/2009', tz=pytz.utc)

        self.assertRaises(Exception, bdate_range,
                          datetime(2005, 1, 1, tzinfo=pytz.utc),
                          '1/1/2009', tz=tz)
Ejemplo n.º 4
0
    def test_daterange_bug_456(self):
        # GH #456
        rng1 = bdate_range('12/5/2011', '12/5/2011')
        rng2 = bdate_range('12/2/2011', '12/5/2011')
        rng2.offset = datetools.BDay()

        result = rng1.union(rng2)
        self.assert_(isinstance(result, DatetimeIndex))
Ejemplo n.º 5
0
 def test_naive_aware_conflicts(self):
     naive = bdate_range(START, END, freq=BDay(), tz=None)
     aware = bdate_range(START, END, freq=BDay(),
                         tz="Asia/Hong_Kong")
     self.assertRaisesRegexp(TypeError, "tz-naive.*tz-aware",
                             naive.join, aware)
     self.assertRaisesRegexp(TypeError, "tz-naive.*tz-aware",
                             aware.join, naive)
Ejemplo n.º 6
0
    def test_with_tz_ambiguous_times(self):
        tz = pytz.timezone('US/Eastern')

        rng = bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1))

        # regular no problem
        self.assert_(rng.tz_validate())

        # March 13, 2011, spring forward, skip from 2 AM to 3 AM
        dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3,
                        freq=datetools.Hour())
        self.assertRaises(pytz.AmbiguousTimeError, dr.tz_localize, tz)

        # after dst transition, it works
        dr = date_range(datetime(2011, 3, 13, 3, 30), periods=3,
                        freq=datetools.Hour(), tz=tz)

        # November 6, 2011, fall back, repeat 2 AM hour
        dr = date_range(datetime(2011, 11, 6, 1, 30), periods=3,
                        freq=datetools.Hour())
        self.assertRaises(pytz.AmbiguousTimeError, dr.tz_localize, tz)

        # UTC is OK
        dr = date_range(datetime(2011, 3, 13), periods=48,
                        freq=datetools.Minute(30), tz=pytz.utc)
Ejemplo n.º 7
0
    def test_misc(self):
        end = datetime(2009, 5, 13)
        dr = bdate_range(end=end, periods=20)
        firstDate = end - 19 * datetools.bday

        assert len(dr) == 20
        assert dr[0] == firstDate
        assert dr[-1] == end
Ejemplo n.º 8
0
    def test_with_tz(self):
        tz = pytz.timezone("US/Central")

        # just want it to work
        start = datetime(2011, 3, 12, tzinfo=pytz.utc)
        dr = bdate_range(start, periods=50, freq=datetools.Hour())
        self.assert_(dr.tz is pytz.utc)

        # DateRange with naive datetimes
        dr = bdate_range("1/1/2005", "1/1/2009", tz=pytz.utc)
        dr = bdate_range("1/1/2005", "1/1/2009", tz=tz)

        # normalized
        central = dr.tz_convert(tz)
        self.assert_(central.tz is tz)
        self.assert_(central[0].tz is tz)

        # datetimes with tzinfo set
        dr = bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc), "1/1/2009", tz=pytz.utc)

        self.assertRaises(Exception, bdate_range, datetime(2005, 1, 1, tzinfo=pytz.utc), "1/1/2009", tz=tz)
Ejemplo n.º 9
0
    def hVar(self, market, model, cob_date, days=262):
        """Calculate the historical var for this fund on the given date.

        :type market: holders.BaseHolder market data
        :type model: simple_files.FileHolder model data for use by pricers
        :type cob_date: str cob date to calculate var for
        :type days: int number of business days to look back for the historical market data, defaults to 262 which works
                    by pandas logic
        :return:
        """
        dates = bdate_range(end=cob_date, periods=days).to_series()
        log.info("Calculating hVar using date range [%s:%s]", dates[0].date(), dates[-1].date())
        prices = self.price_for_dates(market, model, dates)
        return self.price(market, model, cob_date) - prices.dropna().quantile(.05)
Ejemplo n.º 10
0
 def test_finder_daily(self):
     xp = Period('1999-1-1', freq='B').ordinal
     day_lst = [10, 40, 252, 400, 950, 2750, 10000]
     for n in day_lst:
         rng = bdate_range('1999-1-1', periods=n)
         ser = Series(np.random.randn(len(rng)), rng)
         ax = ser.plot()
         xaxis = ax.get_xaxis()
         rs = xaxis.get_majorticklocs()[0]
         self.assertEqual(xp, rs)
         (vmin, vmax) = ax.get_xlim()
         ax.set_xlim(vmin + 0.9, vmax)
         rs = xaxis.get_majorticklocs()[0]
         self.assertEqual(xp, rs)
Ejemplo n.º 11
0
    def test_finder_daily(self):
        import matplotlib.pyplot as plt

        xp = Period("1999-1-1", freq="B").ordinal
        day_lst = [10, 40, 252, 400, 950, 2750, 10000]
        for n in day_lst:
            rng = bdate_range("1999-1-1", periods=n)
            ser = Series(np.random.randn(len(rng)), rng)
            ax = ser.plot()
            xaxis = ax.get_xaxis()
            rs = xaxis.get_majorticklocs()[0]
            self.assertEqual(xp, rs)
            vmin, vmax = ax.get_xlim()
            ax.set_xlim(vmin + 0.9, vmax)
            rs = xaxis.get_majorticklocs()[0]
            self.assertEqual(xp, rs)
            plt.close(ax.get_figure())
Ejemplo n.º 12
0
 def setUp(self):
     self.rng = bdate_range(START, END)
Ejemplo n.º 13
0
 def test_constructor(self):
     bdate_range(START, END, freq=BDay())
     bdate_range(START, periods=20, freq=BDay())
     bdate_range(end=START, periods=20, freq=BDay())
     self.assertRaises(ValueError, date_range, '2011-1-1', '2012-1-1', 'B')
     self.assertRaises(ValueError, bdate_range, '2011-1-1', '2012-1-1', 'B')
Ejemplo n.º 14
0
 def test_summary_pytz(self):
     _skip_if_no_pytz()
     import pytz
     bdate_range('1/1/2005', '1/1/2009', tz=pytz.utc).summary()
Ejemplo n.º 15
0
 def test_intersection_bug(self):
     # GH #771
     a = bdate_range('11/30/2011', '12/31/2011')
     b = bdate_range('12/10/2011', '12/20/2011')
     result = a.intersection(b)
     self.assert_(result.equals(b))
Ejemplo n.º 16
0
 def test_summary_dateutil(self):
     tm._skip_if_no_dateutil()
     import dateutil
     bdate_range('1/1/2005', '1/1/2009', tz=dateutil.tz.tzutc()).summary()
Ejemplo n.º 17
0
 def test_tz_localize(self):
     dr = bdate_range("1/1/2009", "1/1/2010")
     dr_utc = bdate_range("1/1/2009", "1/1/2010", tz=pytz.utc)
     localized = dr.tz_localize(pytz.utc)
     self.assert_numpy_array_equal(dr_utc, localized)
Ejemplo n.º 18
0
 def test_summary_dateutil(self):
     _skip_if_no_dateutil()
     import dateutil
     bdate_range('1/1/2005', '1/1/2009', tz=dateutil.tz.gettz('UTC')).summary()
Ejemplo n.º 19
0
 def test_tz_localize(self):
     dr = bdate_range('1/1/2009', '1/1/2010')
     dr_utc = bdate_range('1/1/2009', '1/1/2010', tz=pytz.utc)
     localized = dr.tz_localize(pytz.utc)
     self.assert_(np.array_equal(dr_utc, localized))
Ejemplo n.º 20
0
 def test_intersection_bug(self):
     # GH #771
     a = bdate_range('11/30/2011', '12/31/2011')
     b = bdate_range('12/10/2011', '12/20/2011')
     result = a.intersection(b)
     self.assertTrue(result.equals(b))
Ejemplo n.º 21
0
 def test_constructor(self):
     bdate_range(START, END, freq=BDay())
     bdate_range(START, periods=20, freq=BDay())
     bdate_range(end=START, periods=20, freq=BDay())
     self.assertRaises(ValueError, date_range, '2011-1-1', '2012-1-1', 'B')
     self.assertRaises(ValueError, bdate_range, '2011-1-1', '2012-1-1', 'B')
Ejemplo n.º 22
0
 def test_constructor(self):
     rng = bdate_range(START, END, freq=datetools.bday)
     rng = bdate_range(START, periods=20, freq=datetools.bday)
     rng = bdate_range(end=START, periods=20, freq=datetools.bday)
Ejemplo n.º 23
0
 def setUp(self):
     self.rng = bdate_range(START, END)
Ejemplo n.º 24
0
 def test_constructor(self):
     rng = bdate_range(START, END, freq=datetools.bday)
     rng = bdate_range(START, periods=20, freq=datetools.bday)
     rng = bdate_range(end=START, periods=20, freq=datetools.bday)
     self.assertRaises(ValueError, date_range, '2011-1-1', '2012-1-1', 'B')
     self.assertRaises(ValueError, bdate_range, '2011-1-1', '2012-1-1', 'B')
Ejemplo n.º 25
0
 def test_naive_aware_conflicts(self):
     naive = bdate_range(START, END, freq=BDay(), tz=None)
     aware = bdate_range(START, END, freq=BDay(), tz="Asia/Hong_Kong")
     assertRaisesRegexp(TypeError, "tz-naive.*tz-aware", naive.join, aware)
     assertRaisesRegexp(TypeError, "tz-naive.*tz-aware", aware.join, naive)
Ejemplo n.º 26
0
 def test_constructor(self):
     rng = bdate_range(START, END, freq=datetools.bday)
     rng = bdate_range(START, periods=20, freq=datetools.bday)
     rng = bdate_range(end=START, periods=20, freq=datetools.bday)
     self.assertRaises(ValueError, date_range, '2011-1-1', '2012-1-1', 'B')
     self.assertRaises(ValueError, bdate_range, '2011-1-1', '2012-1-1', 'B')
 def test_summary_dateutil(self):
     tm._skip_if_no_dateutil()
     import dateutil
     bdate_range('1/1/2005', '1/1/2009', tz=dateutil.tz.tzutc()).summary()
Ejemplo n.º 28
0
 def test_tz_localize(self):
     dr = bdate_range('1/1/2009', '1/1/2010')
     dr_utc = bdate_range('1/1/2009', '1/1/2010', tz=pytz.utc)
     localized = dr.tz_localize(pytz.utc)
     self.assert_(np.array_equal(dr_utc, localized))
Ejemplo n.º 29
0
 def test_summary_pytz(self):
     tm._skip_if_no_pytz()
     import pytz
     bdate_range('1/1/2005', '1/1/2009', tz=pytz.utc).summary()