本文整理汇总了Python中pandas.Int64Index方法的典型用法代码示例。如果您正苦于以下问题:Python pandas.Int64Index方法的具体用法?Python pandas.Int64Index怎么用?Python pandas.Int64Index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas
的用法示例。
在下文中一共展示了pandas.Int64Index方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_astype_conversion
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def test_astype_conversion(self):
# GH#13149, GH#13209
idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D')
result = idx.astype(object)
expected = Index([Period('2016-05-16', freq='D')] +
[Period(NaT, freq='D')] * 3, dtype='object')
tm.assert_index_equal(result, expected)
result = idx.astype(np.int64)
expected = Int64Index([16937] + [-9223372036854775808] * 3,
dtype=np.int64)
tm.assert_index_equal(result, expected)
result = idx.astype(str)
expected = Index(str(x) for x in idx)
tm.assert_index_equal(result, expected)
idx = period_range('1990', '2009', freq='A')
result = idx.astype('i8')
tm.assert_index_equal(result, Index(idx.asi8))
tm.assert_numpy_array_equal(result.values, idx.asi8)
示例2: test_values_multiindex_periodindex
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def test_values_multiindex_periodindex():
# Test to ensure we hit the boxing / nobox part of MI.values
ints = np.arange(2007, 2012)
pidx = pd.PeriodIndex(ints, freq='D')
idx = pd.MultiIndex.from_arrays([ints, pidx])
result = idx.values
outer = pd.Int64Index([x[0] for x in result])
tm.assert_index_equal(outer, pd.Int64Index(ints))
inner = pd.PeriodIndex([x[1] for x in result])
tm.assert_index_equal(inner, pidx)
# n_lev > n_lab
result = idx[:2].values
outer = pd.Int64Index([x[0] for x in result])
tm.assert_index_equal(outer, pd.Int64Index(ints[:2]))
inner = pd.PeriodIndex([x[1] for x in result])
tm.assert_index_equal(inner, pidx[:2])
示例3: test_rangeindex_fallback_coercion_bug
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def test_rangeindex_fallback_coercion_bug():
# GH 12893
foo = pd.DataFrame(np.arange(100).reshape((10, 10)))
bar = pd.DataFrame(np.arange(100).reshape((10, 10)))
df = pd.concat({'foo': foo.stack(), 'bar': bar.stack()}, axis=1)
df.index.names = ['fizz', 'buzz']
str(df)
expected = pd.DataFrame({'bar': np.arange(100),
'foo': np.arange(100)},
index=pd.MultiIndex.from_product(
[range(10), range(10)],
names=['fizz', 'buzz']))
tm.assert_frame_equal(df, expected, check_like=True)
result = df.index.get_level_values('fizz')
expected = pd.Int64Index(np.arange(10), name='fizz').repeat(10)
tm.assert_index_equal(result, expected)
result = df.index.get_level_values('buzz')
expected = pd.Int64Index(np.tile(np.arange(10), 10), name='buzz')
tm.assert_index_equal(result, expected)
示例4: setattributeindex
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def setattributeindex(self, instance, value):
bus_name = instance.bus.index
instance.branch['F_BUS'] = instance.branch['F_BUS'].apply(lambda x: value[bus_name.get_loc(x)])
instance.branch['T_BUS'] = instance.branch['T_BUS'].apply(lambda x: value[bus_name.get_loc(x)])
instance.gen['GEN_BUS'] = instance.gen['GEN_BUS'].apply(lambda x: value[bus_name.get_loc(x)])
try:
instance.load.columns = [v for b, v in zip(instance.bus_name.isin(instance.load.columns), value) if b == True]
except ValueError:
instance.load.columns = value
except AttributeError:
instance.load = pd.DataFrame(0, index=range(0, 1), columns=value, dtype='float')
instance.bus.index = value
if isinstance(instance.bus_name, pd.RangeIndex) or isinstance(instance.bus_name, pd.Int64Index):
logger.debug('Forcing string types for all bus names')
instance.bus_name = ['Bus{}'.format(b) for b in instance.bus_name]
示例5: infer_index_value
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def infer_index_value(left_index_value, right_index_value):
from .core import IndexValue
if isinstance(left_index_value.value, IndexValue.RangeIndex) and \
isinstance(right_index_value.value, IndexValue.RangeIndex):
if left_index_value.value.slice == right_index_value.value.slice:
return left_index_value
return parse_index(pd.Int64Index([]), left_index_value, right_index_value)
# when left index and right index is identical, and both of them are elements unique,
# we can infer that the out index should be identical also
if left_index_value.is_unique and right_index_value.is_unique and \
left_index_value.key == right_index_value.key:
return left_index_value
left_index = left_index_value.to_pandas()
right_index = right_index_value.to_pandas()
out_index = pd.Index([], dtype=find_common_type([left_index.dtype, right_index.dtype]))
return parse_index(out_index, left_index_value, right_index_value)
示例6: __call__
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def __call__(self, a):
assert self.axis == 0
if self.ignore_index:
index_value = parse_index(pd.RangeIndex(a.shape[0]))
else:
if isinstance(a.index_value.value, IndexValue.RangeIndex):
index_value = parse_index(pd.Int64Index([]))
else:
index_value = a.index_value
if a.ndim == 2:
return self.new_dataframe([a], shape=a.shape, dtypes=a.dtypes,
index_value=index_value,
columns_value=a.columns_value)
else:
return self.new_series([a], shape=a.shape, dtype=a.dtype,
index_value=index_value, name=a.name)
示例7: testAbs
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def testAbs(self):
data1 = pd.DataFrame(np.random.rand(10, 10), index=[0, 10, 2, 3, 4, 5, 6, 7, 8, 9],
columns=[4, 1, 3, 2, 10, 5, 9, 8, 6, 7])
df1 = from_pandas(data1, chunk_size=(5, 10))
df2 = df1.abs()
# test df2's index and columns
pd.testing.assert_index_equal(df2.columns_value.to_pandas(), df1.columns_value.to_pandas())
self.assertIsInstance(df2.index_value.value, IndexValue.Int64Index)
self.assertEqual(df2.shape, (10, 10))
df2 = df2.tiles()
df1 = get_tiled(df1)
self.assertEqual(df2.chunk_shape, (2, 1))
for c2, c1 in zip(df2.chunks, df1.chunks):
self.assertIsInstance(c2.op, DataFrameAbs)
self.assertEqual(len(c2.inputs), 1)
# compare with input chunks
self.assertEqual(c2.index, c1.index)
pd.testing.assert_index_equal(c2.columns_value.to_pandas(), c1.columns_value.to_pandas())
pd.testing.assert_index_equal(c2.index_value.to_pandas(), c1.index_value.to_pandas())
示例8: testNot
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def testNot(self):
data1 = pd.DataFrame(np.random.rand(10, 10) > 0.5, index=[0, 10, 2, 3, 4, 5, 6, 7, 8, 9],
columns=[4, 1, 3, 2, 10, 5, 9, 8, 6, 7])
df1 = from_pandas(data1, chunk_size=(5, 10))
df2 = ~df1
# test df2's index and columns
pd.testing.assert_index_equal(df2.columns_value.to_pandas(), df1.columns_value.to_pandas())
self.assertIsInstance(df2.index_value.value, IndexValue.Int64Index)
self.assertEqual(df2.shape, (10, 10))
df2 = df2.tiles()
df1 = get_tiled(df1)
self.assertEqual(df2.chunk_shape, (2, 1))
for c2, c1 in zip(df2.chunks, df1.chunks):
self.assertIsInstance(c2.op, DataFrameNot)
self.assertEqual(len(c2.inputs), 1)
# compare with input chunks
self.assertEqual(c2.index, c1.index)
pd.testing.assert_index_equal(c2.columns_value.to_pandas(), c1.columns_value.to_pandas())
pd.testing.assert_index_equal(c2.index_value.to_pandas(), c1.index_value.to_pandas())
示例9: test_join_left
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def test_join_left(self):
# Join with Int64Index
other = Int64Index(np.arange(25, 14, -1))
res, lidx, ridx = self.index.join(other, how='left',
return_indexers=True)
eres = self.index
eridx = np.array([-1, -1, -1, -1, -1, -1, -1, -1, 9, 7], dtype=np.intp)
assert isinstance(res, RangeIndex)
tm.assert_index_equal(res, eres)
assert lidx is None
tm.assert_numpy_array_equal(ridx, eridx)
# Join withRangeIndex
other = Int64Index(np.arange(25, 14, -1))
res, lidx, ridx = self.index.join(other, how='left',
return_indexers=True)
assert isinstance(res, RangeIndex)
tm.assert_index_equal(res, eres)
assert lidx is None
tm.assert_numpy_array_equal(ridx, eridx)
示例10: test_rangeindex_fallback_coercion_bug
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def test_rangeindex_fallback_coercion_bug(self):
# GH 12893
foo = pd.DataFrame(np.arange(100).reshape((10, 10)))
bar = pd.DataFrame(np.arange(100).reshape((10, 10)))
df = pd.concat({'foo': foo.stack(), 'bar': bar.stack()}, axis=1)
df.index.names = ['fizz', 'buzz']
str(df)
expected = pd.DataFrame({'bar': np.arange(100),
'foo': np.arange(100)},
index=pd.MultiIndex.from_product(
[range(10), range(10)],
names=['fizz', 'buzz']))
tm.assert_frame_equal(df, expected, check_like=True)
result = df.index.get_level_values('fizz')
expected = pd.Int64Index(np.arange(10), name='fizz').repeat(10)
tm.assert_index_equal(result, expected)
result = df.index.get_level_values('buzz')
expected = pd.Int64Index(np.tile(np.arange(10), 10), name='buzz')
tm.assert_index_equal(result, expected)
示例11: test_constructor_empty
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def test_constructor_empty(self):
# GH 17248
c = Categorical([])
expected = Index([])
tm.assert_index_equal(c.categories, expected)
c = Categorical([], categories=[1, 2, 3])
expected = pd.Int64Index([1, 2, 3])
tm.assert_index_equal(c.categories, expected)
示例12: test_union
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def test_union(self):
i1 = timedelta_range('1day', periods=5)
i2 = timedelta_range('3day', periods=5)
result = i1.union(i2)
expected = timedelta_range('1day', periods=7)
tm.assert_index_equal(result, expected)
i1 = Int64Index(np.arange(0, 20, 2))
i2 = timedelta_range(start='1 day', periods=10, freq='D')
i1.union(i2) # Works
i2.union(i1) # Fails with "AttributeError: can't set attribute"
示例13: test_join_outer
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def test_join_outer(self):
# join with Int64Index
other = Int64Index(np.arange(25, 14, -1))
res, lidx, ridx = self.index.join(other, how='outer',
return_indexers=True)
noidx_res = self.index.join(other, how='outer')
tm.assert_index_equal(res, noidx_res)
eres = Int64Index([0, 2, 4, 6, 8, 10, 12, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25])
elidx = np.array([0, 1, 2, 3, 4, 5, 6, 7, -1, 8, -1, 9,
-1, -1, -1, -1, -1, -1, -1], dtype=np.intp)
eridx = np.array([-1, -1, -1, -1, -1, -1, -1, -1, 10, 9, 8, 7, 6,
5, 4, 3, 2, 1, 0], dtype=np.intp)
assert isinstance(res, Int64Index)
assert not isinstance(res, RangeIndex)
tm.assert_index_equal(res, eres)
tm.assert_numpy_array_equal(lidx, elidx)
tm.assert_numpy_array_equal(ridx, eridx)
# join with RangeIndex
other = RangeIndex(25, 14, -1)
res, lidx, ridx = self.index.join(other, how='outer',
return_indexers=True)
noidx_res = self.index.join(other, how='outer')
tm.assert_index_equal(res, noidx_res)
assert isinstance(res, Int64Index)
assert not isinstance(res, RangeIndex)
tm.assert_index_equal(res, eres)
tm.assert_numpy_array_equal(lidx, elidx)
tm.assert_numpy_array_equal(ridx, eridx)
示例14: test_join_inner
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def test_join_inner(self):
# Join with non-RangeIndex
other = Int64Index(np.arange(25, 14, -1))
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([16, 18])
elidx = np.array([8, 9], dtype=np.intp)
eridx = np.array([9, 7], dtype=np.intp)
assert isinstance(res, Int64Index)
tm.assert_index_equal(res, eres)
tm.assert_numpy_array_equal(lidx, elidx)
tm.assert_numpy_array_equal(ridx, eridx)
# Join two RangeIndex
other = RangeIndex(25, 14, -1)
res, lidx, ridx = self.index.join(other, how='inner',
return_indexers=True)
assert isinstance(res, RangeIndex)
tm.assert_index_equal(res, eres)
tm.assert_numpy_array_equal(lidx, elidx)
tm.assert_numpy_array_equal(ridx, eridx)
示例15: test_join_right
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import Int64Index [as 别名]
def test_join_right(self):
# Join with Int64Index
other = Int64Index(np.arange(25, 14, -1))
res, lidx, ridx = self.index.join(other, how='right',
return_indexers=True)
eres = other
elidx = np.array([-1, -1, -1, -1, -1, -1, -1, 9, -1, 8, -1],
dtype=np.intp)
assert isinstance(other, Int64Index)
tm.assert_index_equal(res, eres)
tm.assert_numpy_array_equal(lidx, elidx)
assert ridx is None
# Join withRangeIndex
other = RangeIndex(25, 14, -1)
res, lidx, ridx = self.index.join(other, how='right',
return_indexers=True)
eres = other
assert isinstance(other, RangeIndex)
tm.assert_index_equal(res, eres)
tm.assert_numpy_array_equal(lidx, elidx)
assert ridx is None