Пример #1
0
 def test_cache_keys_are_distinct_for_pytz_vs_dateutil(self):
     tzs = pytz.common_timezones
     for tz_name in tzs:
         if tz_name == "UTC":
             # skip utc as it's a special case in dateutil
             continue
         tz_p = tslib.maybe_get_tz(tz_name)
         tz_d = tslib.maybe_get_tz("dateutil/" + tz_name)
         if tz_d is None:
             # skip timezones that dateutil doesn't know about.
             continue
         self.assertNotEqual(tslib._p_tz_cache_key(tz_p), tslib._p_tz_cache_key(tz_d))
Пример #2
0
    def test_utc_with_system_utc(self):
        from pandas.tslib import maybe_get_tz

        # from system utc to real utc
        ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC'))
        # check that the time hasn't changed.
        self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc()))

        # from system utc to real utc
        ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC'))
        # check that the time hasn't changed.
        self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc()))
Пример #3
0
 def test_cache_keys_are_distinct_for_pytz_vs_dateutil(self):
     tzs = pytz.common_timezones
     for tz_name in tzs:
         if tz_name == 'UTC':
             # skip utc as it's a special case in dateutil
             continue
         tz_p = tslib.maybe_get_tz(tz_name)
         tz_d = tslib.maybe_get_tz('dateutil/' + tz_name)
         if tz_d is None:
             # skip timezones that dateutil doesn't know about.
             continue
         self.assertNotEqual(tslib._p_tz_cache_key(tz_p), tslib._p_tz_cache_key(tz_d))
Пример #4
0
    def test_utc_with_system_utc(self):
        from pandas.tslib import maybe_get_tz

        # from system utc to real utc
        ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC'))
        # check that the time hasn't changed.
        self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc()))

        # from system utc to real utc
        ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC'))
        # check that the time hasn't changed.
        self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc()))
Пример #5
0
    def test_utc_with_system_utc(self):
        if sys.platform == "win32":
            raise nose.SkipTest("Skipped on win32 due to dateutil bug.")

        from pandas.tslib import maybe_get_tz

        # from system utc to real utc
        ts = Timestamp("2001-01-05 11:56", tz=maybe_get_tz("dateutil/UTC"))
        # check that the time hasn't changed.
        self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc()))

        # from system utc to real utc
        ts = Timestamp("2001-01-05 11:56", tz=maybe_get_tz("dateutil/UTC"))
        # check that the time hasn't changed.
        self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc()))
Пример #6
0
    def test_utc_with_system_utc(self):
        if sys.platform == 'win32':
            raise nose.SkipTest('Skipped on win32 due to dateutil bug.')

        from pandas.tslib import maybe_get_tz

        # from system utc to real utc
        ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC'))
        # check that the time hasn't changed.
        self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc()))

        # from system utc to real utc
        ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC'))
        # check that the time hasn't changed.
        self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc()))
Пример #7
0
 def tz(self, tz):
     """
     Construct a dateutil timezone.
     Use tslib.maybe_get_tz so that we get the filename on the tz right
     on windows. See #7337.
     """
     return tslib.maybe_get_tz("dateutil/" + tz)
Пример #8
0
 def tz(self, tz):
     '''
     Construct a dateutil timezone.
     Use tslib.maybe_get_tz so that we get the filename on the tz right
     on windows. See #7337.
     '''
     return tslib.maybe_get_tz('dateutil/' + tz)
Пример #9
0
 def tz(self, tz):
     '''
     Construct a dateutil timezone.
     Use tslib.maybe_get_tz so that we get the filename on the tz right
     on windows. See #7337.
     '''
     return tslib.maybe_get_tz('dateutil/' + tz)
