def test_join_right(self): other = Int64Index([7, 12, 25, 1, 2, 5]) other_mono = Int64Index([1, 2, 5, 7, 12, 25]) # not monotonic res, lidx, ridx = self.index.join(other, how='right', return_indexers=True) eres = other elidx = np.array([-1, 6, -1, -1, 1, -1], dtype='i4') self.assert_(isinstance(other, Int64Index)) self.assert_(res.equals(eres)) self.assert_(np.array_equal(lidx, elidx)) self.assert_(ridx is None) # monotonic res, lidx, ridx = self.index.join(other_mono, how='right', return_indexers=True) eres = other_mono elidx = np.array([-1, 1, -1, -1, 6, -1], dtype='i4') self.assert_(isinstance(other, Int64Index)) self.assert_(res.equals(eres)) self.assert_(np.array_equal(lidx, elidx)) self.assert_(ridx is None)
def test_constructor(self): # pass list, coerce fine index = Int64Index([-5, 0, 1, 2]) expected = np.array([-5, 0, 1, 2], dtype=np.int64) self.assert_(np.array_equal(index, expected)) # from iterable index = Int64Index(iter([-5, 0, 1, 2])) self.assert_(np.array_equal(index, expected)) # scalar raise Exception self.assertRaises(ValueError, Int64Index, 5)
def test_constructor_corner(self): arr = np.array([1, 2, 3, 4], dtype=object) index = Int64Index(arr) self.assert_(index.values.dtype == np.int64) self.assert_(index.equals(arr)) # preventing casting arr = np.array([1, '2', 3, '4'], dtype=object) self.assertRaises(TypeError, Int64Index, arr)
def test_ensure_platform_int(): # verify that when we create certain types of indices # they remain the correct type under platform conversions from pandas.core.index import Int64Index # int64 x = Int64Index([1, 2, 3], dtype='int64') assert (x.dtype == np.int64) pi = com._ensure_platform_int(x) assert (pi.dtype == np.int_) # int32 x = Int64Index([1, 2, 3], dtype='int32') assert (x.dtype == np.int32) pi = com._ensure_platform_int(x) assert (pi.dtype == np.int_)
def unique(self): """ Index.unique with handling for DatetimeIndex/PeriodIndex metadata Returns ------- result : DatetimeIndex or PeriodIndex """ from pandas.core.index import Int64Index result = Int64Index.unique(self) return self._simple_new(result, name=self.name, freq=self.freq, tz=getattr(self, 'tz', None))
def unique(self): """ Index.unique with handling for DatetimeIndex/PeriodIndex metadata Returns ------- result : DatetimeIndex or PeriodIndex """ from pandas.core.index import Int64Index result = Int64Index.unique(self) return self._simple_new(result, name=self.name, freq=self.freq, tz=getattr(self, 'tz', None))
def test_join_inner(self): other = Int64Index([7, 12, 25, 1, 2, 5]) other_mono = Int64Index([1, 2, 5, 7, 12, 25]) # not monotonic res, lidx, ridx = self.index.join(other, how='inner', return_indexers=True) # no guarantee of sortedness, so sort for comparison purposes ind = res.argsort() res = res.take(ind) lidx = lidx.take(ind) ridx = ridx.take(ind) eres = Int64Index([2, 12]) elidx = np.array([1, 6]) eridx = np.array([4, 1]) self.assert_(isinstance(res, Int64Index)) self.assert_(res.equals(eres)) self.assert_(np.array_equal(lidx, elidx)) self.assert_(np.array_equal(ridx, eridx)) # monotonic res, lidx, ridx = self.index.join(other_mono, how='inner', return_indexers=True) res2 = self.index.intersection(other_mono) self.assert_(res.equals(res2)) eridx = np.array([1, 4]) self.assert_(isinstance(res, Int64Index)) self.assert_(res.equals(eres)) self.assert_(np.array_equal(lidx, elidx)) self.assert_(np.array_equal(ridx, eridx))
def test_join_outer(self): other = Int64Index([7, 12, 25, 1, 2, 5]) other_mono = Int64Index([1, 2, 5, 7, 12, 25]) # not monotonic # guarantee of sortedness res, lidx, ridx = self.index.join(other, how='outer', return_indexers=True) noidx_res = self.index.join(other, how='outer') self.assert_(res.equals(noidx_res)) eres = Int64Index([0, 1, 2, 4, 5, 6, 7, 8, 10, 12, 14, 16, 18, 25]) elidx = np.array([0, -1, 1, 2, -1, 3, -1, 4, 5, 6, 7, 8, 9, -1], dtype='i4') eridx = np.array([-1, 3, 4, -1, 5, -1, 0, -1, -1, 1, -1, -1, -1, 2], dtype='i4') self.assert_(isinstance(res, Int64Index)) self.assert_(res.equals(eres)) self.assert_(np.array_equal(lidx, elidx)) self.assert_(np.array_equal(ridx, eridx)) # monotonic res, lidx, ridx = self.index.join(other_mono, how='outer', return_indexers=True) noidx_res = self.index.join(other_mono, how='outer') self.assert_(res.equals(noidx_res)) eridx = np.array([-1, 0, 1, -1, 2, -1, 3, -1, -1, 4, -1, -1, -1, 5], dtype='i4') self.assert_(isinstance(res, Int64Index)) self.assert_(res.equals(eres)) self.assert_(np.array_equal(lidx, elidx)) self.assert_(np.array_equal(ridx, eridx))
def iter_slices(table, other, mode, keep_empty): """Yields indices to extract ranges from `table`. Returns an iterable of integer arrays that can apply to Series objects, i.e. columns of `table`. These indices are of the DataFrame/Series' Index, not array coordinates -- so be sure to use DataFrame.loc, Series.loc, or Series getitem, as opposed to .iloc or indexing directly into Numpy arrays. """ for _c, bin_rows, src_rows in by_shared_chroms(other, table, keep_empty): if src_rows is None: # Emit empty indices since 'table' is missing this chromosome for _ in range(len(bin_rows)): yield Int64Index([]) else: for slc, _s, _e in idx_ranges(src_rows, None, bin_rows.start, bin_rows.end, mode): indices = src_rows.index[slc].values if keep_empty or len(indices): yield indices
def test_take_preserve_name(self): index = Int64Index([1, 2, 3, 4], name='foo') taken = index.take([3, 0, 1]) self.assertEqual(index.name, taken.name)
def test_get_indexer_backfill(self): target = Int64Index(np.arange(10)) indexer = self.index.get_indexer(target, method='backfill') expected = np.array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5]) self.assert_(np.array_equal(indexer, expected))
def test_get_indexer(self): target = Int64Index(np.arange(10)) indexer = self.index.get_indexer(target) expected = np.array([0, -1, 1, -1, 2, -1, 3, -1, 4, -1]) self.assert_(np.array_equal(indexer, expected))
def test_is_monotonic(self): self.assert_(self.index.is_monotonic) index = Int64Index([4, 3, 2, 1]) self.assert_(not index.is_monotonic)
def setUp(self): self.index = Int64Index(np.arange(0, 20, 2))