当前位置: 首页>>代码示例>>Python>>正文


Python Timestamp.tz_localize方法代码示例

本文整理汇总了Python中pandas.core.api.Timestamp.tz_localize方法的典型用法代码示例。如果您正苦于以下问题:Python Timestamp.tz_localize方法的具体用法?Python Timestamp.tz_localize怎么用?Python Timestamp.tz_localize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pandas.core.api.Timestamp的用法示例。


在下文中一共展示了Timestamp.tz_localize方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_tz

# 需要导入模块: from pandas.core.api import Timestamp [as 别名]
# 或者: from pandas.core.api.Timestamp import tz_localize [as 别名]
    def test_tz(self):
        t = "2014-02-01 09:00"
        ts = Timestamp(t)
        local = ts.tz_localize("Asia/Tokyo")
        self.assertEqual(local.hour, 9)
        self.assertEqual(local, Timestamp(t, tz="Asia/Tokyo"))
        conv = local.tz_convert("US/Eastern")
        self.assertEqual(conv, Timestamp("2014-01-31 19:00", tz="US/Eastern"))
        self.assertEqual(conv.hour, 19)

        # preserves nanosecond
        ts = Timestamp(t) + offsets.Nano(5)
        local = ts.tz_localize("Asia/Tokyo")
        self.assertEqual(local.hour, 9)
        self.assertEqual(local.nanosecond, 5)
        conv = local.tz_convert("US/Eastern")
        self.assertEqual(conv.nanosecond, 5)
        self.assertEqual(conv.hour, 19)

        # GH 8025
        with tm.assertRaisesRegexp(TypeError, "Cannot localize tz-aware Timestamp, use " "tz_convert for conversions"):
            Timestamp("2011-01-01", tz="US/Eastern").tz_localize("Asia/Tokyo")

        with tm.assertRaisesRegexp(TypeError, "Cannot convert tz-naive Timestamp, use " "tz_localize to localize"):
            Timestamp("2011-01-01").tz_convert("Asia/Tokyo")
开发者ID:bxhunter,项目名称:pandas,代码行数:27,代码来源:test_tslib.py

示例2: test_tz

# 需要导入模块: from pandas.core.api import Timestamp [as 别名]
# 或者: from pandas.core.api.Timestamp import tz_localize [as 别名]
    def test_tz(self):
        t = '2014-02-01 09:00'
        ts = Timestamp(t)
        local = ts.tz_localize('Asia/Tokyo')
        self.assertEqual(local.hour, 9)
        self.assertEqual(local, Timestamp(t, tz='Asia/Tokyo'))
        conv = local.tz_convert('US/Eastern')
        self.assertEqual(conv,
                         Timestamp('2014-01-31 19:00', tz='US/Eastern'))
        self.assertEqual(conv.hour, 19)

        # preserves nanosecond
        ts = Timestamp(t) + offsets.Nano(5)
        local = ts.tz_localize('Asia/Tokyo')
        self.assertEqual(local.hour, 9)
        self.assertEqual(local.nanosecond, 5)
        conv = local.tz_convert('US/Eastern')
        self.assertEqual(conv.nanosecond, 5)
        self.assertEqual(conv.hour, 19)

        # GH 8025
        with tm.assertRaisesRegexp(TypeError, 'Cannot localize tz-aware Timestamp, use '
                                   'tz_convert for conversions'):
            Timestamp('2011-01-01' ,tz='US/Eastern').tz_localize('Asia/Tokyo')

        with tm.assertRaisesRegexp(TypeError, 'Cannot convert tz-naive Timestamp, use '
                            'tz_localize to localize'):
            Timestamp('2011-01-01').tz_convert('Asia/Tokyo')
开发者ID:BorisVerk,项目名称:pandas,代码行数:30,代码来源:test_tslib.py

示例3: test_tz_localize_ambiguous

