class TimestampOps(object): goal_time = 0.2 def setup(self): self.ts = Timestamp('2017-08-25 08:16:14') self.ts_tz = Timestamp('2017-08-25 08:16:14', tz='US/Eastern') dt = datetime.datetime(2016, 3, 27, 1) self.tzinfo = pytz.timezone('CET').localize(dt, is_dst=False).tzinfo self.ts2 = Timestamp(dt) def time_replace_tz(self): self.ts.replace(tzinfo=pytz.timezone('US/Eastern')) def time_replace_across_dst(self): self.ts2.replace(tzinfo=self.tzinfo) def time_replace_None(self): self.ts_tz.replace(tzinfo=None) def time_to_pydatetime(self): self.ts.to_pydatetime() def time_to_pydatetime_tz(self): self.ts_tz.to_pydatetime()
def test_cant_compare_tz_naive_w_aware(self, utc_fixture): # see GH#1404 a = Timestamp('3/12/2012') b = Timestamp('3/12/2012', tz=utc_fixture) with pytest.raises(TypeError): a == b with pytest.raises(TypeError): a != b with pytest.raises(TypeError): a < b with pytest.raises(TypeError): a <= b with pytest.raises(TypeError): a > b with pytest.raises(TypeError): a >= b with pytest.raises(TypeError): b == a with pytest.raises(TypeError): b != a with pytest.raises(TypeError): b < a with pytest.raises(TypeError): b <= a with pytest.raises(TypeError): b > a with pytest.raises(TypeError): b >= a assert not a == b.to_pydatetime() assert not a.to_pydatetime() == b
def test_datetimeindex_sub_timestamp_overflow(self): dtimax = pd.to_datetime(['now', pd.Timestamp.max]) dtimin = pd.to_datetime(['now', pd.Timestamp.min]) tsneg = Timestamp('1950-01-01') ts_neg_variants = [tsneg, tsneg.to_pydatetime(), tsneg.to_datetime64().astype('datetime64[ns]'), tsneg.to_datetime64().astype('datetime64[D]')] tspos = Timestamp('1980-01-01') ts_pos_variants = [tspos, tspos.to_pydatetime(), tspos.to_datetime64().astype('datetime64[ns]'), tspos.to_datetime64().astype('datetime64[D]')] for variant in ts_neg_variants: with pytest.raises(OverflowError): dtimax - variant expected = pd.Timestamp.max.value - tspos.value for variant in ts_pos_variants: res = dtimax - variant assert res[1].value == expected expected = pd.Timestamp.min.value - tsneg.value for variant in ts_neg_variants: res = dtimin - variant assert res[1].value == expected for variant in ts_pos_variants: with pytest.raises(OverflowError): dtimin - variant
def test_replace_tzinfo(self): # GH#15683 dt = datetime(2016, 3, 27, 1) tzinfo = pytz.timezone('CET').localize(dt, is_dst=False).tzinfo result_dt = dt.replace(tzinfo=tzinfo) result_pd = Timestamp(dt).replace(tzinfo=tzinfo) if PY3: # datetime.timestamp() converts in the local timezone with tm.set_timezone('UTC'): assert result_dt.timestamp() == result_pd.timestamp() assert result_dt == result_pd assert result_dt == result_pd.to_pydatetime() result_dt = dt.replace(tzinfo=tzinfo).replace(tzinfo=None) result_pd = Timestamp(dt).replace(tzinfo=tzinfo).replace(tzinfo=None) if PY3: # datetime.timestamp() converts in the local timezone with tm.set_timezone('UTC'): assert result_dt.timestamp() == result_pd.timestamp() assert result_dt == result_pd assert result_dt == result_pd.to_pydatetime()
class TimestampOps(object): params = [None, 'US/Eastern', pytz.UTC, dateutil.tz.tzutc()] param_names = ['tz'] def setup(self, tz): self.ts = Timestamp('2017-08-25 08:16:14', tz=tz) def time_replace_tz(self, tz): self.ts.replace(tzinfo=pytz.timezone('US/Eastern')) def time_replace_None(self, tz): self.ts.replace(tzinfo=None) def time_to_pydatetime(self, tz): self.ts.to_pydatetime() def time_normalize(self, tz): self.ts.normalize() def time_tz_convert(self, tz): if self.ts.tz is not None: self.ts.tz_convert(tz) def time_tz_localize(self, tz): if self.ts.tz is None: self.ts.tz_localize(tz)
class TimestampOps(object): params = [None, 'US/Eastern'] param_names = ['tz'] def setup(self, tz): self.ts = Timestamp('2017-08-25 08:16:14', tz=tz) def time_replace_tz(self, tz): self.ts.replace(tzinfo=pytz.timezone('US/Eastern')) def time_replace_None(self, tz): self.ts.replace(tzinfo=None) def time_to_pydatetime(self, tz): self.ts.to_pydatetime()
def test_tz_is_utc(self): exp = '"2013-01-10T05:00:00.000Z"' ts = Timestamp('2013-01-10 05:00:00Z') self.assertEqual(exp, pd.json.dumps(ts, iso_dates=True)) dt = ts.to_pydatetime() self.assertEqual(exp, pd.json.dumps(dt, iso_dates=True)) ts = Timestamp('2013-01-10 00:00:00', tz='US/Eastern') self.assertEqual(exp, pd.json.dumps(ts, iso_dates=True)) dt = ts.to_pydatetime() self.assertEqual(exp, pd.json.dumps(dt, iso_dates=True)) ts = Timestamp('2013-01-10 00:00:00-0500') self.assertEqual(exp, pd.json.dumps(ts, iso_dates=True)) dt = ts.to_pydatetime() self.assertEqual(exp, pd.json.dumps(dt, iso_dates=True))
def test_to_pydatetime_nonzero_nano(self): ts = Timestamp('2011-01-01 9:00:00.123456789') # Warn the user of data loss (nanoseconds). with tm.assert_produces_warning(UserWarning, check_stacklevel=False): expected = datetime(2011, 1, 1, 9, 0, 0, 123456) result = ts.to_pydatetime() assert result == expected
def test_tz_is_utc(self): from pandas.io.json import dumps exp = '"2013-01-10T05:00:00.000Z"' ts = Timestamp('2013-01-10 05:00:00Z') assert dumps(ts, iso_dates=True) == exp dt = ts.to_pydatetime() assert dumps(dt, iso_dates=True) == exp ts = Timestamp('2013-01-10 00:00:00', tz='US/Eastern') assert dumps(ts, iso_dates=True) == exp dt = ts.to_pydatetime() assert dumps(dt, iso_dates=True) == exp ts = Timestamp('2013-01-10 00:00:00-0500') assert dumps(ts, iso_dates=True) == exp dt = ts.to_pydatetime() assert dumps(dt, iso_dates=True) == exp
def test_timedeltaindex_add_timestamp_nat_masking(self): # GH17991 checking for overflow-masking with NaT tdinat = pd.to_timedelta(['24658 days 11:15:00', 'NaT']) tsneg = Timestamp('1950-01-01') ts_neg_variants = [tsneg, tsneg.to_pydatetime(), tsneg.to_datetime64().astype('datetime64[ns]'), tsneg.to_datetime64().astype('datetime64[D]')] tspos = Timestamp('1980-01-01') ts_pos_variants = [tspos, tspos.to_pydatetime(), tspos.to_datetime64().astype('datetime64[ns]'), tspos.to_datetime64().astype('datetime64[D]')] for variant in ts_neg_variants + ts_pos_variants: res = tdinat + variant assert res[1] is pd.NaT
def test_cant_compare_tz_naive_w_aware(self): # see gh-1404 a = Timestamp('3/12/2012') b = Timestamp('3/12/2012', tz='utc') pytest.raises(Exception, a.__eq__, b) pytest.raises(Exception, a.__ne__, b) pytest.raises(Exception, a.__lt__, b) pytest.raises(Exception, a.__gt__, b) pytest.raises(Exception, b.__eq__, a) pytest.raises(Exception, b.__ne__, a) pytest.raises(Exception, b.__lt__, a) pytest.raises(Exception, b.__gt__, a) if sys.version_info < (3, 3): pytest.raises(Exception, a.__eq__, b.to_pydatetime()) pytest.raises(Exception, a.to_pydatetime().__eq__, b) else: assert not a == b.to_pydatetime() assert not a.to_pydatetime() == b
def test_cant_compare_tz_naive_w_aware_dateutil(self): # see gh-1404 a = Timestamp('3/12/2012') b = Timestamp('3/12/2012', tz=tzutc()) pytest.raises(Exception, a.__eq__, b) pytest.raises(Exception, a.__ne__, b) pytest.raises(Exception, a.__lt__, b) pytest.raises(Exception, a.__gt__, b) pytest.raises(Exception, b.__eq__, a) pytest.raises(Exception, b.__ne__, a) pytest.raises(Exception, b.__lt__, a) pytest.raises(Exception, b.__gt__, a) if PY2: pytest.raises(Exception, a.__eq__, b.to_pydatetime()) pytest.raises(Exception, a.to_pydatetime().__eq__, b) else: assert not a == b.to_pydatetime() assert not a.to_pydatetime() == b
def test_conversion(self): # GH#9255 ts = Timestamp('2000-01-01') result = ts.to_pydatetime() expected = datetime(2000, 1, 1) assert result == expected assert type(result) == type(expected) result = ts.to_datetime64() expected = np.datetime64(ts.value, 'ns') assert result == expected assert type(result) == type(expected) assert result.dtype == expected.dtype
def test_constructor_coverage(self): # float value for periods expected = pd.interval_range(start=0, periods=10) result = pd.interval_range(start=0, periods=10.5) tm.assert_index_equal(result, expected) # equivalent timestamp-like start/end start, end = Timestamp('2017-01-01'), Timestamp('2017-01-15') expected = pd.interval_range(start=start, end=end) result = pd.interval_range(start=start.to_pydatetime(), end=end.to_pydatetime()) tm.assert_index_equal(result, expected) result = pd.interval_range(start=start.tz_localize('UTC'), end=end.tz_localize('UTC')) tm.assert_index_equal(result, expected) result = pd.interval_range(start=start.asm8, end=end.asm8) tm.assert_index_equal(result, expected) # equivalent freq with timestamp equiv_freq = ['D', Day(), Timedelta(days=1), timedelta(days=1), DateOffset(days=1)] for freq in equiv_freq: result = pd.interval_range(start=start, end=end, freq=freq) tm.assert_index_equal(result, expected) # equivalent timedelta-like start/end start, end = Timedelta(days=1), Timedelta(days=10) expected = pd.interval_range(start=start, end=end) result = pd.interval_range(start=start.to_pytimedelta(), end=end.to_pytimedelta()) tm.assert_index_equal(result, expected) result = pd.interval_range(start=start.asm8, end=end.asm8) tm.assert_index_equal(result, expected) # equivalent freq with timedelta equiv_freq = ['D', Day(), Timedelta(days=1), timedelta(days=1)] for freq in equiv_freq: result = pd.interval_range(start=start, end=end, freq=freq) tm.assert_index_equal(result, expected)
def test_constructor_fromordinal(self): base = datetime(2000, 1, 1) ts = Timestamp.fromordinal(base.toordinal(), freq='D') assert base == ts assert ts.freq == 'D' assert base.toordinal() == ts.toordinal() ts = Timestamp.fromordinal(base.toordinal(), tz='US/Eastern') assert Timestamp('2000-01-01', tz='US/Eastern') == ts assert base.toordinal() == ts.toordinal() # GH#3042 dt = datetime(2011, 4, 16, 0, 0) ts = Timestamp.fromordinal(dt.toordinal()) assert ts.to_pydatetime() == dt # with a tzinfo stamp = Timestamp('2011-4-16', tz='US/Eastern') dt_tz = stamp.to_pydatetime() ts = Timestamp.fromordinal(dt_tz.toordinal(), tz='US/Eastern') assert ts.to_pydatetime() == dt_tz
def _pandas_ts_to_int(ts: pd.Timestamp) -> int: # pyre-ignore[11] """Convert int timestamp into pandas timestamp.""" return ts.to_pydatetime().timestamp()
def test_timestamp_to_datetime_dateutil(self): stamp = Timestamp('20090415', tz='dateutil/US/Eastern', freq='D') dtval = stamp.to_pydatetime() assert stamp == dtval assert stamp.tzinfo == dtval.tzinfo
def test_subtraction_ops_with_tz(self): # check that dt/dti subtraction ops with tz are validated dti = date_range('20130101', periods=3) ts = Timestamp('20130101') dt = ts.to_pydatetime() dti_tz = date_range('20130101', periods=3).tz_localize('US/Eastern') ts_tz = Timestamp('20130101').tz_localize('US/Eastern') ts_tz2 = Timestamp('20130101').tz_localize('CET') dt_tz = ts_tz.to_pydatetime() td = Timedelta('1 days') def _check(result, expected): self.assertEqual(result, expected) self.assertIsInstance(result, Timedelta) # scalars result = ts - ts expected = Timedelta('0 days') _check(result, expected) result = dt_tz - ts_tz expected = Timedelta('0 days') _check(result, expected) result = ts_tz - dt_tz expected = Timedelta('0 days') _check(result, expected) # tz mismatches self.assertRaises(TypeError, lambda: dt_tz - ts) self.assertRaises(TypeError, lambda: dt_tz - dt) self.assertRaises(TypeError, lambda: dt_tz - ts_tz2) self.assertRaises(TypeError, lambda: dt - dt_tz) self.assertRaises(TypeError, lambda: ts - dt_tz) self.assertRaises(TypeError, lambda: ts_tz2 - ts) self.assertRaises(TypeError, lambda: ts_tz2 - dt) self.assertRaises(TypeError, lambda: ts_tz - ts_tz2) # with dti self.assertRaises(TypeError, lambda: dti - ts_tz) self.assertRaises(TypeError, lambda: dti_tz - ts) self.assertRaises(TypeError, lambda: dti_tz - ts_tz2) result = dti_tz - dt_tz expected = TimedeltaIndex(['0 days', '1 days', '2 days']) tm.assert_index_equal(result, expected) result = dt_tz - dti_tz expected = TimedeltaIndex(['0 days', '-1 days', '-2 days']) tm.assert_index_equal(result, expected) result = dti_tz - ts_tz expected = TimedeltaIndex(['0 days', '1 days', '2 days']) tm.assert_index_equal(result, expected) result = ts_tz - dti_tz expected = TimedeltaIndex(['0 days', '-1 days', '-2 days']) tm.assert_index_equal(result, expected) result = td - td expected = Timedelta('0 days') _check(result, expected) result = dti_tz - td expected = DatetimeIndex( ['20121231', '20130101', '20130102'], tz='US/Eastern') tm.assert_index_equal(result, expected)
def test_subtraction_ops_with_tz(self): # check that dt/dti subtraction ops with tz are validated dti = date_range('20130101', periods=3) ts = Timestamp('20130101') dt = ts.to_pydatetime() dti_tz = date_range('20130101', periods=3).tz_localize('US/Eastern') ts_tz = Timestamp('20130101').tz_localize('US/Eastern') ts_tz2 = Timestamp('20130101').tz_localize('CET') dt_tz = ts_tz.to_pydatetime() td = Timedelta('1 days') def _check(result, expected): assert result == expected assert isinstance(result, Timedelta) # scalars result = ts - ts expected = Timedelta('0 days') _check(result, expected) result = dt_tz - ts_tz expected = Timedelta('0 days') _check(result, expected) result = ts_tz - dt_tz expected = Timedelta('0 days') _check(result, expected) # tz mismatches pytest.raises(TypeError, lambda: dt_tz - ts) pytest.raises(TypeError, lambda: dt_tz - dt) pytest.raises(TypeError, lambda: dt_tz - ts_tz2) pytest.raises(TypeError, lambda: dt - dt_tz) pytest.raises(TypeError, lambda: ts - dt_tz) pytest.raises(TypeError, lambda: ts_tz2 - ts) pytest.raises(TypeError, lambda: ts_tz2 - dt) pytest.raises(TypeError, lambda: ts_tz - ts_tz2) # with dti pytest.raises(TypeError, lambda: dti - ts_tz) pytest.raises(TypeError, lambda: dti_tz - ts) pytest.raises(TypeError, lambda: dti_tz - ts_tz2) result = dti_tz - dt_tz expected = TimedeltaIndex(['0 days', '1 days', '2 days']) tm.assert_index_equal(result, expected) result = dt_tz - dti_tz expected = TimedeltaIndex(['0 days', '-1 days', '-2 days']) tm.assert_index_equal(result, expected) result = dti_tz - ts_tz expected = TimedeltaIndex(['0 days', '1 days', '2 days']) tm.assert_index_equal(result, expected) result = ts_tz - dti_tz expected = TimedeltaIndex(['0 days', '-1 days', '-2 days']) tm.assert_index_equal(result, expected) result = td - td expected = Timedelta('0 days') _check(result, expected) result = dti_tz - td expected = DatetimeIndex(['20121231', '20130101', '20130102'], tz='US/Eastern') tm.assert_index_equal(result, expected)
def ts_to_dt(ts: Timestamp) -> datetime: return ts.to_pydatetime()
def test_timestamp_to_datetime_explicit_pytz(self): stamp = Timestamp("20090415", tz=pytz.timezone("US/Eastern"), freq="D") dtval = stamp.to_pydatetime() assert stamp == dtval assert stamp.tzinfo == dtval.tzinfo
class TickData(BaseData): """ Tick data contains information about: * last trade in market * orderbook snapshot * intraday market statistics. """ symbol: str = "" exchange: Exchange = Exchange.SHFE datetime: datetime = datetime(2019, 1, 1) name: str = "" volume: float = 0 last_price: float = 0 last_volume: float = 0 limit_up: float = 0 limit_down: float = 0 open_price: float = 0 high_price: float = 0 low_price: float = 0 pre_close: float = 0 bid_price_1: float = 0 bid_price_2: float = 0 bid_price_3: float = 0 bid_price_4: float = 0 bid_price_5: float = 0 ask_price_1: float = 0 ask_price_2: float = 0 ask_price_3: float = 0 ask_price_4: float = 0 ask_price_5: float = 0 bid_volume_1: float = 0 bid_volume_2: float = 0 bid_volume_3: float = 0 bid_volume_4: float = 0 bid_volume_5: float = 0 ask_volume_1: float = 0 ask_volume_2: float = 0 ask_volume_3: float = 0 ask_volume_4: float = 0 ask_volume_5: float = 0 # StarQuant unique field depth: int = 0 open_interest: float = 0 def __post_init__(self): """""" self.vt_symbol = f"{self.symbol}.{self.exchange.value}" self.timestamp = Timestamp(self.datetime) self.full_symbol = generate_full_symbol(self.exchange, self.symbol) def deserialize(self, msg: str): try: v = msg.split('|') self.full_symbol = v[0] self.timestamp = pd.to_datetime(v[1]) self.datetime = self.timestamp.to_pydatetime() self.symbol, self.exchange = extract_full_symbol(self.full_symbol) self.vt_symbol = generate_vt_symbol(self.symbol, self.exchange) self.last_price = float(v[2]) self.volume = int(v[3]) if (len(v) < 17): self.depth = 1 self.bid_price_1 = float(v[4]) self.bid_volume_1 = int(v[5]) self.ask_price_1 = float(v[6]) self.ask_volume_1 = int(v[7]) self.open_interest = int(v[8]) self.open_price = float(v[9]) self.high_price = float(v[10]) self.low_price = float(v[11]) self.pre_close = float(v[12]) self.limit_up = float(v[13]) self.limit_down = float(v[14]) else: self.depth = 5 self.bid_price_1 = float(v[4]) self.bid_volume_1 = int(v[5]) self.ask_price_1 = float(v[6]) self.ask_volume_1 = int(v[7]) self.bid_price_2 = float(v[8]) self.bid_volume_2 = int(v[9]) self.ask_price_2 = float(v[10]) self.ask_volume_2 = int(v[11]) self.bid_price_3 = float(v[12]) self.bid_volume_3 = int(v[13]) self.ask_price_3 = float(v[14]) self.ask_volume_3 = int(v[15]) self.bid_price_4 = float(v[16]) self.bid_volume_4 = int(v[17]) self.ask_price_4 = float(v[18]) self.ask_volume_4 = int(v[19]) self.bid_price_5 = float(v[20]) self.bid_volume_5 = int(v[21]) self.ask_price_5 = float(v[22]) self.ask_volume_5 = int(v[23]) self.open_interest = int(v[24]) self.open_price = float(v[25]) self.high_price = float(v[26]) self.low_price = float(v[27]) self.pre_close = float(v[28]) self.limit_up = float(v[29]) self.limit_down = float(v[30]) except Exception as e: print(e) pass
def test_timestamp_to_datetime_dateutil(self): stamp = Timestamp("20090415", tz="dateutil/US/Eastern") dtval = stamp.to_pydatetime() assert stamp == dtval assert stamp.tzinfo == dtval.tzinfo
def test_timestamp_to_datetime_explicit_dateutil(self): stamp = Timestamp("20090415", tz=gettz("US/Eastern")) dtval = stamp.to_pydatetime() assert stamp == dtval assert stamp.tzinfo == dtval.tzinfo
def test_timestamp_to_datetime(self): stamp = Timestamp("20090415", tz="US/Eastern", freq="D") dtval = stamp.to_pydatetime() assert stamp == dtval assert stamp.tzinfo == dtval.tzinfo
def test_timestamp_to_datetime_explicit_dateutil(self): stamp = Timestamp('20090415', tz=gettz('US/Eastern'), freq='D') dtval = stamp.to_pydatetime() assert stamp == dtval assert stamp.tzinfo == dtval.tzinfo
def test_timestamp_to_datetime_tzoffset(self): tzinfo = tzoffset(None, 7200) expected = Timestamp('3/11/2012 04:00', tz=tzinfo) result = Timestamp(expected.to_pydatetime()) assert expected == result
def test_to_pydatetime_fold(self): # GH#45087 tzstr = "dateutil/usr/share/zoneinfo/America/Chicago" ts = Timestamp(year=2013, month=11, day=3, hour=1, minute=0, fold=1, tz=tzstr) dt = ts.to_pydatetime() assert dt.fold == 1