Python pandas.tseries.frequencies.get_freq_code() Examples
The following are 30
code examples of pandas.tseries.frequencies.get_freq_code().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pandas.tseries.frequencies
, or try the search function
.
Example #1
Source File: test_frequencies.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def test_get_to_timestamp_base(self): tsb = frequencies.get_to_timestamp_base assert (tsb(frequencies.get_freq_code('D')[0]) == frequencies.get_freq_code('D')[0]) assert (tsb(frequencies.get_freq_code('W')[0]) == frequencies.get_freq_code('D')[0]) assert (tsb(frequencies.get_freq_code('M')[0]) == frequencies.get_freq_code('D')[0]) assert (tsb(frequencies.get_freq_code('S')[0]) == frequencies.get_freq_code('S')[0]) assert (tsb(frequencies.get_freq_code('T')[0]) == frequencies.get_freq_code('S')[0]) assert (tsb(frequencies.get_freq_code('H')[0]) == frequencies.get_freq_code('S')[0])
Example #2
Source File: offsets.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def _end_apply_index(self, i, freq): """Offsets index to end of Period frequency""" off = i.to_perioddelta('D') from pandas.tseries.frequencies import get_freq_code base, mult = get_freq_code(freq) base_period = i.to_period(base) if self.n > 0: # when adding, dates on end roll to next roll = np.where(base_period.to_timestamp(how='end') == i - off, self.n, self.n - 1) else: roll = self.n base = (base_period + roll).to_timestamp(how='end') return base + off # way to get around weirdness with rule_code
Example #3
Source File: offsets.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def _beg_apply_index(self, i, freq): """Offsets index to beginning of Period frequency""" off = i.to_perioddelta('D') from pandas.tseries.frequencies import get_freq_code base, mult = get_freq_code(freq) base_period = i.to_period(base) if self.n <= 0: # when subtracting, dates on start roll to prior roll = np.where(base_period.to_timestamp() == i - off, self.n, self.n + 1) else: roll = self.n base = (base_period + roll).to_timestamp() return base + off
Example #4
Source File: test_frequencies.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_get_to_timestamp_base(self): tsb = frequencies.get_to_timestamp_base assert (tsb(frequencies.get_freq_code('D')[0]) == frequencies.get_freq_code('D')[0]) assert (tsb(frequencies.get_freq_code('W')[0]) == frequencies.get_freq_code('D')[0]) assert (tsb(frequencies.get_freq_code('M')[0]) == frequencies.get_freq_code('D')[0]) assert (tsb(frequencies.get_freq_code('S')[0]) == frequencies.get_freq_code('S')[0]) assert (tsb(frequencies.get_freq_code('T')[0]) == frequencies.get_freq_code('S')[0]) assert (tsb(frequencies.get_freq_code('H')[0]) == frequencies.get_freq_code('S')[0])
Example #5
Source File: offsets.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
def _beg_apply_index(self, i, freq): """Offsets index to beginning of Period frequency""" off = i.to_perioddelta('D') from pandas.tseries.frequencies import get_freq_code base, mult = get_freq_code(freq) base_period = i.to_period(base) if self.n <= 0: # when subtracting, dates on start roll to prior roll = np.where(base_period.to_timestamp() == i - off, self.n, self.n + 1) else: roll = self.n base = (base_period + roll).to_timestamp() return base + off
Example #6
Source File: offsets.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
def _end_apply_index(self, i, freq): """Offsets index to end of Period frequency""" off = i.to_perioddelta('D') from pandas.tseries.frequencies import get_freq_code base, mult = get_freq_code(freq) base_period = i.to_period(base) if self.n > 0: # when adding, dates on end roll to next roll = np.where(base_period.to_timestamp(how='end') == i - off, self.n, self.n - 1) else: roll = self.n base = (base_period + roll).to_timestamp(how='end') return base + off # way to get around weirdness with rule_code
Example #7
Source File: test_frequencies.py From vnpy_crypto with MIT License | 6 votes |
def test_get_to_timestamp_base(self): tsb = frequencies.get_to_timestamp_base assert (tsb(frequencies.get_freq_code('D')[0]) == frequencies.get_freq_code('D')[0]) assert (tsb(frequencies.get_freq_code('W')[0]) == frequencies.get_freq_code('D')[0]) assert (tsb(frequencies.get_freq_code('M')[0]) == frequencies.get_freq_code('D')[0]) assert (tsb(frequencies.get_freq_code('S')[0]) == frequencies.get_freq_code('S')[0]) assert (tsb(frequencies.get_freq_code('T')[0]) == frequencies.get_freq_code('S')[0]) assert (tsb(frequencies.get_freq_code('H')[0]) == frequencies.get_freq_code('S')[0])
Example #8
Source File: period.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _period_index_cmp(opname, nat_result=False): """ Wrap comparison operations to convert datetime-like to datetime64 """ def wrapper(self, other): if isinstance(other, Period): func = getattr(self._values, opname) other_base, _ = _gfc(other.freq) if other.freq != self.freq: msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) result = func(other.ordinal) elif isinstance(other, PeriodIndex): if other.freq != self.freq: msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) result = getattr(self._values, opname)(other._values) mask = self._isnan | other._isnan if mask.any(): result[mask] = nat_result return result elif other is tslib.NaT: result = np.empty(len(self._values), dtype=bool) result.fill(nat_result) else: other = Period(other, freq=self.freq) func = getattr(self._values, opname) result = func(other.ordinal) if self.hasnans: result[self._isnan] = nat_result return result return wrapper
Example #9
Source File: period.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def to_timestamp(self, freq=None, how='start'): """ Cast to DatetimeIndex Parameters ---------- freq : string or DateOffset, default 'D' for week or longer, 'S' otherwise Target frequency how : {'s', 'e', 'start', 'end'} Returns ------- DatetimeIndex """ how = _validate_end_alias(how) if freq is None: base, mult = _gfc(self.freq) freq = frequencies.get_to_timestamp_base(base) else: freq = Period._maybe_convert_freq(freq) base, mult = _gfc(freq) new_data = self.asfreq(freq, how) new_data = period.periodarr_to_dt64arr(new_data._values, base) return DatetimeIndex(new_data, freq='infer', name=self.name)
Example #10
Source File: period.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def _period_index_cmp(opname, nat_result=False): """ Wrap comparison operations to convert datetime-like to datetime64 """ def wrapper(self, other): if isinstance(other, Period): func = getattr(self._values, opname) other_base, _ = _gfc(other.freq) if other.freq != self.freq: msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) result = func(other.ordinal) elif isinstance(other, PeriodIndex): if other.freq != self.freq: msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) result = getattr(self._values, opname)(other._values) mask = self._isnan | other._isnan if mask.any(): result[mask] = nat_result return result elif other is tslib.NaT: result = np.empty(len(self._values), dtype=bool) result.fill(nat_result) else: other = Period(other, freq=self.freq) func = getattr(self._values, opname) result = func(other.ordinal) if self.hasnans: result[self._isnan] = nat_result return result return wrapper
Example #11
Source File: period.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def dt64arr_to_periodarr(data, freq, tz): if data.dtype != np.dtype('M8[ns]'): raise ValueError('Wrong dtype: %s' % data.dtype) freq = Period._maybe_convert_freq(freq) base, mult = _gfc(freq) return period.dt64arr_to_periodarr(data.view('i8'), base, tz) # --- Period index sketch
Example #12
Source File: test_timestamp.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def test_frequency_misc(self): assert (frequencies.get_freq_group('T') == frequencies.FreqGroup.FR_MIN) code, stride = frequencies.get_freq_code(offsets.Hour()) assert code == frequencies.FreqGroup.FR_HR code, stride = frequencies.get_freq_code((5, 'T')) assert code == frequencies.FreqGroup.FR_MIN assert stride == 5 offset = offsets.Hour() result = frequencies.to_offset(offset) assert result == offset result = frequencies.to_offset((5, 'T')) expected = offsets.Minute(5) assert result == expected pytest.raises(ValueError, frequencies.get_freq_code, (5, 'baz')) pytest.raises(ValueError, frequencies.to_offset, '100foo') pytest.raises(ValueError, frequencies.to_offset, ('', '')) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): result = frequencies.get_standard_freq(offsets.Hour()) assert result == 'H'
Example #13
Source File: period.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _range_from_fields(year=None, month=None, quarter=None, day=None, hour=None, minute=None, second=None, freq=None): if hour is None: hour = 0 if minute is None: minute = 0 if second is None: second = 0 if day is None: day = 1 ordinals = [] if quarter is not None: if freq is None: freq = 'Q' base = frequencies.FreqGroup.FR_QTR else: base, mult = _gfc(freq) if base != frequencies.FreqGroup.FR_QTR: raise AssertionError("base must equal FR_QTR") year, quarter = _make_field_arrays(year, quarter) for y, q in zip(year, quarter): y, m = _quarter_to_myear(y, q, freq) val = period.period_ordinal(y, m, 1, 1, 1, 1, 0, 0, base) ordinals.append(val) else: base, mult = _gfc(freq) arrays = _make_field_arrays(year, month, day, hour, minute, second) for y, mth, d, h, mn, s in zip(*arrays): ordinals.append(period.period_ordinal( y, mth, d, h, mn, s, 0, 0, base)) return np.array(ordinals, dtype=np.int64), freq
Example #14
Source File: period.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def to_timestamp(self, freq=None, how='start'): """ Cast to DatetimeIndex Parameters ---------- freq : string or DateOffset, default 'D' for week or longer, 'S' otherwise Target frequency how : {'s', 'e', 'start', 'end'} Returns ------- DatetimeIndex """ how = _validate_end_alias(how) if freq is None: base, mult = _gfc(self.freq) freq = frequencies.get_to_timestamp_base(base) else: freq = Period._maybe_convert_freq(freq) base, mult = _gfc(freq) new_data = self.asfreq(freq, how) new_data = period.periodarr_to_dt64arr(new_data._values, base) return DatetimeIndex(new_data, freq='infer', name=self.name)
Example #15
Source File: period.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def dt64arr_to_periodarr(data, freq, tz): if data.dtype != np.dtype('M8[ns]'): raise ValueError('Wrong dtype: %s' % data.dtype) freq = Period._maybe_convert_freq(freq) base, mult = _gfc(freq) return period.dt64arr_to_periodarr(data.view('i8'), base, tz) # --- Period index sketch
Example #16
Source File: period.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _field_accessor(name, alias, docstring=None): def f(self): base, mult = _gfc(self.freq) result = get_period_field_arr(alias, self._values, base) return Index(result, name=self.name) f.__name__ = name f.__doc__ = docstring return property(f)
Example #17
Source File: test_timeseries.py From Computable with MIT License | 5 votes |
def test_frequency_misc(self): self.assertEquals(fmod.get_freq_group('T'), fmod.FreqGroup.FR_MIN) code, stride = fmod.get_freq_code(offsets.Hour()) self.assertEquals(code, fmod.FreqGroup.FR_HR) code, stride = fmod.get_freq_code((5, 'T')) self.assertEquals(code, fmod.FreqGroup.FR_MIN) self.assertEquals(stride, 5) offset = offsets.Hour() result = fmod.to_offset(offset) self.assertEquals(result, offset) result = fmod.to_offset((5, 'T')) expected = offsets.Minute(5) self.assertEquals(result, expected) self.assertRaises(ValueError, fmod.get_freq_code, (5, 'baz')) self.assertRaises(ValueError, fmod.to_offset, '100foo') self.assertRaises(ValueError, fmod.to_offset, ('', '')) result = fmod.get_standard_freq(offsets.Hour()) self.assertEquals(result, 'H')
Example #18
Source File: period.py From vnpy_crypto with MIT License | 5 votes |
def _range_from_fields(year=None, month=None, quarter=None, day=None, hour=None, minute=None, second=None, freq=None): if hour is None: hour = 0 if minute is None: minute = 0 if second is None: second = 0 if day is None: day = 1 ordinals = [] if quarter is not None: if freq is None: freq = 'Q' base = frequencies.FreqGroup.FR_QTR else: base, mult = _gfc(freq) if base != frequencies.FreqGroup.FR_QTR: raise AssertionError("base must equal FR_QTR") year, quarter = _make_field_arrays(year, quarter) for y, q in zip(year, quarter): y, m = _quarter_to_myear(y, q, freq) val = period.period_ordinal(y, m, 1, 1, 1, 1, 0, 0, base) ordinals.append(val) else: base, mult = _gfc(freq) arrays = _make_field_arrays(year, month, day, hour, minute, second) for y, mth, d, h, mn, s in zip(*arrays): ordinals.append(period.period_ordinal( y, mth, d, h, mn, s, 0, 0, base)) return np.array(ordinals, dtype=np.int64), freq
Example #19
Source File: period.py From vnpy_crypto with MIT License | 5 votes |
def to_timestamp(self, freq=None, how='start'): """ Cast to DatetimeIndex Parameters ---------- freq : string or DateOffset, optional Target frequency. The default is 'D' for week or longer, 'S' otherwise how : {'s', 'e', 'start', 'end'} Returns ------- DatetimeIndex """ how = _validate_end_alias(how) if freq is None: base, mult = _gfc(self.freq) freq = frequencies.get_to_timestamp_base(base) else: freq = Period._maybe_convert_freq(freq) base, mult = _gfc(freq) new_data = self.asfreq(freq, how) new_data = period.periodarr_to_dt64arr(new_data._ndarray_values, base) return DatetimeIndex(new_data, freq='infer', name=self.name)
Example #20
Source File: period.py From vnpy_crypto with MIT License | 5 votes |
def dt64arr_to_periodarr(data, freq, tz): if data.dtype != np.dtype('M8[ns]'): raise ValueError('Wrong dtype: %s' % data.dtype) freq = Period._maybe_convert_freq(freq) base, mult = _gfc(freq) return period.dt64arr_to_periodarr(data.view('i8'), base, tz) # --- Period index sketch
Example #21
Source File: period.py From vnpy_crypto with MIT License | 5 votes |
def _field_accessor(name, alias, docstring=None): def f(self): base, mult = _gfc(self.freq) result = get_period_field_arr(alias, self._ndarray_values, base) return Index(result, name=self.name) f.__name__ = name f.__doc__ = docstring return property(f)
Example #22
Source File: test_frequencies.py From vnpy_crypto with MIT License | 5 votes |
def test_frequency_misc(self): assert (resolution.get_freq_group('T') == frequencies.FreqGroup.FR_MIN) code, stride = frequencies.get_freq_code(offsets.Hour()) assert code == frequencies.FreqGroup.FR_HR code, stride = frequencies.get_freq_code((5, 'T')) assert code == frequencies.FreqGroup.FR_MIN assert stride == 5 offset = offsets.Hour() result = frequencies.to_offset(offset) assert result == offset result = frequencies.to_offset((5, 'T')) expected = offsets.Minute(5) assert result == expected with tm.assert_raises_regex(ValueError, 'Invalid frequency'): frequencies.get_freq_code((5, 'baz')) with tm.assert_raises_regex(ValueError, 'Invalid frequency'): frequencies.to_offset('100foo') with tm.assert_raises_regex(ValueError, 'Could not evaluate'): frequencies.to_offset(('', ''))
Example #23
Source File: test_frequencies.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_frequency_misc(self): assert (resolution.get_freq_group('T') == frequencies.FreqGroup.FR_MIN) code, stride = frequencies.get_freq_code(offsets.Hour()) assert code == frequencies.FreqGroup.FR_HR code, stride = frequencies.get_freq_code((5, 'T')) assert code == frequencies.FreqGroup.FR_MIN assert stride == 5 offset = offsets.Hour() result = frequencies.to_offset(offset) assert result == offset result = frequencies.to_offset((5, 'T')) expected = offsets.Minute(5) assert result == expected with tm.assert_raises_regex(ValueError, 'Invalid frequency'): frequencies.get_freq_code((5, 'baz')) with tm.assert_raises_regex(ValueError, 'Invalid frequency'): frequencies.to_offset('100foo') with tm.assert_raises_regex(ValueError, 'Could not evaluate'): frequencies.to_offset(('', ''))
Example #24
Source File: period.py From Splunking-Crime with GNU Affero General Public License v3.0 | 4 votes |
def _get_ordinal_range(start, end, periods, freq, mult=1): if com._count_not_none(start, end, periods) != 2: raise ValueError('Of the three parameters: start, end, and periods, ' 'exactly two must be specified') if freq is not None: _, mult = _gfc(freq) if start is not None: start = Period(start, freq) if end is not None: end = Period(end, freq) is_start_per = isinstance(start, Period) is_end_per = isinstance(end, Period) if is_start_per and is_end_per and start.freq != end.freq: raise ValueError('start and end must have same freq') if (start is tslib.NaT or end is tslib.NaT): raise ValueError('start and end must not be NaT') if freq is None: if is_start_per: freq = start.freq elif is_end_per: freq = end.freq else: # pragma: no cover raise ValueError('Could not infer freq from start/end') if periods is not None: periods = periods * mult if start is None: data = np.arange(end.ordinal - periods + mult, end.ordinal + 1, mult, dtype=np.int64) else: data = np.arange(start.ordinal, start.ordinal + periods, mult, dtype=np.int64) else: data = np.arange(start.ordinal, end.ordinal + 1, mult, dtype=np.int64) return data, freq
Example #25
Source File: test_frequencies.py From twitter-stock-recommendation with MIT License | 4 votes |
def test_get_freq_code(self): # frequency str assert (frequencies.get_freq_code('A') == (frequencies.get_freq('A'), 1)) assert (frequencies.get_freq_code('3D') == (frequencies.get_freq('D'), 3)) assert (frequencies.get_freq_code('-2M') == (frequencies.get_freq('M'), -2)) # tuple assert (frequencies.get_freq_code(('D', 1)) == (frequencies.get_freq('D'), 1)) assert (frequencies.get_freq_code(('A', 3)) == (frequencies.get_freq('A'), 3)) assert (frequencies.get_freq_code(('M', -2)) == (frequencies.get_freq('M'), -2)) # numeric tuple assert frequencies.get_freq_code((1000, 1)) == (1000, 1) # offsets assert (frequencies.get_freq_code(offsets.Day()) == (frequencies.get_freq('D'), 1)) assert (frequencies.get_freq_code(offsets.Day(3)) == (frequencies.get_freq('D'), 3)) assert (frequencies.get_freq_code(offsets.Day(-2)) == (frequencies.get_freq('D'), -2)) assert (frequencies.get_freq_code(offsets.MonthEnd()) == (frequencies.get_freq('M'), 1)) assert (frequencies.get_freq_code(offsets.MonthEnd(3)) == (frequencies.get_freq('M'), 3)) assert (frequencies.get_freq_code(offsets.MonthEnd(-2)) == (frequencies.get_freq('M'), -2)) assert (frequencies.get_freq_code(offsets.Week()) == (frequencies.get_freq('W'), 1)) assert (frequencies.get_freq_code(offsets.Week(3)) == (frequencies.get_freq('W'), 3)) assert (frequencies.get_freq_code(offsets.Week(-2)) == (frequencies.get_freq('W'), -2)) # Monday is weekday=0 assert (frequencies.get_freq_code(offsets.Week(weekday=1)) == (frequencies.get_freq('W-TUE'), 1)) assert (frequencies.get_freq_code(offsets.Week(3, weekday=0)) == (frequencies.get_freq('W-MON'), 3)) assert (frequencies.get_freq_code(offsets.Week(-2, weekday=4)) == (frequencies.get_freq('W-FRI'), -2))
Example #26
Source File: period.py From elasticintel with GNU General Public License v3.0 | 4 votes |
def _get_ordinal_range(start, end, periods, freq, mult=1): if com._count_not_none(start, end, periods) != 2: raise ValueError('Of the three parameters: start, end, and periods, ' 'exactly two must be specified') if freq is not None: _, mult = _gfc(freq) if start is not None: start = Period(start, freq) if end is not None: end = Period(end, freq) is_start_per = isinstance(start, Period) is_end_per = isinstance(end, Period) if is_start_per and is_end_per and start.freq != end.freq: raise ValueError('start and end must have same freq') if (start is tslib.NaT or end is tslib.NaT): raise ValueError('start and end must not be NaT') if freq is None: if is_start_per: freq = start.freq elif is_end_per: freq = end.freq else: # pragma: no cover raise ValueError('Could not infer freq from start/end') if periods is not None: periods = periods * mult if start is None: data = np.arange(end.ordinal - periods + mult, end.ordinal + 1, mult, dtype=np.int64) else: data = np.arange(start.ordinal, start.ordinal + periods, mult, dtype=np.int64) else: data = np.arange(start.ordinal, end.ordinal + 1, mult, dtype=np.int64) return data, freq
Example #27
Source File: period.py From elasticintel with GNU General Public License v3.0 | 4 votes |
def asfreq(self, freq=None, how='E'): """ Convert the PeriodIndex to the specified frequency `freq`. Parameters ---------- freq : str a frequency how : str {'E', 'S'} 'E', 'END', or 'FINISH' for end, 'S', 'START', or 'BEGIN' for start. Whether the elements should be aligned to the end or start within pa period. January 31st ('END') vs. Janury 1st ('START') for example. Returns ------- new : PeriodIndex with the new frequency Examples -------- >>> pidx = pd.period_range('2010-01-01', '2015-01-01', freq='A') >>> pidx <class 'pandas.core.indexes.period.PeriodIndex'> [2010, ..., 2015] Length: 6, Freq: A-DEC >>> pidx.asfreq('M') <class 'pandas.core.indexes.period.PeriodIndex'> [2010-12, ..., 2015-12] Length: 6, Freq: M >>> pidx.asfreq('M', how='S') <class 'pandas.core.indexes.period.PeriodIndex'> [2010-01, ..., 2015-01] Length: 6, Freq: M """ how = _validate_end_alias(how) freq = Period._maybe_convert_freq(freq) base1, mult1 = _gfc(self.freq) base2, mult2 = _gfc(freq) asi8 = self.asi8 # mult1 can't be negative or 0 end = how == 'E' if end: ordinal = asi8 + mult1 - 1 else: ordinal = asi8 new_data = period.period_asfreq_arr(ordinal, base1, base2, end) if self.hasnans: new_data[self._isnan] = tslib.iNaT return self._simple_new(new_data, self.name, freq=freq)
Example #28
Source File: test_frequencies.py From elasticintel with GNU General Public License v3.0 | 4 votes |
def test_get_freq_code(self): # frequency str assert (frequencies.get_freq_code('A') == (frequencies.get_freq('A'), 1)) assert (frequencies.get_freq_code('3D') == (frequencies.get_freq('D'), 3)) assert (frequencies.get_freq_code('-2M') == (frequencies.get_freq('M'), -2)) # tuple assert (frequencies.get_freq_code(('D', 1)) == (frequencies.get_freq('D'), 1)) assert (frequencies.get_freq_code(('A', 3)) == (frequencies.get_freq('A'), 3)) assert (frequencies.get_freq_code(('M', -2)) == (frequencies.get_freq('M'), -2)) # numeric tuple assert frequencies.get_freq_code((1000, 1)) == (1000, 1) # offsets assert (frequencies.get_freq_code(offsets.Day()) == (frequencies.get_freq('D'), 1)) assert (frequencies.get_freq_code(offsets.Day(3)) == (frequencies.get_freq('D'), 3)) assert (frequencies.get_freq_code(offsets.Day(-2)) == (frequencies.get_freq('D'), -2)) assert (frequencies.get_freq_code(offsets.MonthEnd()) == (frequencies.get_freq('M'), 1)) assert (frequencies.get_freq_code(offsets.MonthEnd(3)) == (frequencies.get_freq('M'), 3)) assert (frequencies.get_freq_code(offsets.MonthEnd(-2)) == (frequencies.get_freq('M'), -2)) assert (frequencies.get_freq_code(offsets.Week()) == (frequencies.get_freq('W'), 1)) assert (frequencies.get_freq_code(offsets.Week(3)) == (frequencies.get_freq('W'), 3)) assert (frequencies.get_freq_code(offsets.Week(-2)) == (frequencies.get_freq('W'), -2)) # Monday is weekday=0 assert (frequencies.get_freq_code(offsets.Week(weekday=1)) == (frequencies.get_freq('W-TUE'), 1)) assert (frequencies.get_freq_code(offsets.Week(3, weekday=0)) == (frequencies.get_freq('W-MON'), 3)) assert (frequencies.get_freq_code(offsets.Week(-2, weekday=4)) == (frequencies.get_freq('W-FRI'), -2))
Example #29
Source File: utils.py From pyfinance with MIT License | 4 votes |
def get_anlz_factor(freq): """Find the number of periods per year given a frequency. Parameters ---------- freq : str Any frequency str or anchored offset str recognized by Pandas. Returns ------- float Example ------- >>> get_anlz_factor('D') 252.0 >>> get_anlz_factor('5D') # 5-business-day periods per year 50.4 >>> get_anlz_factor('Q') 4.0 >>> get_anlz_factor('Q-DEC') 4.0 >>> get_anlz_factor('BQS-APR') 4.0 """ # 'Q-NOV' would give us (2001, 1); we just want (2000, 1). try: base, mult = get_freq_code(freq) except ValueError: # The above will fail for a bunch of irregular frequencies, such # as 'Q-NOV' or 'BQS-APR' freq = freq.upper() if freq.startswith(("A-", "BA-", "AS-", "BAS-")): freq = "A" elif freq.startswith(("Q-", "BQ-", "QS-", "BQS-")): freq = "Q" elif freq in {"MS", "BMS"}: freq = "M" else: raise ValueError("Invalid frequency: %s" % freq) base, mult = get_freq_code(freq) return PERIODS_PER_YEAR[(base // 1000) * 1000] / mult
Example #30
Source File: period.py From vnpy_crypto with MIT License | 4 votes |
def _get_ordinal_range(start, end, periods, freq, mult=1): if com._count_not_none(start, end, periods) != 2: raise ValueError('Of the three parameters: start, end, and periods, ' 'exactly two must be specified') if freq is not None: _, mult = _gfc(freq) if start is not None: start = Period(start, freq) if end is not None: end = Period(end, freq) is_start_per = isinstance(start, Period) is_end_per = isinstance(end, Period) if is_start_per and is_end_per and start.freq != end.freq: raise ValueError('start and end must have same freq') if (start is tslib.NaT or end is tslib.NaT): raise ValueError('start and end must not be NaT') if freq is None: if is_start_per: freq = start.freq elif is_end_per: freq = end.freq else: # pragma: no cover raise ValueError('Could not infer freq from start/end') if periods is not None: periods = periods * mult if start is None: data = np.arange(end.ordinal - periods + mult, end.ordinal + 1, mult, dtype=np.int64) else: data = np.arange(start.ordinal, start.ordinal + periods, mult, dtype=np.int64) else: data = np.arange(start.ordinal, end.ordinal + 1, mult, dtype=np.int64) return data, freq