本文整理汇总了Python中pandas.interval_range方法的典型用法代码示例。如果您正苦于以下问题:Python pandas.interval_range方法的具体用法?Python pandas.interval_range怎么用?Python pandas.interval_range使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas
的用法示例。
在下文中一共展示了pandas.interval_range方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_interval_index
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import interval_range [as 别名]
def test_interval_index(self):
# GH 19977
index = pd.interval_range(start=0, periods=3)
df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
index=index,
columns=['A', 'B', 'C'])
expected = 1
result = df.loc[0.5, 'A']
assert_almost_equal(result, expected)
index = pd.interval_range(start=0, periods=3, closed='both')
df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
index=index,
columns=['A', 'B', 'C'])
index_exp = pd.interval_range(start=0, periods=2,
freq=1, closed='both')
expected = pd.Series([1, 4], index=index_exp, name='A')
result = df.loc[1, 'A']
assert_series_equal(result, expected)
示例2: test_cython_agg_empty_buckets_nanops
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import interval_range [as 别名]
def test_cython_agg_empty_buckets_nanops(observed):
# GH-18869 can't call nanops on empty groups, so hardcode expected
# for these
df = pd.DataFrame([11, 12, 13], columns=['a'])
grps = range(0, 25, 5)
# add / sum
result = df.groupby(pd.cut(df['a'], grps),
observed=observed)._cython_agg_general('add')
intervals = pd.interval_range(0, 20, freq=5)
expected = pd.DataFrame(
{"a": [0, 0, 36, 0]},
index=pd.CategoricalIndex(intervals, name='a', ordered=True))
if observed:
expected = expected[expected.a != 0]
tm.assert_frame_equal(result, expected)
# prod
result = df.groupby(pd.cut(df['a'], grps),
observed=observed)._cython_agg_general('prod')
expected = pd.DataFrame(
{"a": [1, 1, 1716, 1]},
index=pd.CategoricalIndex(intervals, name='a', ordered=True))
if observed:
expected = expected[expected.a != 1]
tm.assert_frame_equal(result, expected)
示例3: test_interval_array_equal
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import interval_range [as 别名]
def test_interval_array_equal(kwargs):
arr = interval_range(**kwargs).values
assert_interval_array_equal(arr, arr)
示例4: test_interval_array_equal_closed_mismatch
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import interval_range [as 别名]
def test_interval_array_equal_closed_mismatch():
kwargs = dict(start=0, periods=5)
arr1 = interval_range(closed="left", **kwargs).values
arr2 = interval_range(closed="right", **kwargs).values
msg = """\
IntervalArray are different
Attribute "closed" are different
\\[left\\]: left
\\[right\\]: right"""
with pytest.raises(AssertionError, match=msg):
assert_interval_array_equal(arr1, arr2)
示例5: test_interval_array_equal_periods_mismatch
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import interval_range [as 别名]
def test_interval_array_equal_periods_mismatch():
kwargs = dict(start=0)
arr1 = interval_range(periods=5, **kwargs).values
arr2 = interval_range(periods=6, **kwargs).values
msg = """\
IntervalArray.left are different
IntervalArray.left length are different
\\[left\\]: 5, Int64Index\\(\\[0, 1, 2, 3, 4\\], dtype='int64'\\)
\\[right\\]: 6, Int64Index\\(\\[0, 1, 2, 3, 4, 5\\], dtype='int64'\\)"""
with pytest.raises(AssertionError, match=msg):
assert_interval_array_equal(arr1, arr2)
示例6: test_interval_array_equal_start_mismatch
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import interval_range [as 别名]
def test_interval_array_equal_start_mismatch():
kwargs = dict(periods=4)
arr1 = interval_range(start=0, **kwargs).values
arr2 = interval_range(start=1, **kwargs).values
msg = """\
IntervalArray.left are different
IntervalArray.left values are different \\(100.0 %\\)
\\[left\\]: Int64Index\\(\\[0, 1, 2, 3\\], dtype='int64'\\)
\\[right\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)"""
with pytest.raises(AssertionError, match=msg):
assert_interval_array_equal(arr1, arr2)
示例7: bin_spikes
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import interval_range [as 别名]
def bin_spikes(spikes, binsize, interval_indices=False):
"""
Wrapper for bincount2D which is intended to take in a TimeSeries object of spike times
and cluster identities and spit out spike counts in bins of a specified width binsize, also in
another TimeSeries object. Can either return a TS object with each row labeled with the
corresponding interval or the value of the left edge of the bin.
:param spikes: Spike times and cluster identities of sorted spikes
:type spikes: TimeSeries object with \'clusters\' column and timestamps
:param binsize: Width of the non-overlapping bins in which to bin spikes
:type binsize: float
:param interval_indices: Whether to use intervals as the time stamps for binned spikes, rather
than the left edge value of the bins, defaults to False
:type interval_indices: bool, optional
:return: Object with 2D array of shape T x N, for T timesteps and N clusters, and the
associated time stamps.
:rtype: TimeSeries object
"""
if type(spikes) is not core.TimeSeries:
raise TypeError('Input spikes need to be in TimeSeries object format')
if not hasattr(spikes, 'clusters'):
raise AttributeError('Input spikes need to have a clusters attribute. Make sure you set '
'columns=(\'clusters\',)) when constructing spikes.')
rates, tbins, clusters = bincount2D(spikes.times, spikes.clusters, binsize)
if interval_indices:
intervals = pd.interval_range(tbins[0], tbins[-1], freq=binsize, closed='left')
return core.TimeSeries(times=intervals, values=rates.T[:-1], columns=clusters)
else:
return core.TimeSeries(times=tbins, values=rates.T, columns=clusters)
示例8: histogram_plot
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import interval_range [as 别名]
def histogram_plot(nx, ny, x, source, xlo=None, xhi=None, nsub=5, output_backend=DEFAULT_BACKEND):
if not present(source, x): return None
xlo, xhi = source[x].min() if xlo is None else xlo, source[x].max() if xhi is None else xhi
bins = pd.interval_range(start=xlo, end=xhi, periods=nsub * (xhi - xlo), closed='left')
c = [palettes.Inferno[int(xhi - xlo + 1)][int(b.left - xlo)] for b in bins]
hrz_categorized = pd.cut(source[x], bins)
counts = hrz_categorized.groupby(hrz_categorized).count()
f = figure(output_backend=output_backend, plot_width=nx, plot_height=ny, x_range=Range1d(start=xlo, end=xhi),
x_axis_label=x)
f.quad(left=counts.index.categories.left, right=counts.index.categories.right, top=counts, bottom=0,
color=c, fill_alpha=0.2)
f.toolbar_location = None
f.yaxis.visible = False
return f
示例9: test_construction_from_numeric
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import interval_range [as 别名]
def test_construction_from_numeric(self, closed):
# combinations of start/end/periods without freq
expected = IntervalIndex.from_breaks(
np.arange(0, 6), name='foo', closed=closed)
result = interval_range(start=0, end=5, name='foo', closed=closed)
tm.assert_index_equal(result, expected)
result = interval_range(start=0, periods=5, name='foo', closed=closed)
tm.assert_index_equal(result, expected)
result = interval_range(end=5, periods=5, name='foo', closed=closed)
tm.assert_index_equal(result, expected)
# combinations of start/end/periods with freq
expected = IntervalIndex.from_tuples([(0, 2), (2, 4), (4, 6)],
name='foo', closed=closed)
result = interval_range(start=0, end=6, freq=2, name='foo',
closed=closed)
tm.assert_index_equal(result, expected)
result = interval_range(start=0, periods=3, freq=2, name='foo',
closed=closed)
tm.assert_index_equal(result, expected)
result = interval_range(end=6, periods=3, freq=2, name='foo',
closed=closed)
tm.assert_index_equal(result, expected)
# output truncates early if freq causes end to be skipped.
expected = IntervalIndex.from_tuples([(0.0, 1.5), (1.5, 3.0)],
name='foo', closed=closed)
result = interval_range(start=0, end=4, freq=1.5, name='foo',
closed=closed)
tm.assert_index_equal(result, expected)
示例10: test_construction_from_timedelta
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import interval_range [as 别名]
def test_construction_from_timedelta(self, closed):
# combinations of start/end/periods without freq
start, end = Timedelta('1 day'), Timedelta('6 days')
breaks = timedelta_range(start=start, end=end)
expected = IntervalIndex.from_breaks(breaks, name='foo', closed=closed)
result = interval_range(start=start, end=end, name='foo',
closed=closed)
tm.assert_index_equal(result, expected)
result = interval_range(start=start, periods=5, name='foo',
closed=closed)
tm.assert_index_equal(result, expected)
result = interval_range(end=end, periods=5, name='foo',
closed=closed)
tm.assert_index_equal(result, expected)
# combinations of start/end/periods with fixed freq
freq = '2D'
start, end = Timedelta('1 day'), Timedelta('7 days')
breaks = timedelta_range(start=start, end=end, freq=freq)
expected = IntervalIndex.from_breaks(breaks, name='foo', closed=closed)
result = interval_range(start=start, end=end, freq=freq, name='foo',
closed=closed)
tm.assert_index_equal(result, expected)
result = interval_range(start=start, periods=3, freq=freq, name='foo',
closed=closed)
tm.assert_index_equal(result, expected)
result = interval_range(end=end, periods=3, freq=freq, name='foo',
closed=closed)
tm.assert_index_equal(result, expected)
# output truncates early if freq causes end to be skipped.
end = Timedelta('7 days 1 hour')
result = interval_range(start=start, end=end, freq=freq, name='foo',
closed=closed)
tm.assert_index_equal(result, expected)
示例11: test_constructor_coverage
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import interval_range [as 别名]
def test_constructor_coverage(self):
# float value for periods
expected = pd.interval_range(start=0, periods=10)
result = pd.interval_range(start=0, periods=10.5)
tm.assert_index_equal(result, expected)
# equivalent timestamp-like start/end
start, end = Timestamp('2017-01-01'), Timestamp('2017-01-15')
expected = pd.interval_range(start=start, end=end)
result = pd.interval_range(start=start.to_pydatetime(),
end=end.to_pydatetime())
tm.assert_index_equal(result, expected)
result = pd.interval_range(start=start.tz_localize('UTC'),
end=end.tz_localize('UTC'))
tm.assert_index_equal(result, expected)
result = pd.interval_range(start=start.asm8, end=end.asm8)
tm.assert_index_equal(result, expected)
# equivalent freq with timestamp
equiv_freq = ['D', Day(), Timedelta(days=1), timedelta(days=1),
DateOffset(days=1)]
for freq in equiv_freq:
result = pd.interval_range(start=start, end=end, freq=freq)
tm.assert_index_equal(result, expected)
# equivalent timedelta-like start/end
start, end = Timedelta(days=1), Timedelta(days=10)
expected = pd.interval_range(start=start, end=end)
result = pd.interval_range(start=start.to_pytimedelta(),
end=end.to_pytimedelta())
tm.assert_index_equal(result, expected)
result = pd.interval_range(start=start.asm8, end=end.asm8)
tm.assert_index_equal(result, expected)
# equivalent freq with timedelta
equiv_freq = ['D', Day(), Timedelta(days=1), timedelta(days=1)]
for freq in equiv_freq:
result = pd.interval_range(start=start, end=end, freq=freq)
tm.assert_index_equal(result, expected)