本文整理汇总了Python中pandas.core.index.MultiIndex类的典型用法代码示例。如果您正苦于以下问题:Python MultiIndex类的具体用法?Python MultiIndex怎么用?Python MultiIndex使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MultiIndex类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _wrap_applied_output
def _wrap_applied_output(self, keys, values, not_indexed_same=False):
if len(keys) == 0:
return Series([])
key_names = [ping.name for ping in self.groupings]
if isinstance(values[0], Series):
if not_indexed_same:
data_dict = dict(zip(keys, values))
result = DataFrame(data_dict).T
if len(self.groupings) > 1:
result.index = MultiIndex.from_tuples(keys, names=key_names)
return result
else:
cat_values = np.concatenate([x.values for x in values])
cat_index = values[0].index
if len(values) > 1:
cat_index = cat_index.append([x.index for x in values[1:]])
return Series(cat_values, index=cat_index)
elif isinstance(values[0], DataFrame):
# possible that Series -> DataFrame by applied function
return self._wrap_frames(keys, values,
not_indexed_same=not_indexed_same)
else:
if len(self.groupings) > 1:
index = MultiIndex.from_tuples(keys, names=key_names)
return Series(values, index)
else:
return Series(values, keys)
开发者ID:hammer,项目名称:pandas,代码行数:29,代码来源:groupby.py
示例2: test_apply_categorical_data
def test_apply_categorical_data(self):
# GH 10138
for ordered in [True, False]:
dense = Categorical(list("abc"), ordered=ordered)
# 'b' is in the categories but not in the list
missing = Categorical(list("aaa"), categories=["a", "b"], ordered=ordered)
values = np.arange(len(dense))
df = DataFrame({"missing": missing, "dense": dense, "values": values})
grouped = df.groupby(["missing", "dense"])
# missing category 'b' should still exist in the output index
idx = MultiIndex.from_product(
[Categorical(["a", "b"], ordered=ordered), Categorical(["a", "b", "c"], ordered=ordered)],
names=["missing", "dense"],
)
expected = DataFrame([0, 1, 2, np.nan, np.nan, np.nan], index=idx, columns=["values"])
assert_frame_equal(grouped.apply(lambda x: np.mean(x)), expected)
assert_frame_equal(grouped.mean(), expected)
assert_frame_equal(grouped.agg(np.mean), expected)
# but for transform we should still get back the original index
idx = MultiIndex.from_product([["a"], ["a", "b", "c"]], names=["missing", "dense"])
expected = Series(1, index=idx)
assert_series_equal(grouped.apply(lambda x: 1), expected)
开发者ID:parthea,项目名称:pandas,代码行数:25,代码来源:test_categorical.py
示例3: test_reset_index
def test_reset_index(self):
df = tm.makeDataFrame()[:5]
ser = df.stack()
ser.index.names = ["hash", "category"]
ser.name = "value"
df = ser.reset_index()
self.assertIn("value", df)
df = ser.reset_index(name="value2")
self.assertIn("value2", df)
# check inplace
s = ser.reset_index(drop=True)
s2 = ser
s2.reset_index(drop=True, inplace=True)
assert_series_equal(s, s2)
# level
index = MultiIndex(
levels=[["bar"], ["one", "two", "three"], [0, 1]],
labels=[[0, 0, 0, 0, 0, 0], [0, 1, 2, 0, 1, 2], [0, 1, 0, 1, 0, 1]],
)
s = Series(np.random.randn(6), index=index)
rs = s.reset_index(level=1)
self.assertEqual(len(rs.columns), 2)
rs = s.reset_index(level=[0, 2], drop=True)
self.assertTrue(rs.index.equals(Index(index.get_level_values(1))))
tm.assertIsInstance(rs, Series)
开发者ID:srib,项目名称:pandas,代码行数:30,代码来源:test_alter_axes.py
示例4: test_reset_index
def test_reset_index(self):
df = tm.makeDataFrame()[:5]
ser = df.stack()
ser.index.names = ['hash', 'category']
ser.name = 'value'
df = ser.reset_index()
self.assertIn('value', df)
df = ser.reset_index(name='value2')
self.assertIn('value2', df)
# check inplace
s = ser.reset_index(drop=True)
s2 = ser
s2.reset_index(drop=True, inplace=True)
assert_series_equal(s, s2)
# level
index = MultiIndex(levels=[['bar'], ['one', 'two', 'three'], [0, 1]],
labels=[[0, 0, 0, 0, 0, 0], [0, 1, 2, 0, 1, 2],
[0, 1, 0, 1, 0, 1]])
s = Series(np.random.randn(6), index=index)
rs = s.reset_index(level=1)
self.assertEqual(len(rs.columns), 2)
rs = s.reset_index(level=[0, 2], drop=True)
self.assert_index_equal(rs.index, Index(index.get_level_values(1)))
tm.assertIsInstance(rs, Series)
开发者ID:AkiraKane,项目名称:pandas,代码行数:29,代码来源:test_alter_axes.py
示例5: test_apply_categorical_data
def test_apply_categorical_data(self):
# GH 10138
for ordered in [True, False]:
dense = Categorical(list('abc'), ordered=ordered)
# 'b' is in the categories but not in the list
missing = Categorical(
list('aaa'), categories=['a', 'b'], ordered=ordered)
values = np.arange(len(dense))
df = DataFrame({'missing': missing,
'dense': dense,
'values': values})
grouped = df.groupby(['missing', 'dense'])
# missing category 'b' should still exist in the output index
idx = MultiIndex.from_product(
[Categorical(['a', 'b'], ordered=ordered),
Categorical(['a', 'b', 'c'], ordered=ordered)],
names=['missing', 'dense'])
expected = DataFrame([0, 1, 2, np.nan, np.nan, np.nan],
index=idx,
columns=['values'])
assert_frame_equal(grouped.apply(lambda x: np.mean(x)), expected)
assert_frame_equal(grouped.mean(), expected)
assert_frame_equal(grouped.agg(np.mean), expected)
# but for transform we should still get back the original index
idx = MultiIndex.from_product([['a'], ['a', 'b', 'c']],
names=['missing', 'dense'])
expected = Series(1, index=idx)
assert_series_equal(grouped.apply(lambda x: 1), expected)
开发者ID:AlexisMignon,项目名称:pandas,代码行数:31,代码来源:test_categorical.py
示例6: _coo_to_sparse_series
def _coo_to_sparse_series(A, dense_index=False):
""" Convert a scipy.sparse.coo_matrix to a SparseSeries.
Use the defaults given in the SparseSeries constructor. """
s = Series(A.data, MultiIndex.from_arrays((A.row, A.col)))
s = s.sort_index()
s = s.to_sparse() # TODO: specify kind?
if dense_index:
# is there a better constructor method to use here?
i = range(A.shape[0])
j = range(A.shape[1])
ind = MultiIndex.from_product([i, j])
s = s.reindex_axis(ind)
return s
开发者ID:gpoulin,项目名称:pandas,代码行数:13,代码来源:scipy_sparse.py
示例7: test_slice_locs_not_sorted
def test_slice_locs_not_sorted(self):
index = MultiIndex(levels=[Index(range(4)),
Index(range(4)),
Index(range(4))],
labels=[np.array([0, 0, 1, 2, 2, 2, 3, 3]),
np.array([0, 1, 0, 0, 0, 1, 0, 1]),
np.array([1, 0, 1, 1, 0, 0, 1, 0])])
self.assertRaises(Exception, index.slice_locs, (1, 0, 1),
(2, 1, 0))
# works
sorted_index, _ = index.sortlevel(0)
result = sorted_index.slice_locs((1, 0, 1), (2, 1, 0))
开发者ID:MLnick,项目名称:pandas,代码行数:14,代码来源:test_index.py
示例8: test_droplevel_with_names
def test_droplevel_with_names(self):
index = self.index[self.index.get_loc('foo')]
dropped = index.droplevel(0)
self.assertEqual(dropped.name, 'second')
index = MultiIndex(levels=[Index(range(4)),
Index(range(4)),
Index(range(4))],
labels=[np.array([0, 0, 1, 2, 2, 2, 3, 3]),
np.array([0, 1, 0, 0, 0, 1, 0, 1]),
np.array([1, 0, 1, 1, 0, 0, 1, 0])],
names=['one', 'two', 'three'])
dropped = index.droplevel(0)
self.assertEqual(dropped.names, ['two', 'three'])
开发者ID:fperez,项目名称:pandas,代码行数:14,代码来源:test_index.py
示例9: test_get_loc
def test_get_loc(self):
self.assert_(self.index.get_loc(('foo', 'two')) == 1)
self.assert_(self.index.get_loc(('baz', 'two')) == 3)
self.assertRaises(KeyError, self.index.get_loc, ('bar', 'two'))
self.assertRaises(KeyError, self.index.get_loc, 'quux')
# 3 levels
index = MultiIndex(levels=[Index(range(4)),
Index(range(4)),
Index(range(4))],
labels=[np.array([0, 0, 1, 2, 2, 2, 3, 3]),
np.array([0, 1, 0, 0, 0, 1, 0, 1]),
np.array([1, 0, 1, 1, 0, 0, 1, 0])])
self.assertRaises(KeyError, index.get_loc, (1, 1))
self.assert_(index.get_loc((2, 0)) == slice(3, 5))
开发者ID:MLnick,项目名称:pandas,代码行数:15,代码来源:test_index.py
示例10: test_alignment
def test_alignment(self):
x = Series(data=[1, 2, 3], index=MultiIndex.from_tuples([("A", 1), ("A", 2), ("B", 3)]))
y = Series(data=[4, 5, 6], index=MultiIndex.from_tuples([("Z", 1), ("Z", 2), ("B", 3)]))
res = x - y
exp_index = x.index.union(y.index)
exp = x.reindex(exp_index) - y.reindex(exp_index)
assert_series_equal(res, exp)
# hit non-monotonic code path
res = x[::-1] - y[::-1]
exp_index = x.index.union(y.index)
exp = x.reindex(exp_index) - y.reindex(exp_index)
assert_series_equal(res, exp)
开发者ID:flyingV,项目名称:pandas,代码行数:15,代码来源:test_multilevel.py
示例11: test_mixed_depth_drop
def test_mixed_depth_drop(self):
arrays = [[ 'a', 'top', 'top', 'routine1', 'routine1', 'routine2'],
[ '', 'OD', 'OD', 'result1', 'result2', 'result1'],
[ '', 'wx', 'wy', '', '', '']]
tuples = zip(*arrays)
tuples.sort()
index = MultiIndex.from_tuples(tuples)
df = DataFrame(randn(4,6),columns = index)
result = df.drop('a',axis=1)
expected = df.drop([('a','','')],axis=1)
assert_frame_equal(expected, result)
result = df.drop(['top'],axis=1)
expected = df.drop([('top','OD','wx')], axis=1)
expected = expected.drop([('top','OD','wy')], axis=1)
assert_frame_equal(expected, result)
result = df.drop(('top', 'OD', 'wx'), axis=1)
expected = df.drop([('top','OD','wx')], axis=1)
assert_frame_equal(expected, result)
expected = df.drop([('top','OD','wy')], axis=1)
expected = df.drop('top', axis=1)
result = df.drop('result1', level=1, axis=1)
expected = df.drop([('routine1', 'result1', ''),
('routine2', 'result1', '')], axis=1)
assert_frame_equal(expected, result)
开发者ID:afonit,项目名称:pandas,代码行数:30,代码来源:test_multilevel.py
示例12: test_agg_compat
def test_agg_compat(self):
# GH 12334
df = DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'two',
'two', 'two', 'one', 'two'],
'C': np.random.randn(8) + 1.0,
'D': np.arange(8)})
g = df.groupby(['A', 'B'])
expected = pd.concat([g['D'].sum(),
g['D'].std()],
axis=1)
expected.columns = MultiIndex.from_tuples([('C', 'sum'),
('C', 'std')])
result = g['D'].agg({'C': ['sum', 'std']})
assert_frame_equal(result, expected, check_like=True)
expected = pd.concat([g['D'].sum(),
g['D'].std()],
axis=1)
expected.columns = ['C', 'D']
result = g['D'].agg({'C': 'sum', 'D': 'std'})
assert_frame_equal(result, expected, check_like=True)
开发者ID:AlexisMignon,项目名称:pandas,代码行数:27,代码来源:test_aggregate.py
示例13: test_constructor
def test_constructor(self):
self.assertTrue(self.ts.index.is_all_dates)
# Pass in Series
derived = Series(self.ts)
self.assertTrue(derived.index.is_all_dates)
self.assertTrue(tm.equalContents(derived.index, self.ts.index))
# Ensure new index is not created
self.assertEqual(id(self.ts.index), id(derived.index))
# Mixed type Series
mixed = Series(['hello', np.NaN], index=[0, 1])
self.assertEqual(mixed.dtype, np.object_)
self.assertIs(mixed[1], np.NaN)
self.assertFalse(self.empty.index.is_all_dates)
self.assertFalse(Series({}).index.is_all_dates)
self.assertRaises(Exception, Series, np.random.randn(3, 3),
index=np.arange(3))
mixed.name = 'Series'
rs = Series(mixed).name
xp = 'Series'
self.assertEqual(rs, xp)
# raise on MultiIndex GH4187
m = MultiIndex.from_arrays([[1, 2], [3, 4]])
self.assertRaises(NotImplementedError, Series, m)
开发者ID:RogerThomas,项目名称:pandas,代码行数:29,代码来源:test_constructors.py
示例14: setUp
def setUp(self):
index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'],
['one', 'two', 'three']],
labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
[0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
names=['first', 'second'])
self.frame = DataFrame(np.random.randn(10, 3), index=index,
columns=Index(['A', 'B', 'C'], name='exp'))
self.single_level = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux']],
labels=[[0, 1, 2, 3]],
names=['first'])
# create test series object
arrays = [['bar', 'bar', 'baz', 'baz', 'qux', 'qux', 'foo', 'foo'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = zip(*arrays)
index = MultiIndex.from_tuples(tuples)
s = Series(randn(8), index=index)
s[3] = np.NaN
self.series = s
tm.N = 100
self.tdf = tm.makeTimeDataFrame()
self.ymd = self.tdf.groupby([lambda x: x.year, lambda x: x.month,
lambda x: x.day]).sum()
# use Int64Index, to make sure things work
self.ymd.index.levels = [lev.astype('i8')
for lev in self.ymd.index.levels]
self.ymd.index.names = ['year', 'month', 'day']
开发者ID:ashokez,项目名称:pandas,代码行数:31,代码来源:test_multilevel.py
示例15: test_mixed_depth_pop
def test_mixed_depth_pop(self):
arrays = [
["a", "top", "top", "routine1", "routine1", "routine2"],
["", "OD", "OD", "result1", "result2", "result1"],
["", "wx", "wy", "", "", ""],
]
tuples = zip(*arrays)
tuples.sort()
index = MultiIndex.from_tuples(tuples)
df = DataFrame(randn(4, 6), columns=index)
df1 = df.copy()
df2 = df.copy()
result = df1.pop("a")
expected = df2.pop(("a", "", ""))
assert_series_equal(expected, result)
assert_frame_equal(df1, df2)
self.assertEquals(result.name, "a")
expected = df1["top"]
df1 = df1.drop(["top"], axis=1)
result = df2.pop("top")
assert_frame_equal(expected, result)
assert_frame_equal(df1, df2)
开发者ID:flyingV,项目名称:pandas,代码行数:25,代码来源:test_multilevel.py
示例16: setUp
def setUp(self):
index = MultiIndex(
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
names=["first", "second"],
)
self.frame = DataFrame(np.random.randn(10, 3), index=index, columns=Index(["A", "B", "C"], name="exp"))
self.single_level = MultiIndex(levels=[["foo", "bar", "baz", "qux"]], labels=[[0, 1, 2, 3]], names=["first"])
# create test series object
arrays = [
["bar", "bar", "baz", "baz", "qux", "qux", "foo", "foo"],
["one", "two", "one", "two", "one", "two", "one", "two"],
]
tuples = zip(*arrays)
index = MultiIndex.from_tuples(tuples)
s = Series(randn(8), index=index)
s[3] = np.NaN
self.series = s
tm.N = 100
self.tdf = tm.makeTimeDataFrame()
self.ymd = self.tdf.groupby([lambda x: x.year, lambda x: x.month, lambda x: x.day]).sum()
# use Int64Index, to make sure things work
self.ymd.index.levels = [lev.astype("i8") for lev in self.ymd.index.levels]
self.ymd.index.names = ["year", "month", "day"]
开发者ID:flyingV,项目名称:pandas,代码行数:28,代码来源:test_multilevel.py
示例17: test_delevel_infer_dtype
def test_delevel_infer_dtype(self):
tuples = [tuple for tuple in cart_product(["foo", "bar"], [10, 20], [1.0, 1.1])]
index = MultiIndex.from_tuples(tuples, names=["prm0", "prm1", "prm2"])
df = DataFrame(np.random.randn(8, 3), columns=["A", "B", "C"], index=index)
deleveled = df.reset_index()
self.assert_(com.is_integer_dtype(deleveled["prm1"]))
self.assert_(com.is_float_dtype(deleveled["prm2"]))
开发者ID:flyingV,项目名称:pandas,代码行数:7,代码来源:test_multilevel.py
示例18: _wrap_result_expand
def _wrap_result_expand(self, result, expand=False):
if not isinstance(expand, bool):
raise ValueError("expand must be True or False")
from pandas.core.index import Index, MultiIndex
if not hasattr(result, 'ndim'):
return result
if isinstance(self.series, Index):
name = getattr(result, 'name', None)
# if result is a boolean np.array, return the np.array
# instead of wrapping it into a boolean Index (GH 8875)
if hasattr(result, 'dtype') and is_bool_dtype(result):
return result
if expand:
result = list(result)
return MultiIndex.from_tuples(result, names=name)
else:
return Index(result, name=name)
else:
index = self.series.index
if expand:
def cons_row(x):
if is_list_like(x):
return x
else:
return [ x ]
cons = self.series._constructor_expanddim
data = [cons_row(x) for x in result]
return cons(data, index=index)
else:
name = getattr(result, 'name', None)
cons = self.series._constructor
return cons(result, name=name, index=index)
开发者ID:hamedhsn,项目名称:pandas,代码行数:35,代码来源:strings.py
示例19: _agg_index
def _agg_index(self, index, try_parse_dates=True):
if np.isscalar(self.index_col):
if try_parse_dates and self._should_parse_dates(self.index_col):
index = self._conv_date(index)
na_values = self.na_values
if isinstance(na_values, dict):
na_values = _get_na_values(self.index_name, na_values)
index, na_count = _convert_types(index, na_values)
index = Index(index, name=self.index_name)
if self.verbose and na_count:
print 'Found %d NA values in the index' % na_count
else:
arrays = []
for i, arr in enumerate(index):
if (try_parse_dates and
self._should_parse_dates(self.index_col[i])):
arr = self._conv_date(arr)
col_na_values = self.na_values
if isinstance(self.na_values, dict):
col_name = self.index_name[i]
if col_name is not None:
col_na_values = _get_na_values(col_name,
self.na_values)
arr, _ = _convert_types(arr, col_na_values)
arrays.append(arr)
index = MultiIndex.from_arrays(arrays, names=self.index_name)
return index
开发者ID:MikeLindenau,项目名称:pandas,代码行数:27,代码来源:parsers.py
示例20: test_dataframe_insert_column_all_na
def test_dataframe_insert_column_all_na(self):
# GH #1534
mix = MultiIndex.from_tuples([('1a', '2a'), ('1a', '2b'), ('1a', '2c')])
df = DataFrame([[1,2],[3,4],[5,6]], index=mix)
s = Series({(1,1): 1, (1,2): 2})
df['new'] = s
self.assert_(df['new'].isnull().all())
开发者ID:eisenkdr,项目名称:pandas,代码行数:7,代码来源:test_multilevel.py
注:本文中的pandas.core.index.MultiIndex类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论