class Properties(object): def setup(self): self.per = Period('2017-09-06 08:28', freq='min') def time_year(self): self.per.year def time_month(self): self.per.month def time_day(self): self.per.day def time_hour(self): self.per.hour def time_minute(self): self.per.minute def time_second(self): self.per.second def time_is_leap_year(self): self.per.is_leap_year def time_quarter(self): self.per.quarter def time_qyear(self): self.per.qyear def time_week(self): self.per.week def time_daysinmonth(self): self.per.daysinmonth def time_dayofweek(self): self.per.dayofweek def time_dayofyear(self): self.per.dayofyear def time_start_time(self): self.per.start_time def time_end_time(self): self.per.end_time def time_to_timestamp(): self.per.to_timestamp() def time_now(): self.per.now() def time_asfreq(): self.per.asfreq('A')
def test_to_timestamp_mult(self): p = Period("2011-01", freq="M") assert p.to_timestamp(how="S") == Timestamp("2011-01-01") expected = Timestamp("2011-02-01") - Timedelta(1, "ns") assert p.to_timestamp(how="E") == expected p = Period("2011-01", freq="3M") assert p.to_timestamp(how="S") == Timestamp("2011-01-01") expected = Timestamp("2011-04-01") - Timedelta(1, "ns") assert p.to_timestamp(how="E") == expected
def test_period_cons_quarterly(self, month): # bugs in scikits.timeseries freq = f"Q-{month}" exp = Period("1989Q3", freq=freq) assert "1989Q3" in str(exp) stamp = exp.to_timestamp("D", how="end") p = Period(stamp, freq=freq) assert p == exp stamp = exp.to_timestamp("3D", how="end") p = Period(stamp, freq=freq) assert p == exp
def test_period_cons_quarterly(self): # bugs in scikits.timeseries for month in MONTHS: freq = 'Q-%s' % month exp = Period('1989Q3', freq=freq) assert '1989Q3' in str(exp) stamp = exp.to_timestamp('D', how='end') p = Period(stamp, freq=freq) assert p == exp stamp = exp.to_timestamp('3D', how='end') p = Period(stamp, freq=freq) assert p == exp
class PeriodUnaryMethods(object): params = ['M', 'min'] param_names = ['freq'] def setup(self, freq): self.per = Period('2012-06-01', freq=freq) def time_to_timestamp(self, freq): self.per.to_timestamp() def time_now(self, freq): self.per.now(freq) def time_asfreq(self, freq): self.per.asfreq('A')
class PeriodUnaryMethods: params = ["M", "min"] param_names = ["freq"] def setup(self, freq): self.per = Period("2012-06-01", freq=freq) def time_to_timestamp(self, freq): self.per.to_timestamp() def time_now(self, freq): self.per.now(freq) def time_asfreq(self, freq): self.per.asfreq("A")
def test_period_cons_annual(self): # bugs in scikits.timeseries for month in MONTHS: freq = 'A-%s' % month exp = Period('1989', freq=freq) stamp = exp.to_timestamp('D', how='end') + timedelta(days=30) p = Period(stamp, freq=freq) assert p == exp + 1 assert isinstance(p, Period)
def test_period_cons_annual(self, month): # bugs in scikits.timeseries freq = f"A-{month}" exp = Period("1989", freq=freq) stamp = exp.to_timestamp("D", how="end") + timedelta(days=30) p = Period(stamp, freq=freq) assert p == exp + 1 assert isinstance(p, Period)
def func(start_period: pd.Period, end_period: pd.Period) -> pd.DataFrame: start_period = max(start_period, pd.Period(self._currency_min_date[name], freq='M')) end_period = min(end_period, pd.Period.now(freq='M')) date_range = pd.date_range(start=start_period.to_timestamp(), end=(end_period + 1).to_timestamp(), freq='D') df = pd.DataFrame({'date': date_range, 'close': 1.0}) df['period'] = df['date'].dt.to_period(freq='M') df_new = df[(start_period <= df['period']) & (df['period'] <= end_period)].copy() return df_new
def test_to_timestamp_out_of_bounds(self): # GH#19643, used to incorrectly give Timestamp in 1754 per = Period("0001-01-01", freq="B") msg = "Out of bounds nanosecond timestamp" with pytest.raises(OutOfBoundsDatetime, match=msg): per.to_timestamp()
def test_to_timestamp(self): p = Period('1982', freq='A') start_ts = p.to_timestamp(how='S') aliases = ['s', 'StarT', 'BEGIn'] for a in aliases: assert start_ts == p.to_timestamp('D', how=a) # freq with mult should not affect to the result assert start_ts == p.to_timestamp('3D', how=a) end_ts = p.to_timestamp(how='E') aliases = ['e', 'end', 'FINIsH'] for a in aliases: assert end_ts == p.to_timestamp('D', how=a) assert end_ts == p.to_timestamp('3D', how=a) from_lst = ['A', 'Q', 'M', 'W', 'B', 'D', 'H', 'Min', 'S'] def _ex(p): return Timestamp((p + 1).start_time.value - 1) for i, fcode in enumerate(from_lst): p = Period('1982', freq=fcode) result = p.to_timestamp().to_period(fcode) assert result == p assert p.start_time == p.to_timestamp(how='S') assert p.end_time == _ex(p) # Frequency other than daily p = Period('1985', freq='A') result = p.to_timestamp('H', how='end') expected = datetime(1985, 12, 31, 23) assert result == expected result = p.to_timestamp('3H', how='end') assert result == expected result = p.to_timestamp('T', how='end') expected = datetime(1985, 12, 31, 23, 59) assert result == expected result = p.to_timestamp('2T', how='end') assert result == expected result = p.to_timestamp(how='end') expected = datetime(1985, 12, 31) assert result == expected expected = datetime(1985, 1, 1) result = p.to_timestamp('H', how='start') assert result == expected result = p.to_timestamp('T', how='start') assert result == expected result = p.to_timestamp('S', how='start') assert result == expected result = p.to_timestamp('3H', how='start') assert result == expected result = p.to_timestamp('5S', how='start') assert result == expected
def test_to_timestamp_out_of_bounds(self): # GH#19643, currently gives Timestamp('1754-08-30 22:43:41.128654848') per = Period("0001-01-01", freq="B") with pytest.raises(OutOfBoundsDatetime): per.to_timestamp()
def test_to_timestamp_out_of_bounds(self): # GH#19643, currently gives Timestamp('1754-08-30 22:43:41.128654848') per = Period('0001-01-01', freq='B') with pytest.raises(OutOfBoundsDatetime): per.to_timestamp()
def test_to_timestamp_out_of_bounds(self): # GH#19643, used to incorrectly give Timestamp in 1754 per = Period("0001-01-01", freq="B") with pytest.raises(OutOfBoundsDatetime): per.to_timestamp()
def test_to_timestamp(self): p = Period("1982", freq="A") start_ts = p.to_timestamp(how="S") aliases = ["s", "StarT", "BEGIn"] for a in aliases: assert start_ts == p.to_timestamp("D", how=a) # freq with mult should not affect to the result assert start_ts == p.to_timestamp("3D", how=a) end_ts = p.to_timestamp(how="E") aliases = ["e", "end", "FINIsH"] for a in aliases: assert end_ts == p.to_timestamp("D", how=a) assert end_ts == p.to_timestamp("3D", how=a) from_lst = ["A", "Q", "M", "W", "B", "D", "H", "Min", "S"] def _ex(p): return Timestamp((p + p.freq).start_time.value - 1) for i, fcode in enumerate(from_lst): p = Period("1982", freq=fcode) result = p.to_timestamp().to_period(fcode) assert result == p assert p.start_time == p.to_timestamp(how="S") assert p.end_time == _ex(p) # Frequency other than daily p = Period("1985", freq="A") result = p.to_timestamp("H", how="end") expected = Timestamp(1986, 1, 1) - Timedelta(1, "ns") assert result == expected result = p.to_timestamp("3H", how="end") assert result == expected result = p.to_timestamp("T", how="end") expected = Timestamp(1986, 1, 1) - Timedelta(1, "ns") assert result == expected result = p.to_timestamp("2T", how="end") assert result == expected result = p.to_timestamp(how="end") expected = Timestamp(1986, 1, 1) - Timedelta(1, "ns") assert result == expected expected = datetime(1985, 1, 1) result = p.to_timestamp("H", how="start") assert result == expected result = p.to_timestamp("T", how="start") assert result == expected result = p.to_timestamp("S", how="start") assert result == expected result = p.to_timestamp("3H", how="start") assert result == expected result = p.to_timestamp("5S", how="start") assert result == expected