本文整理匯總了Python中pandas.Interval方法的典型用法代碼示例。如果您正苦於以下問題:Python pandas.Interval方法的具體用法?Python pandas.Interval怎麽用?Python pandas.Interval使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas
的用法示例。
在下文中一共展示了pandas.Interval方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_overlaps_interval
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_overlaps_interval(
self, constructor, start_shift, closed, other_closed):
start, shift = start_shift
interval = Interval(start, start + 3 * shift, other_closed)
# intervals: identical, nested, spanning, partial, adjacent, disjoint
tuples = [(start, start + 3 * shift),
(start + shift, start + 2 * shift),
(start - shift, start + 4 * shift),
(start + 2 * shift, start + 4 * shift),
(start + 3 * shift, start + 4 * shift),
(start + 4 * shift, start + 5 * shift)]
interval_container = constructor.from_tuples(tuples, closed)
adjacent = (interval.closed_right and interval_container.closed_left)
expected = np.array([True, True, True, True, adjacent, False])
result = interval_container.overlaps(interval)
tm.assert_numpy_array_equal(result, expected)
示例2: test_is_unique_interval
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_is_unique_interval(self, closed):
"""
Interval specific tests for is_unique in addition to base class tests
"""
# unique overlapping - distinct endpoints
idx = IntervalIndex.from_tuples([(0, 1), (0.5, 1.5)], closed=closed)
assert idx.is_unique is True
# unique overlapping - shared endpoints
idx = pd.IntervalIndex.from_tuples(
[(1, 2), (1, 3), (2, 3)], closed=closed)
assert idx.is_unique is True
# unique nested
idx = IntervalIndex.from_tuples([(-1, 1), (-2, 2)], closed=closed)
assert idx.is_unique is True
示例3: test_get_item
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_get_item(self, closed):
i = IntervalIndex.from_arrays((0, 1, np.nan), (1, 2, np.nan),
closed=closed)
assert i[0] == Interval(0.0, 1.0, closed=closed)
assert i[1] == Interval(1.0, 2.0, closed=closed)
assert isna(i[2])
result = i[0:1]
expected = IntervalIndex.from_arrays((0.,), (1.,), closed=closed)
tm.assert_index_equal(result, expected)
result = i[0:2]
expected = IntervalIndex.from_arrays((0., 1), (1., 2.), closed=closed)
tm.assert_index_equal(result, expected)
result = i[1:3]
expected = IntervalIndex.from_arrays((1., np.nan), (2., np.nan),
closed=closed)
tm.assert_index_equal(result, expected)
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
示例4: test_get_loc_datetimelike_nonoverlapping
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_get_loc_datetimelike_nonoverlapping(self, breaks):
# GH 20636
# nonoverlapping = IntervalIndex method and no i8 conversion
index = IntervalIndex.from_breaks(breaks)
value = index[0].mid
result = index.get_loc(value)
expected = 0
assert result == expected
interval = Interval(index[0].left, index[1].right)
result = index.get_loc(interval)
expected = slice(0, 2)
assert result == expected
# Make consistent with test_interval_new.py (see #16316, #16386)
示例5: test_get_loc_datetimelike_overlapping
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_get_loc_datetimelike_overlapping(self, arrays):
# GH 20636
# overlapping = IntervalTree method with i8 conversion
index = IntervalIndex.from_arrays(*arrays)
value = index[0].mid + Timedelta('12 hours')
result = np.sort(index.get_loc(value))
expected = np.array([0, 1], dtype='intp')
assert tm.assert_numpy_array_equal(result, expected)
interval = Interval(index[0].left, index[1].right)
result = np.sort(index.get_loc(interval))
expected = np.array([0, 1, 2], dtype='intp')
assert tm.assert_numpy_array_equal(result, expected)
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
示例6: test_contains
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_contains(self):
# Only endpoints are valid.
i = IntervalIndex.from_arrays([0, 1], [1, 2])
# Invalid
assert 0 not in i
assert 1 not in i
assert 2 not in i
# Valid
assert Interval(0, 1) in i
assert Interval(0, 2) in i
assert Interval(0, 0.5) in i
assert Interval(3, 5) not in i
assert Interval(-1, 0, closed='left') not in i
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
示例7: testcontains
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def testcontains(self):
# can select values that are IN the range of a value
i = IntervalIndex.from_arrays([0, 1], [1, 2])
assert i.contains(0.1)
assert i.contains(0.5)
assert i.contains(1)
assert i.contains(Interval(0, 1))
assert i.contains(Interval(0, 2))
# these overlaps completely
assert i.contains(Interval(0, 3))
assert i.contains(Interval(1, 3))
assert not i.contains(20)
assert not i.contains(-20)
示例8: test_sort_values
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_sort_values(self, closed):
index = self.create_index(closed=closed)
result = index.sort_values()
tm.assert_index_equal(result, index)
result = index.sort_values(ascending=False)
tm.assert_index_equal(result, index[::-1])
# with nan
index = IntervalIndex([Interval(1, 2), np.nan, Interval(0, 1)])
result = index.sort_values()
expected = IntervalIndex([Interval(0, 1), Interval(1, 2), np.nan])
tm.assert_index_equal(result, expected)
result = index.sort_values(ascending=False)
expected = IntervalIndex([np.nan, Interval(1, 2), Interval(0, 1)])
tm.assert_index_equal(result, expected)
示例9: test_get_loc_interval
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_get_loc_interval(self, closed, side):
idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=closed)
for bound in [[0, 1], [1, 2], [2, 3], [3, 4],
[0, 2], [2.5, 3], [-1, 4]]:
# if get_loc is supplied an interval, it should only search
# for exact matches, not overlaps or covers, else KeyError.
if closed == side:
if bound == [0, 1]:
assert idx.get_loc(Interval(0, 1, closed=side)) == 0
elif bound == [2, 3]:
assert idx.get_loc(Interval(2, 3, closed=side)) == 1
else:
with pytest.raises(KeyError):
idx.get_loc(Interval(*bound, closed=side))
else:
with pytest.raises(KeyError):
idx.get_loc(Interval(*bound, closed=side))
示例10: test_contains
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_contains(self):
index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right')
# __contains__ requires perfect matches to intervals.
assert 0 not in index
assert 1 not in index
assert 2 not in index
assert Interval(0, 1, closed='right') in index
assert Interval(0, 2, closed='right') not in index
assert Interval(0, 0.5, closed='right') not in index
assert Interval(3, 5, closed='right') not in index
assert Interval(-1, 0, closed='left') not in index
assert Interval(0, 1, closed='left') not in index
assert Interval(0, 1, closed='both') not in index
示例11: test_contains_method
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_contains_method(self):
index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right')
assert not index.contains(0)
assert index.contains(0.1)
assert index.contains(0.5)
assert index.contains(1)
assert index.contains(Interval(0, 1, closed='right'))
assert not index.contains(Interval(0, 1, closed='left'))
assert not index.contains(Interval(0, 1, closed='both'))
assert not index.contains(Interval(0, 2, closed='right'))
assert not index.contains(Interval(0, 3, closed='right'))
assert not index.contains(Interval(1, 3, closed='right'))
assert not index.contains(20)
assert not index.contains(-20)
示例12: test_with_slices
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_with_slices(self):
s = self.s
# slice of interval
with pytest.raises(NotImplementedError):
s.loc[Interval(3, 6):]
with pytest.raises(NotImplementedError):
s[Interval(3, 6):]
expected = s.iloc[3:5]
result = s[[Interval(3, 6)]]
tm.assert_series_equal(expected, result)
# slice of scalar with step != 1
with pytest.raises(ValueError):
s[0:4:2]
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
示例13: test_non_unique_moar
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_non_unique_moar(self):
idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)])
s = Series(range(len(idx)), index=idx)
result = s.loc[Interval(1, 3)]
expected = s.iloc[[0, 1]]
tm.assert_series_equal(expected, result)
# non-unique index and slices not allowed
with pytest.raises(ValueError):
s.loc[Interval(1, 3):]
with pytest.raises(ValueError):
s[Interval(1, 3):]
# non-unique
with pytest.raises(ValueError):
s[[Interval(1, 3)]]
# TODO: check this behavior is consistent with test_interval_new.py
示例14: test_non_unique_moar
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_non_unique_moar(self):
idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)])
s = Series(range(len(idx)), index=idx)
expected = s.iloc[[0, 1]]
result = s.loc[Interval(1, 3)]
tm.assert_series_equal(expected, result)
expected = s
result = s.loc[Interval(1, 3):]
tm.assert_series_equal(expected, result)
expected = s
result = s[Interval(1, 3):]
tm.assert_series_equal(expected, result)
expected = s.iloc[[0, 1]]
result = s[[Interval(1, 3)]]
tm.assert_series_equal(expected, result)
示例15: test_contains
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import Interval [as 別名]
def test_contains(self, interval):
assert 0.5 in interval
assert 1 in interval
assert 0 not in interval
msg = "__contains__ not defined for two intervals"
with pytest.raises(TypeError, match=msg):
interval in interval
interval_both = Interval(0, 1, closed='both')
assert 0 in interval_both
assert 1 in interval_both
interval_neither = Interval(0, 1, closed='neither')
assert 0 not in interval_neither
assert 0.5 in interval_neither
assert 1 not in interval_neither