本文整理汇总了Python中pandas.tseries.frequencies.to_offset方法的典型用法代码示例。如果您正苦于以下问题:Python frequencies.to_offset方法的具体用法?Python frequencies.to_offset怎么用?Python frequencies.to_offset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.tseries.frequencies
的用法示例。
在下文中一共展示了frequencies.to_offset方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __eq__
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
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__.
示例2: test_to_offset_negative
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
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)
示例3: maybe_infer_freq
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
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
示例4: _from_sequence
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
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
示例5: snap
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
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?
示例6: snap
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
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)
示例7: _get_range_edges
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
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
示例8: __ne__
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
def __ne__(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 True
if isinstance(other, Tick):
return self.delta != other.delta
else:
return True
示例9: test_round_int64
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
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
示例10: test_to_offset_invalid
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
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')
示例11: test_to_offset_leading_plus
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
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)
示例12: test_to_offset_pd_timedelta
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
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))
示例13: test_rule_aliases
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
def test_rule_aliases():
rule = frequencies.to_offset('10us')
assert rule == offsets.Micro(10)
示例14: test_frequency_misc
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
def test_frequency_misc(self):
assert (resolution.get_freq_group('T') ==
FreqGroup.FR_MIN)
code, stride = get_freq_code(offsets.Hour())
assert code == FreqGroup.FR_HR
code, stride = get_freq_code((5, 'T'))
assert code == 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 pytest.raises(ValueError, match='Invalid frequency'):
get_freq_code((5, 'baz'))
with pytest.raises(ValueError, match='Invalid frequency'):
frequencies.to_offset('100foo')
with pytest.raises(ValueError, match='Could not evaluate'):
frequencies.to_offset(('', ''))
示例15: test_round_int64
# 需要导入模块: from pandas.tseries import frequencies [as 别名]
# 或者: from pandas.tseries.frequencies import to_offset [as 别名]
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