# 需要导入模块: from pandas.core.api import Timestamp [as 别名]
# 或者: from pandas.core.api.Timestamp import tz_localize [as 别名]
    def test_tz_localize_ambiguous(self):

        ts = Timestamp("2014-11-02 01:00")
        ts_dst = ts.tz_localize("US/Eastern", ambiguous=True)
        ts_no_dst = ts.tz_localize("US/Eastern", ambiguous=False)

        rng = date_range("2014-11-02", periods=3, freq="H", tz="US/Eastern")
        self.assertEqual(rng[1], ts_dst)
        self.assertEqual(rng[2], ts_no_dst)
        self.assertRaises(ValueError, ts.tz_localize, "US/Eastern", ambiguous="infer")

        # GH 8025
        with tm.assertRaisesRegexp(TypeError, "Cannot localize tz-aware Timestamp, use " "tz_convert for conversions"):
            Timestamp("2011-01-01", tz="US/Eastern").tz_localize("Asia/Tokyo")

        with tm.assertRaisesRegexp(TypeError, "Cannot convert tz-naive Timestamp, use " "tz_localize to localize"):
            Timestamp("2011-01-01").tz_convert("Asia/Tokyo")
开发者ID:arvindchari88,项目名称:newGitTest,代码行数:19,代码来源:test_tslib.py

示例4: test_tz

# 需要导入模块: from pandas.core.api import Timestamp [as 别名]
# 或者: from pandas.core.api.Timestamp import tz_localize [as 别名]
    def test_tz(self):
        t = "2014-02-01 09:00"
        ts = Timestamp(t)
        local = ts.tz_localize("Asia/Tokyo")
        self.assertEqual(local.hour, 9)
        self.assertEqual(local, Timestamp(t, tz="Asia/Tokyo"))
        conv = local.tz_convert("US/Eastern")
        self.assertEqual(conv, Timestamp("2014-01-31 19:00", tz="US/Eastern"))
        self.assertEqual(conv.hour, 19)

        # preserves nanosecond
        ts = Timestamp(t) + offsets.Nano(5)
        local = ts.tz_localize("Asia/Tokyo")
        self.assertEqual(local.hour, 9)
        self.assertEqual(local.nanosecond, 5)
        conv = local.tz_convert("US/Eastern")
        self.assertEqual(conv.nanosecond, 5)
        self.assertEqual(conv.hour, 19)
开发者ID:arvindchari88,项目名称:newGitTest,代码行数:20,代码来源:test_tslib.py

示例5: test_tz_localize_ambiguous

# 需要导入模块: from pandas.core.api import Timestamp [as 别名]
# 或者: from pandas.core.api.Timestamp import tz_localize [as 别名]
    def test_tz_localize_ambiguous(self):

        ts = Timestamp('2014-11-02 01:00')
        ts_dst = ts.tz_localize('US/Eastern', ambiguous=True)
        ts_no_dst = ts.tz_localize('US/Eastern', ambiguous=False)

        rng = date_range('2014-11-02', periods=3, freq='H', tz='US/Eastern')
        self.assertEqual(rng[1], ts_dst)
        self.assertEqual(rng[2], ts_no_dst)
        self.assertRaises(ValueError, ts.tz_localize, 'US/Eastern', ambiguous='infer')

        # GH 8025
        with tm.assertRaisesRegexp(TypeError, 'Cannot localize tz-aware Timestamp, use '
                                   'tz_convert for conversions'):
            Timestamp('2011-01-01' ,tz='US/Eastern').tz_localize('Asia/Tokyo')

        with tm.assertRaisesRegexp(TypeError, 'Cannot convert tz-naive Timestamp, use '
                            'tz_localize to localize'):
            Timestamp('2011-01-01').tz_convert('Asia/Tokyo')
开发者ID:amirneto,项目名称:pandas,代码行数:21,代码来源:test_tslib.py

示例6: test_tz_localize_roundtrip

