Skip to content

BUG: Bug in multi-index slice setting, related GH3738 #7667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,10 @@ def _convert_slice_indexer_getitem(self, key, is_index_slice=False):
def _convert_slice_indexer(self, key, typ=None):
""" convert a slice indexer. disallow floats in the start/stop/step """

# if we are not a slice, then we are done
if not isinstance(key, slice):
return key

# validate iloc
if typ == 'iloc':

Expand Down Expand Up @@ -2008,6 +2012,11 @@ def _convert_scalar_indexer(self, key, typ=None):
def _convert_slice_indexer(self, key, typ=None):
""" convert a slice indexer, by definition these are labels
unless we are iloc """

# if we are not a slice, then we are done
if not isinstance(key, slice):
return key

if typ == 'iloc':
return super(Float64Index, self)._convert_slice_indexer(key,
typ=typ)
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,29 @@ def f():
df.loc['bar'] *= 2
self.assertRaises(TypeError, f)

# from SO
#http://stackoverflow.com/questions/24572040/pandas-access-the-level-of-multiindex-for-inplace-operation
df_orig = DataFrame.from_dict({'price': {
('DE', 'Coal', 'Stock'): 2,
('DE', 'Gas', 'Stock'): 4,
('DE', 'Elec', 'Demand'): 1,
('FR', 'Gas', 'Stock'): 5,
('FR', 'Solar', 'SupIm'): 0,
('FR', 'Wind', 'SupIm'): 0}})
df_orig.index = MultiIndex.from_tuples(df_orig.index, names=['Sit', 'Com', 'Type'])

expected = df_orig.copy()
expected.iloc[[0,2,3]] *= 2

idx = pd.IndexSlice
df = df_orig.copy()
df.loc[idx[:,:,'Stock'],:] *= 2
assert_frame_equal(df, expected)

df = df_orig.copy()
df.loc[idx[:,:,'Stock'],'price'] *= 2
assert_frame_equal(df, expected)

def test_getitem_multiindex(self):

# GH 5725
Expand Down