Python pandas.interval_range() Examples
The following are 19
code examples of pandas.interval_range().
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
, or try the search function
.
Example #1
Source File: test_indexing.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #2
Source File: test_indexing.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
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)
Example #3
Source File: test_assert_interval_array_equal.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_interval_array_equal(kwargs): arr = interval_range(**kwargs).values assert_interval_array_equal(arr, arr)
Example #4
Source File: test_cython.py From twitter-stock-recommendation with MIT License | 5 votes |
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)
Example #5
Source File: test_interval.py From elasticintel with GNU General Public License v3.0 | 5 votes |
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)
Example #6
Source File: test_interval.py From elasticintel with GNU General Public License v3.0 | 5 votes |
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)
Example #7
Source File: line.py From choochoo with GNU General Public License v2.0 | 5 votes |
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
Example #8
Source File: test_assert_interval_array_equal.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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)
Example #9
Source File: test_assert_interval_array_equal.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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)
Example #10
Source File: test_assert_interval_array_equal.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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)
Example #11
Source File: test_assert_interval_array_equal.py From recruit with Apache License 2.0 | 5 votes |
def test_interval_array_equal(kwargs): arr = interval_range(**kwargs).values assert_interval_array_equal(arr, arr)
Example #12
Source File: test_cython.py From recruit with Apache License 2.0 | 5 votes |
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)
Example #13
Source File: test_cython.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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)
Example #14
Source File: test_cython.py From vnpy_crypto with MIT License | 5 votes |
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)
Example #15
Source File: processing.py From ibllib with MIT License | 5 votes |
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)
Example #16
Source File: test_assert_interval_array_equal.py From recruit with Apache License 2.0 | 5 votes |
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)
Example #17
Source File: test_assert_interval_array_equal.py From recruit with Apache License 2.0 | 5 votes |
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)
Example #18
Source File: test_assert_interval_array_equal.py From recruit with Apache License 2.0 | 5 votes |
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)
Example #19
Source File: test_interval.py From elasticintel with GNU General Public License v3.0 | 4 votes |
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)