Пример #10
0
    def test_tslib_tz_convert(self):
        def compare_utc_to_local(tz_didx, utc_didx):
            f = lambda x: tslib.tz_convert_single(x, 'UTC', tz_didx.tz)
            result = tslib.tz_convert(tz_didx.asi8, 'UTC', tz_didx.tz)
            result_single = np.vectorize(f)(tz_didx.asi8)
            self.assert_numpy_array_equal(result, result_single)

        def compare_local_to_utc(tz_didx, utc_didx):
            f = lambda x: tslib.tz_convert_single(x, tz_didx.tz, 'UTC')
            result = tslib.tz_convert(utc_didx.asi8, tz_didx.tz, 'UTC')
            result_single = np.vectorize(f)(utc_didx.asi8)
            self.assert_numpy_array_equal(result, result_single)

        for tz in ['UTC', 'Asia/Tokyo', 'US/Eastern', 'Europe/Moscow']:
            # US: 2014-03-09 - 2014-11-11
            # MOSCOW: 2014-10-26  /  2014-12-31
            tz_didx = date_range('2014-03-01', '2015-01-10', freq='H', tz=tz)
            utc_didx = date_range('2014-03-01', '2015-01-10', freq='H')
            compare_utc_to_local(tz_didx, utc_didx)
            # local tz to UTC can be differ in hourly (or higher) freqs because
            # of DST
            compare_local_to_utc(tz_didx, utc_didx)

            tz_didx = date_range('2000-01-01', '2020-01-01', freq='D', tz=tz)
            utc_didx = date_range('2000-01-01', '2020-01-01', freq='D')
            compare_utc_to_local(tz_didx, utc_didx)
            compare_local_to_utc(tz_didx, utc_didx)

            tz_didx = date_range('2000-01-01', '2100-01-01', freq='A', tz=tz)
            utc_didx = date_range('2000-01-01', '2100-01-01', freq='A')
            compare_utc_to_local(tz_didx, utc_didx)
            compare_local_to_utc(tz_didx, utc_didx)

        # Check empty array
        result = tslib.tz_convert(np.array([], dtype=np.int64),
                                  tslib.maybe_get_tz('US/Eastern'),
                                  tslib.maybe_get_tz('Asia/Tokyo'))
        self.assert_numpy_array_equal(result, np.array([], dtype=np.int64))

        # Check all-NaT array
        result = tslib.tz_convert(np.array([tslib.iNaT], dtype=np.int64),
                                  tslib.maybe_get_tz('US/Eastern'),
                                  tslib.maybe_get_tz('Asia/Tokyo'))
        self.assert_numpy_array_equal(result,
                                      np.array([tslib.iNaT], dtype=np.int64))
Пример #11
0
    def test_tslib_tz_convert(self):
        def compare_utc_to_local(tz_didx, utc_didx):
            f = lambda x: tslib.tz_convert_single(x, 'UTC', tz_didx.tz)
            result = tslib.tz_convert(tz_didx.asi8, 'UTC', tz_didx.tz)
            result_single = np.vectorize(f)(tz_didx.asi8)
            self.assert_numpy_array_equal(result, result_single)

        def compare_local_to_utc(tz_didx, utc_didx):
            f = lambda x: tslib.tz_convert_single(x, tz_didx.tz, 'UTC')
            result = tslib.tz_convert(utc_didx.asi8, tz_didx.tz, 'UTC')
            result_single = np.vectorize(f)(utc_didx.asi8)
            self.assert_numpy_array_equal(result, result_single)

        for tz in ['UTC', 'Asia/Tokyo', 'US/Eastern', 'Europe/Moscow']:
            # US: 2014-03-09 - 2014-11-11
            # MOSCOW: 2014-10-26  /  2014-12-31
            tz_didx = date_range('2014-03-01', '2015-01-10', freq='H', tz=tz)
            utc_didx = date_range('2014-03-01', '2015-01-10', freq='H')
            compare_utc_to_local(tz_didx, utc_didx)
            # local tz to UTC can be differ in hourly (or higher) freqs because
            # of DST
            compare_local_to_utc(tz_didx, utc_didx)

            tz_didx = date_range('2000-01-01', '2020-01-01', freq='D', tz=tz)
            utc_didx = date_range('2000-01-01', '2020-01-01', freq='D')
            compare_utc_to_local(tz_didx, utc_didx)
            compare_local_to_utc(tz_didx, utc_didx)

            tz_didx = date_range('2000-01-01', '2100-01-01', freq='A', tz=tz)
            utc_didx = date_range('2000-01-01', '2100-01-01', freq='A')
            compare_utc_to_local(tz_didx, utc_didx)
            compare_local_to_utc(tz_didx, utc_didx)

        # Check empty array
        result = tslib.tz_convert(np.array([], dtype=np.int64),
                                  tslib.maybe_get_tz('US/Eastern'),
                                  tslib.maybe_get_tz('Asia/Tokyo'))
        self.assert_numpy_array_equal(result, np.array([], dtype=np.int64))

        # Check all-NaT array
        result = tslib.tz_convert(np.array([tslib.iNaT], dtype=np.int64),
                                  tslib.maybe_get_tz('US/Eastern'),
                                  tslib.maybe_get_tz('Asia/Tokyo'))
        self.assert_numpy_array_equal(result, np.array(
            [tslib.iNaT], dtype=np.int64))
