Ejemplo n.º 1
0
class TimestampOps:
    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)

    def time_to_julian_date(self, tz):
        self.ts.to_julian_date()

    def time_floor(self, tz):
        self.ts.floor("5T")

    def time_ceil(self, tz):
        self.ts.ceil("5T")
Ejemplo n.º 2
0
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)

    def time_to_julian_date(self, tz):
        self.ts.to_julian_date()

    def time_floor(self, tz):
        self.ts.floor('5T')

    def time_ceil(self, tz):
        self.ts.ceil('5T')
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
    def set_datetime(self, dt: pd.Timestamp) -> None:
        self._current_prices_col = None
        self._current_prices = None
        if self._current_dt is not None:
            # make up events for skipped days for update splits and divs.
            current_date = self._current_dt.normalize()
            target_date = dt.normalize()
            if current_date != target_date:
                for date in pd.bdate_range(current_date, target_date):
                    if date == current_date or date == target_date:
                        continue

                    stop = date + pd.DateOffset(days=1, seconds=-1)
                    table = self._prices.get_as_dict(date, stop)
                    bar_times = table.get_datetime_index()
                    if len(bar_times) == 0:
                        continue

                    open_time = bar_times[0]
                    super().set_datetime(open_time)
                    self.set_price('open')
                    self.market_open(self)

                    close_time = bar_times[-1]
                    super().set_datetime(close_time)
                    self.set_price('close')
                    self.update_portfolio_value()
                    self.market_close(self)
        super().set_datetime(dt)
Ejemplo n.º 5
0
def merge_date_time(x:pd.Timestamp,y:pd.Timestamp):
    '''
        This function takes in a date part (as naive timestamp) and a 
        time part (as Timestamp to today's date), and return a combined 
        timestamp.
    '''
    x = pd.Timestamp(x)
    y = pd.Timestamp(y)
    return pd.Timestamp(x.value + y.value - y.normalize().value)
Ejemplo n.º 6
0
class TimestampOps(object):
    params = [None, 'US/Eastern', 'UTC']
    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()
Ejemplo n.º 7
0
def _ts_floor(ts: pd.Timestamp, freq: str = 'month'):

    d = {
        'month': lambda: pd.Timedelta('1 day') * (ts.day - 1),
        'week': lambda: pd.Timedelta('1 day') * ts.weekday,
        'day': lambda: 0
    }

    f = d.get(freq)

    if not f:
        raise ValueError(
            """freq must be month, week, or day."""
        )
    
    return ts.normalize() - f()
Ejemplo n.º 8
0
 def set_datetime(self, dt: pd.Timestamp) -> None:
     if self._current_dt is not None:
         # make up events for skipped days for update splits and divs.
         current_date = self._current_dt.normalize()
         target_date = dt.normalize()
         for date in pd.bdate_range(current_date, target_date):
             if date == current_date or date == target_date:
                 continue
             super().set_datetime(date)
             self._update_time()
             self.market_open(self)
             if self._current_row is not None:
                 self._current_prices = self._current_row[
                     self.dataloader.ohlcv[3]].to_dict()
                 self.update_portfolio_value()
             self.market_close(self)
     super().set_datetime(dt)
     self._update_time()
Ejemplo n.º 9
0
 def test_normalize(self, tz_naive_fixture, arg):
     tz = tz_naive_fixture
     ts = Timestamp(arg, tz=tz)
     result = ts.normalize()
     expected = Timestamp("2013-11-30", tz=tz)
     assert result == expected
Ejemplo n.º 10
0
 def test_normalize(self, dt64, ts):
     alt = Timestamp(dt64)
     result = ts.normalize()
     assert result._reso == ts._reso
     assert result == alt.normalize()
Ejemplo n.º 11
0
 def test_normalize(self, tz_naive_fixture, arg):
     tz = tz_naive_fixture
     ts = Timestamp(arg, tz=tz)
     result = ts.normalize()
     expected = Timestamp('2013-11-30', tz=tz)
     assert result == expected
Ejemplo n.º 12
0
def get_ists_claims(
    date: pd.Timestamp,
    ids: Optional[pd.Index] = None,
    lr_flag: bool = True,
    columns: Optional[List] = None,
) -> pd.DataFrame:
    """
    Given a series of IDs and a date, return a df with ISTS claim info for each ID

    Claim info is taken from last LR date before given date.

    Parameters
    ----------
    date: pd.Timestamp
        Date to look up

    ids: pd.Index
        optional list of ids to select

    lr_flag: bool = True
        If True, just return records flagged as on the Live Register 
        
    columns: Optional[List] = None
        Columns from the ISTS claim database to return. Default is all columns.

    Returns
    -------
    df: pd.DataFrame
    """
    col_list = unpack(
        get_col_list(engine,
                     "ists_claims",
                     columns=columns,
                     required_columns=["lr_flag"]) +
        get_col_list(engine,
                     "ists_personal",
                     columns=columns,
                     required_columns=["ppsn"]))
    lookup_date = nearest_lr_date(date.normalize(), how="previous").date()
    query_text = f"""\
        SELECT {col_list} 
            FROM ists_claims c
            JOIN ists_personal p
            ON c.personal_id=p.id
            WHERE lr_date = '{lookup_date}'
        """
    if lr_flag:
        query_text += """\
            AND lr_flag is true
        """
    query, params = get_parameterized_query(query_text, ids)
    ists = pd.read_sql(
        query,
        con=engine,
        params=params,
        parse_dates=datetime_cols(engine, "ists_claims") +
        datetime_cols(engine, "ists_personal"),
    ).drop_duplicates("ppsn", keep="first")

    return (ists.dropna(axis=0, how="all").reset_index(drop=True).set_index(
        "ppsn", verify_integrity=True))