Python pandas.tseries.frequencies.to_offset() Examples
The following are 30
code examples of pandas.tseries.frequencies.to_offset().
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: offsets.py From recruit with Apache License 2.0 | 6 votes |
def __eq__(self, other): if isinstance(other, compat.string_types): from pandas.tseries.frequencies import to_offset try: # GH#23524 if to_offset fails, we are dealing with an # incomparable type so == is False and != is True other = to_offset(other) except ValueError: # e.g. "infer" return False if isinstance(other, Tick): return self.delta == other.delta else: return False # This is identical to DateOffset.__hash__, but has to be redefined here # for Python 3, because we've redefined __eq__.
Example #2
Source File: timedeltas.py From recruit with Apache License 2.0 | 6 votes |
def _from_sequence(cls, data, dtype=_TD_DTYPE, copy=False, freq=None, unit=None): if dtype: _validate_td64_dtype(dtype) freq, freq_infer = dtl.maybe_infer_freq(freq) data, inferred_freq = sequence_to_td64ns(data, copy=copy, unit=unit) freq, freq_infer = dtl.validate_inferred_freq(freq, inferred_freq, freq_infer) result = cls._simple_new(data, freq=freq) if inferred_freq is None and freq is not None: # this condition precludes `freq_infer` cls._validate_frequency(result, freq) elif freq_infer: # Set _freq directly to bypass duplicative _validate_frequency # check. result._freq = to_offset(result.inferred_freq) return result
Example #3
Source File: resample.py From Computable with MIT License | 6 votes |
def _get_range_edges(axis, offset, closed='left', base=0): if isinstance(offset, compat.string_types): offset = to_offset(offset) if isinstance(offset, Tick): day_nanos = _delta_to_nanoseconds(timedelta(1)) # #1165 if (day_nanos % offset.nanos) == 0: return _adjust_dates_anchored(axis[0], axis[-1], offset, closed=closed, base=base) first, last = axis[0], axis[-1] if not isinstance(offset, Tick): # and first.time() != last.time(): # hack! first = tools.normalize_date(first) last = tools.normalize_date(last) if closed == 'left': first = Timestamp(offset.rollback(first)) else: first = Timestamp(first - offset) last = Timestamp(last + offset) return first, last
Example #4
Source File: datetimelike.py From recruit with Apache License 2.0 | 6 votes |
def maybe_infer_freq(freq): """ Comparing a DateOffset to the string "infer" raises, so we need to be careful about comparisons. Make a dummy variable `freq_infer` to signify the case where the given freq is "infer" and set freq to None to avoid comparison trouble later on. Parameters ---------- freq : {DateOffset, None, str} Returns ------- freq : {DateOffset, None} freq_infer : bool """ freq_infer = False if not isinstance(freq, DateOffset): # if a passed freq is None, don't infer automatically if freq != 'infer': freq = frequencies.to_offset(freq) else: freq_infer = True freq = None return freq, freq_infer
Example #5
Source File: datetimes.py From recruit with Apache License 2.0 | 6 votes |
def snap(self, freq='S'): """ Snap time stamps to nearest occurring frequency """ # Superdumb, punting on any optimizing freq = to_offset(freq) snapped = np.empty(len(self), dtype=_NS_DTYPE) for i, v in enumerate(self): s = v if not freq.onOffset(s): t0 = freq.rollback(s) t1 = freq.rollforward(s) if abs(s - t0) < abs(t1 - s): s = t0 else: s = t1 snapped[i] = s # we know it conforms; skip check return DatetimeIndex._simple_new(snapped, freq=freq) # TODO: what about self.name? tz? if so, use shallow_copy?
Example #6
Source File: offsets.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def __eq__(self, other): if isinstance(other, compat.string_types): from pandas.tseries.frequencies import to_offset try: # GH#23524 if to_offset fails, we are dealing with an # incomparable type so == is False and != is True other = to_offset(other) except ValueError: # e.g. "infer" return False if isinstance(other, Tick): return self.delta == other.delta else: return False # This is identical to DateOffset.__hash__, but has to be redefined here # for Python 3, because we've redefined __eq__.
Example #7
Source File: test_frequencies.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def test_to_offset_negative(self): freqstr = '-1S' result = frequencies.to_offset(freqstr) assert (result.n == -1) freqstr = '-5min10s' result = frequencies.to_offset(freqstr) assert (result.n == -310) freqstr = '-2SM' result = frequencies.to_offset(freqstr) assert (result.n == -2) freqstr = '-1SMS' result = frequencies.to_offset(freqstr) assert (result.n == -1)
Example #8
Source File: test_frequencies.py From vnpy_crypto with MIT License | 6 votes |
def test_to_offset_negative(self): freqstr = '-1S' result = frequencies.to_offset(freqstr) assert (result.n == -1) freqstr = '-5min10s' result = frequencies.to_offset(freqstr) assert (result.n == -310) freqstr = '-2SM' result = frequencies.to_offset(freqstr) assert (result.n == -2) freqstr = '-1SMS' result = frequencies.to_offset(freqstr) assert (result.n == -1)
Example #9
Source File: datetimes.py From vnpy_crypto with MIT License | 6 votes |
def snap(self, freq='S'): """ Snap time stamps to nearest occurring frequency """ # Superdumb, punting on any optimizing freq = to_offset(freq) snapped = np.empty(len(self), dtype=_NS_DTYPE) for i, v in enumerate(self): s = v if not freq.onOffset(s): t0 = freq.rollback(s) t1 = freq.rollforward(s) if abs(s - t0) < abs(t1 - s): s = t0 else: s = t1 snapped[i] = s # we know it conforms; skip check return DatetimeIndex(snapped, freq=freq, verify_integrity=False)
Example #10
Source File: test_frequencies.py From recruit with Apache License 2.0 | 6 votes |
def test_to_offset_negative(self): freqstr = '-1S' result = frequencies.to_offset(freqstr) assert (result.n == -1) freqstr = '-5min10s' result = frequencies.to_offset(freqstr) assert (result.n == -310) freqstr = '-2SM' result = frequencies.to_offset(freqstr) assert (result.n == -2) freqstr = '-1SMS' result = frequencies.to_offset(freqstr) assert (result.n == -1)
Example #11
Source File: test_frequencies.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_to_offset_invalid(self): # GH 13930 with pytest.raises(ValueError, match='Invalid frequency: U1'): frequencies.to_offset('U1') with pytest.raises(ValueError, match='Invalid frequency: -U'): frequencies.to_offset('-U') with pytest.raises(ValueError, match='Invalid frequency: 3U1'): frequencies.to_offset('3U1') with pytest.raises(ValueError, match='Invalid frequency: -2-3U'): frequencies.to_offset('-2-3U') with pytest.raises(ValueError, match='Invalid frequency: -2D:3H'): frequencies.to_offset('-2D:3H') with pytest.raises(ValueError, match='Invalid frequency: 1.5.0S'): frequencies.to_offset('1.5.0S') # split offsets with spaces are valid assert frequencies.to_offset('2D 3H') == offsets.Hour(51) assert frequencies.to_offset('2 D3 H') == offsets.Hour(51) assert frequencies.to_offset('2 D 3 H') == offsets.Hour(51) assert frequencies.to_offset(' 2 D 3 H ') == offsets.Hour(51) assert frequencies.to_offset(' H ') == offsets.Hour() assert frequencies.to_offset(' 3 H ') == offsets.Hour(3) # special cases assert frequencies.to_offset('2SMS-15') == offsets.SemiMonthBegin(2) with pytest.raises(ValueError, match='Invalid frequency: 2SMS-15-15'): frequencies.to_offset('2SMS-15-15') with pytest.raises(ValueError, match='Invalid frequency: 2SMS-15D'): frequencies.to_offset('2SMS-15D')
Example #12
Source File: test_frequencies.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_to_offset_leading_plus(self): freqstr = '+1d' result = frequencies.to_offset(freqstr) assert (result.n == 1) freqstr = '+2h30min' result = frequencies.to_offset(freqstr) assert (result.n == 150) for bad_freq in ['+-1d', '-+1h', '+1', '-7', '+d', '-m']: with pytest.raises(ValueError, match='Invalid frequency:'): frequencies.to_offset(bad_freq)
Example #13
Source File: test_scalar_compat.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_round_int64(self, start, index_freq, periods, round_freq): dt = date_range(start=start, freq=index_freq, periods=periods) unit = to_offset(round_freq).nanos # test floor result = dt.floor(round_freq) diff = dt.asi8 - result.asi8 mod = result.asi8 % unit assert (mod == 0).all(), "floor not a {} multiple".format(round_freq) assert (0 <= diff).all() and (diff < unit).all(), "floor error" # test ceil result = dt.ceil(round_freq) diff = result.asi8 - dt.asi8 mod = result.asi8 % unit assert (mod == 0).all(), "ceil not a {} multiple".format(round_freq) assert (0 <= diff).all() and (diff < unit).all(), "ceil error" # test round result = dt.round(round_freq) diff = abs(result.asi8 - dt.asi8) mod = result.asi8 % unit assert (mod == 0).all(), "round not a {} multiple".format(round_freq) assert (diff <= unit // 2).all(), "round error" if unit % 2 == 0: assert ( result.asi8[diff == unit // 2] % 2 == 0 ).all(), "round half to even error" # ---------------------------------------------------------------- # DatetimeIndex.normalize
Example #14
Source File: test_frequencies.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_rule_aliases(): rule = frequencies.to_offset('10us') assert rule == offsets.Micro(10)
Example #15
Source File: test_frequencies.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_to_offset_pd_timedelta(self): # Tests for #9064 td = Timedelta(days=1, seconds=1) result = frequencies.to_offset(td) expected = offsets.Second(86401) assert (expected == result) td = Timedelta(days=-1, seconds=1) result = frequencies.to_offset(td) expected = offsets.Second(-86399) assert (expected == result) td = Timedelta(hours=1, minutes=10) result = frequencies.to_offset(td) expected = offsets.Minute(70) assert (expected == result) td = Timedelta(hours=1, minutes=-10) result = frequencies.to_offset(td) expected = offsets.Minute(50) assert (expected == result) td = Timedelta(weeks=1) result = frequencies.to_offset(td) expected = offsets.Day(7) assert (expected == result) td1 = Timedelta(hours=1) result1 = frequencies.to_offset(td1) result2 = frequencies.to_offset('60min') assert (result1 == result2) td = Timedelta(microseconds=1) result = frequencies.to_offset(td) expected = offsets.Micro(1) assert (expected == result) td = Timedelta(microseconds=0) pytest.raises(ValueError, lambda: frequencies.to_offset(td))
Example #16
Source File: resample.py From vnpy_crypto with MIT License | 5 votes |
def asfreq(obj, freq, method=None, how=None, normalize=False, fill_value=None): """ Utility frequency conversion method for Series/DataFrame """ if isinstance(obj.index, PeriodIndex): if method is not None: raise NotImplementedError("'method' argument is not supported") if how is None: how = 'E' new_obj = obj.copy() new_obj.index = obj.index.asfreq(freq, how=how) elif len(obj.index) == 0: new_obj = obj.copy() new_obj.index = obj.index._shallow_copy(freq=to_offset(freq)) else: dti = date_range(obj.index[0], obj.index[-1], freq=freq) dti.name = obj.index.name new_obj = obj.reindex(dti, method=method, fill_value=fill_value) if normalize: new_obj.index = new_obj.index.normalize() return new_obj
Example #17
Source File: resample.py From vnpy_crypto with MIT License | 5 votes |
def _get_range_edges(first, last, offset, closed='left', base=0): if isinstance(offset, compat.string_types): offset = to_offset(offset) if isinstance(offset, Tick): is_day = isinstance(offset, Day) day_nanos = delta_to_nanoseconds(timedelta(1)) # #1165 if (is_day and day_nanos % offset.nanos == 0) or not is_day: return _adjust_dates_anchored(first, last, offset, closed=closed, base=base) if not isinstance(offset, Tick): # and first.time() != last.time(): # hack! first = first.normalize() last = last.normalize() if closed == 'left': first = Timestamp(offset.rollback(first)) else: first = Timestamp(first - offset) last = Timestamp(last + offset) return first, last
Example #18
Source File: resample.py From vnpy_crypto with MIT License | 5 votes |
def _upsample(self, method, limit=None, fill_value=None): """ method : string {'backfill', 'bfill', 'pad', 'ffill', 'asfreq'} method for upsampling limit : int, default None Maximum size gap to fill when reindexing fill_value : scalar, default None Value to use for missing values See also -------- .fillna """ self._set_binner() if self.axis: raise AssertionError('axis must be 0') if self._from_selection: raise ValueError("Upsampling from level= or on= selection" " is not supported, use .set_index(...)" " to explicitly set index to" " datetime-like") ax = self.ax obj = self._selected_obj binner = self.binner res_index = self._adjust_binner_for_upsample(binner) # if we have the same frequency as our axis, then we are equal sampling if limit is None and to_offset(ax.inferred_freq) == self.freq: result = obj.copy() result.index = res_index else: result = obj.reindex(res_index, method=method, limit=limit, fill_value=fill_value) result = self._apply_loffset(result) return self._wrap_result(result)
Example #19
Source File: resample.py From Computable with MIT License | 5 votes |
def __init__(self, freq='Min', closed=None, label=None, how='mean', nperiods=None, axis=0, fill_method=None, limit=None, loffset=None, kind=None, convention=None, base=0): self.freq = to_offset(freq) end_types = set(['M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W']) rule = self.freq.rule_code if (rule in end_types or ('-' in rule and rule[:rule.find('-')] in end_types)): if closed is None: closed = 'right' if label is None: label = 'right' else: if closed is None: closed = 'left' if label is None: label = 'left' self.closed = closed self.label = label self.nperiods = nperiods self.kind = kind self.convention = convention or 'E' self.convention = self.convention.lower() self.axis = axis self.loffset = loffset self.how = how self.fill_method = fill_method self.limit = limit self.base = base
Example #20
Source File: test_unary_ops.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_round_int64(self, timestamp, freq): """check that all rounding modes are accurate to int64 precision see GH#22591 """ dt = Timestamp(timestamp) unit = to_offset(freq).nanos # test floor result = dt.floor(freq) assert result.value % unit == 0, "floor not a {} multiple".format(freq) assert 0 <= dt.value - result.value < unit, "floor error" # test ceil result = dt.ceil(freq) assert result.value % unit == 0, "ceil not a {} multiple".format(freq) assert 0 <= result.value - dt.value < unit, "ceil error" # test round result = dt.round(freq) assert result.value % unit == 0, "round not a {} multiple".format(freq) assert abs(result.value - dt.value) <= unit // 2, "round error" if unit % 2 == 0 and abs(result.value - dt.value) == unit // 2: # round half to even assert result.value // unit % 2 == 0, "round half to even error" # -------------------------------------------------------------- # Timestamp.replace
Example #21
Source File: resample.py From vnpy_crypto with MIT License | 5 votes |
def _wrap_result(self, result): """ potentially wrap any results """ if isinstance(result, ABCSeries) and self._selection is not None: result.name = self._selection if isinstance(result, ABCSeries) and result.empty: obj = self.obj result.index = obj.index._shallow_copy(freq=to_offset(self.freq)) result.name = getattr(obj, 'name', None) return result
Example #22
Source File: dtypes.py From vnpy_crypto with MIT License | 5 votes |
def _parse_dtype_strict(cls, freq): if isinstance(freq, compat.string_types): if freq.startswith('period[') or freq.startswith('Period['): m = cls._match.search(freq) if m is not None: freq = m.group('freq') from pandas.tseries.frequencies import to_offset freq = to_offset(freq) if freq is not None: return freq raise ValueError("could not construct PeriodDtype")
Example #23
Source File: period.py From vnpy_crypto with MIT License | 5 votes |
def _add_delta_td(self, other): assert isinstance(other, (timedelta, np.timedelta64, Tick)) nanos = delta_to_nanoseconds(other) own_offset = frequencies.to_offset(self.freq.rule_code) if isinstance(own_offset, Tick): offset_nanos = delta_to_nanoseconds(own_offset) if np.all(nanos % offset_nanos == 0): return self.shift(nanos // offset_nanos) # raise when input doesn't have freq raise IncompatibleFrequency("Input has different freq from " "{cls}(freq={freqstr})" .format(cls=type(self).__name__, freqstr=self.freqstr))
Example #24
Source File: period.py From vnpy_crypto with MIT License | 5 votes |
def _maybe_convert_timedelta(self, other): if isinstance( other, (timedelta, np.timedelta64, Tick, np.ndarray)): offset = frequencies.to_offset(self.freq.rule_code) if isinstance(offset, Tick): if isinstance(other, np.ndarray): nanos = np.vectorize(delta_to_nanoseconds)(other) else: nanos = delta_to_nanoseconds(other) offset_nanos = delta_to_nanoseconds(offset) check = np.all(nanos % offset_nanos == 0) if check: return nanos // offset_nanos elif isinstance(other, DateOffset): freqstr = other.rule_code base = frequencies.get_base_alias(freqstr) if base == self.freq.rule_code: return other.n msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) elif is_integer(other): # integer is passed to .shift via # _add_datetimelike_methods basically # but ufunc may pass integer to _add_delta return other # raise when input doesn't have freq msg = "Input has different freq from PeriodIndex(freq={0})" raise IncompatibleFrequency(msg.format(self.freqstr))
Example #25
Source File: timedeltas.py From vnpy_crypto with MIT License | 5 votes |
def _maybe_cast_slice_bound(self, label, side, kind): """ If label is a string, cast it to timedelta according to resolution. Parameters ---------- label : object side : {'left', 'right'} kind : {'ix', 'loc', 'getitem'} Returns ------- label : object """ assert kind in ['ix', 'loc', 'getitem', None] if isinstance(label, compat.string_types): parsed = _coerce_scalar_to_timedelta_type(label, box=True) lbound = parsed.round(parsed.resolution) if side == 'left': return lbound else: return (lbound + to_offset(parsed.resolution) - Timedelta(1, 'ns')) elif ((is_integer(label) or is_float(label)) and not is_timedelta64_dtype(label)): self._invalid_indexer('slice', label) return label
Example #26
Source File: timedeltas.py From vnpy_crypto with MIT License | 5 votes |
def union(self, other): """ Specialized union for TimedeltaIndex objects. If combine overlapping ranges with the same DateOffset, will be much faster than Index.union Parameters ---------- other : TimedeltaIndex or array-like Returns ------- y : Index or TimedeltaIndex """ self._assert_can_do_setop(other) if not isinstance(other, TimedeltaIndex): try: other = TimedeltaIndex(other) except (TypeError, ValueError): pass this, other = self, other if this._can_fast_union(other): return this._fast_union(other) else: result = Index.union(this, other) if isinstance(result, TimedeltaIndex): if result.freq is None: result.freq = to_offset(result.inferred_freq) return result
Example #27
Source File: datetimelike.py From vnpy_crypto with MIT License | 5 votes |
def shift(self, n, freq=None): """ Specialized shift which produces a DatetimeIndex Parameters ---------- n : int Periods to shift by freq : DateOffset or timedelta-like, optional Returns ------- shifted : DatetimeIndex """ if freq is not None and freq != self.freq: if isinstance(freq, compat.string_types): freq = frequencies.to_offset(freq) offset = n * freq result = self + offset if hasattr(self, 'tz'): result._tz = self.tz return result if n == 0: # immutable so OK return self if self.freq is None: raise NullFrequencyError("Cannot shift with no freq") start = self[0] + n * self.freq end = self[-1] + n * self.freq attribs = self._get_attributes_dict() attribs['start'] = start attribs['end'] = end return type(self)(**attribs)
Example #28
Source File: datetimelike.py From vnpy_crypto with MIT License | 5 votes |
def freq(self, value): if value is not None: value = frequencies.to_offset(value) self._validate_frequency(self, value) self._freq = value
Example #29
Source File: datetimes.py From vnpy_crypto with MIT License | 5 votes |
def union(self, other): """ Specialized union for DatetimeIndex objects. If combine overlapping ranges with the same DateOffset, will be much faster than Index.union Parameters ---------- other : DatetimeIndex or array-like Returns ------- y : Index or DatetimeIndex """ self._assert_can_do_setop(other) if not isinstance(other, DatetimeIndex): try: other = DatetimeIndex(other) except TypeError: pass this, other = self._maybe_utc_convert(other) if this._can_fast_union(other): return this._fast_union(other) else: result = Index.union(this, other) if isinstance(result, DatetimeIndex): result._tz = timezones.tz_standardize(this.tz) if (result.freq is None and (this.freq is not None or other.freq is not None)): result.freq = to_offset(result.inferred_freq) return result
Example #30
Source File: window.py From vnpy_crypto with MIT License | 5 votes |
def _validate_freq(self): """ validate & return window frequency """ from pandas.tseries.frequencies import to_offset try: return to_offset(self.window) except (TypeError, ValueError): raise ValueError("passed window {0} is not " "compatible with a datetimelike " "index".format(self.window))