# 需要导入模块: from pandas.core.api import Timestamp [as 别名]
# 或者: from pandas.core.api.Timestamp import tz_localize [as 别名]
    def test_tz_localize_roundtrip(self):
        for tz in ["UTC", "Asia/Tokyo", "US/Eastern", "dateutil/US/Pacific"]:
            for t in ["2014-02-01 09:00", "2014-07-08 09:00", "2014-11-01 17:00", "2014-11-05 00:00"]:
                ts = Timestamp(t)
                localized = ts.tz_localize(tz)
                self.assertEqual(localized, Timestamp(t, tz=tz))

                with tm.assertRaises(TypeError):
                    localized.tz_localize(tz)

                reset = localized.tz_localize(None)
                self.assertEqual(reset, ts)
                self.assertTrue(reset.tzinfo is None)
开发者ID:arvindchari88,项目名称:newGitTest,代码行数:15,代码来源:test_tslib.py

示例7: test_tz_localize_roundtrip

# 需要导入模块: from pandas.core.api import Timestamp [as 别名]
# 或者: from pandas.core.api.Timestamp import tz_localize [as 别名]
    def test_tz_localize_roundtrip(self):
        for tz in ['UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/US/Pacific']:
            for t in ['2014-02-01 09:00', '2014-07-08 09:00', '2014-11-01 17:00',
                      '2014-11-05 00:00']:
                ts = Timestamp(t)
                localized = ts.tz_localize(tz)
                self.assertEqual(localized, Timestamp(t, tz=tz))

                with tm.assertRaises(Exception):
                    localized.tz_localize(tz)

                reset = localized.tz_localize(None)
                self.assertEqual(reset, ts)
                self.assertTrue(reset.tzinfo is None)
开发者ID:Brajen259,项目名称:pandas,代码行数:16,代码来源:test_tslib.py

示例8: test_now

# 需要导入模块: from pandas.core.api import Timestamp [as 别名]
# 或者: from pandas.core.api.Timestamp import tz_localize [as 别名]
    def test_now(self):
        # #9000
        ts_from_string = Timestamp('now')
        ts_from_method = Timestamp.now()
        ts_datetime = datetime.datetime.now()

        ts_from_string_tz = Timestamp('now', tz='US/Eastern')
        ts_from_method_tz = Timestamp.now(tz='US/Eastern')

        # Check that the delta between the times is less than 1s (arbitrarily small)
        delta = Timedelta(seconds=1)
        self.assertTrue((ts_from_method - ts_from_string) < delta)
        self.assertTrue((ts_from_method_tz - ts_from_string_tz) < delta)
        self.assertTrue((ts_from_string_tz.tz_localize(None) - ts_from_string) < delta)
开发者ID:FedericoCeratto,项目名称:pandas,代码行数:16,代码来源:test_tslib.py

示例9: test_today

# 需要导入模块: from pandas.core.api import Timestamp [as 别名]
# 或者: from pandas.core.api.Timestamp import tz_localize [as 别名]
    def test_today(self):

        ts_from_string = Timestamp("today")
        ts_from_method = Timestamp.today()
        ts_datetime = datetime.datetime.today()

        ts_from_string_tz = Timestamp("today", tz="US/Eastern")
        ts_from_method_tz = Timestamp.today(tz="US/Eastern")

        # Check that the delta between the times is less than 1s (arbitrarily small)
        delta = Timedelta(seconds=1)
        self.assertTrue(abs(ts_from_method - ts_from_string) < delta)
        self.assertTrue(abs(ts_datetime - ts_from_method) < delta)
        self.assertTrue(abs(ts_from_method_tz - ts_from_string_tz) < delta)
        self.assertTrue(abs(ts_from_string_tz.tz_localize(None) - ts_from_method_tz.tz_localize(None)) < delta)
开发者ID:arvindchari88,项目名称:newGitTest,代码行数:17,代码来源:test_tslib.py


注:本文中的pandas.core.api.Timestamp.tz_localize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。