Пример #12
0
    def test_tslib_tz_convert(self):
        def compare_utc_to_local(tz_didx, utc_didx):
            f = lambda x: tslib.tz_convert_single(x, "UTC", tz_didx.tz)
            result = tslib.tz_convert(tz_didx.asi8, "UTC", tz_didx.tz)
            result_single = np.vectorize(f)(tz_didx.asi8)
            self.assert_numpy_array_equal(result, result_single)

        def compare_local_to_utc(tz_didx, utc_didx):
            f = lambda x: tslib.tz_convert_single(x, tz_didx.tz, "UTC")
            result = tslib.tz_convert(utc_didx.asi8, tz_didx.tz, "UTC")
            result_single = np.vectorize(f)(utc_didx.asi8)
            self.assert_numpy_array_equal(result, result_single)

        for tz in ["UTC", "Asia/Tokyo", "US/Eastern", "Europe/Moscow"]:
            # US: 2014-03-09 - 2014-11-11
            # MOSCOW: 2014-10-26  /  2014-12-31
            tz_didx = date_range("2014-03-01", "2015-01-10", freq="H", tz=tz)
            utc_didx = date_range("2014-03-01", "2015-01-10", freq="H")
            compare_utc_to_local(tz_didx, utc_didx)
            # local tz to UTC can be differ in hourly (or higher) freqs because
            # of DST
            compare_local_to_utc(tz_didx, utc_didx)

            tz_didx = date_range("2000-01-01", "2020-01-01", freq="D", tz=tz)
            utc_didx = date_range("2000-01-01", "2020-01-01", freq="D")
            compare_utc_to_local(tz_didx, utc_didx)
            compare_local_to_utc(tz_didx, utc_didx)

            tz_didx = date_range("2000-01-01", "2100-01-01", freq="A", tz=tz)
            utc_didx = date_range("2000-01-01", "2100-01-01", freq="A")
            compare_utc_to_local(tz_didx, utc_didx)
            compare_local_to_utc(tz_didx, utc_didx)

        # Check empty array
        result = tslib.tz_convert(
            np.array([], dtype=np.int64), tslib.maybe_get_tz("US/Eastern"), tslib.maybe_get_tz("Asia/Tokyo")
        )
        self.assert_numpy_array_equal(result, np.array([], dtype=np.int64))

        # Check all-NaT array
        result = tslib.tz_convert(
            np.array([tslib.iNaT], dtype=np.int64), tslib.maybe_get_tz("US/Eastern"), tslib.maybe_get_tz("Asia/Tokyo")
        )
        self.assert_numpy_array_equal(result, np.array([tslib.iNaT], dtype=np.int64))
Пример #13
0
def _maybe_get_tz(tz, date=None):
    tz = tslib.maybe_get_tz(tz)
    if com.is_integer(tz):
        import pytz
        tz = pytz.FixedOffset(tz / 60)

    # localize and get the tz
    if date is not None and tz is not None:
        if date.tzinfo is not None and hasattr(tz, 'localize'):
            tz = tz.localize(date.replace(tzinfo=None)).tzinfo

    return tz
Пример #14
0
def _maybe_get_tz(tz, date=None):
    tz = tslib.maybe_get_tz(tz)
    if com.is_integer(tz):
        import pytz

        tz = pytz.FixedOffset(tz / 60)

    # localize and get the tz
    if date is not None and tz is not None:
        if date.tzinfo is not None and hasattr(tz, "localize"):
            tz = tz.localize(date.replace(tzinfo=None)).tzinfo

    return tz
    def test_range_tz_dateutil(self):
        # GH 2906
        tm._skip_if_no_dateutil()
        # Use maybe_get_tz to fix filename in tz under dateutil.
        from pandas.tslib import maybe_get_tz
        tz = lambda x: maybe_get_tz('dateutil/' + x)

        start = datetime(2011, 1, 1, tzinfo=tz('US/Eastern'))
        end = datetime(2011, 1, 3, tzinfo=tz('US/Eastern'))

        dr = date_range(start=start, periods=3)
        self.assert_(dr.tz == tz('US/Eastern'))
        self.assert_(dr[0] == start)
        self.assert_(dr[2] == end)

        dr = date_range(end=end, periods=3)
        self.assert_(dr.tz == tz('US/Eastern'))
        self.assert_(dr[0] == start)
        self.assert_(dr[2] == end)

        dr = date_range(start=start, end=end)
        self.assert_(dr.tz == tz('US/Eastern'))
        self.assert_(dr[0] == start)
        self.assert_(dr[2] == end)
Пример #16
0
    def test_range_tz_dateutil(self):
        # GH 2906
        tm._skip_if_no_dateutil()
        # Use maybe_get_tz to fix filename in tz under dateutil.
        from pandas.tslib import maybe_get_tz
        tz = lambda x: maybe_get_tz('dateutil/' + x)

        start = datetime(2011, 1, 1, tzinfo=tz('US/Eastern'))
        end = datetime(2011, 1, 3, tzinfo=tz('US/Eastern'))

        dr = date_range(start=start, periods=3)
        self.assertTrue(dr.tz == tz('US/Eastern'))
        self.assertTrue(dr[0] == start)
        self.assertTrue(dr[2] == end)

        dr = date_range(end=end, periods=3)
        self.assertTrue(dr.tz == tz('US/Eastern'))
        self.assertTrue(dr[0] == start)
        self.assertTrue(dr[2] == end)

        dr = date_range(start=start, end=end)
        self.assertTrue(dr.tz == tz('US/Eastern'))
        self.assertTrue(dr[0] == start)
        self.assertTrue(dr[2] == end)