本文整理汇总了Python中pandas.core.api.Timestamp类的典型用法代码示例。如果您正苦于以下问题:Python Timestamp类的具体用法?Python Timestamp怎么用?Python Timestamp使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Timestamp类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_tz
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")
示例2: test_tz
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')
示例3: test_tz_convert_roundtrip
def test_tz_convert_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, tz="UTC")
converted = ts.tz_convert(tz)
reset = converted.tz_convert(None)
self.assertEqual(reset, Timestamp(t))
self.assertTrue(reset.tzinfo is None)
self.assertEqual(reset, converted.tz_convert("UTC").tz_localize(None))
示例4: test_tz_convert_roundtrip
def test_tz_convert_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, tz='UTC')
converted = ts.tz_convert(tz)
reset = converted.tz_convert(None)
self.assertEqual(reset, Timestamp(t))
self.assertTrue(reset.tzinfo is None)
self.assertEqual(reset, converted.tz_convert('UTC').tz_localize(None))
示例5: test_tz_localize_roundtrip
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)
示例6: test_conversion
def test_conversion(self):
# GH 9255
ts = Timestamp('2000-01-01')
result = ts.to_pydatetime()
expected = datetime.datetime(2000, 1, 1)
self.assertEqual(result, expected)
self.assertEqual(type(result), type(expected))
result = ts.to_datetime64()
expected = np.datetime64(ts.value, 'ns')
self.assertEqual(result, expected)
self.assertEqual(type(result), type(expected))
self.assertEqual(result.dtype, expected.dtype)
示例7: test_tz_localize_roundtrip
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)
示例8: test_now
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)
示例9: test_today
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)
示例10: test_tz_localize_ambiguous
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")
示例11: test_tz
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)
示例12: test_tz_localize_ambiguous
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')