Skip to content

INT/CLN: clean up block slicing semantics #6446

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
Feb 22, 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
31 changes: 20 additions & 11 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2695,22 +2695,31 @@ def get_slice(self, slobj, axis=0, raise_on_error=False):
return bm

def _slice_blocks(self, slobj, axis):
new_blocks = []
"""
slice the blocks using the provided slice object
this is only for slicing on axis != 0
"""

if axis == 0:
raise AssertionError("cannot _slice_blocks on axis=0")

slicer = [slice(None, None) for _ in range(self.ndim)]
slicer[axis] = slobj
slicer = tuple(slicer)
is_unique = self.axes[0].is_unique

for block in self.blocks:
newb = make_block(block._slice(slicer),
block.items,
block.ref_items,
klass=block.__class__,
fastpath=True,
placement=block._ref_locs)
newb.set_ref_locs(block._ref_locs)
new_blocks.append(newb)
return new_blocks
def place(block):
if not is_unique:
return block._ref_locs
return None

return [ make_block(block._slice(slicer),
block.items,
block.ref_items,
klass=block.__class__,
fastpath=True,
placement=place(block)
) for block in self.blocks ]

def get_series_dict(self):
# For DataFrame
Expand Down
22 changes: 10 additions & 12 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,21 +380,19 @@ def test_iloc_exceeds_bounds(self):
assert_series_equal(result,expected)

# doc example
df = DataFrame(np.random.randn(5,2),columns=list('AB'))
self.assertRaises(IndexError, lambda : df.iloc[[4,5,6]])
self.assertRaises(IndexError, lambda : df.iloc[:,4])
def check(result,expected):
str(result)
result.dtypes
assert_frame_equal(result,expected)

result = df.iloc[4:6]
expected = df.iloc[[4]]
assert_frame_equal(result,expected)
dfl = DataFrame(np.random.randn(5,2),columns=list('AB'))
check(dfl.iloc[:,2:3],DataFrame(index=dfl.index))
check(dfl.iloc[:,1:3],dfl.iloc[:,[1]])
check(dfl.iloc[4:6],dfl.iloc[[4]])

result = df.iloc[:,2:3]
expected = DataFrame(index=df.index)
assert_frame_equal(result,expected)
self.assertRaises(IndexError, lambda : dfl.iloc[[4,5,6]])
self.assertRaises(IndexError, lambda : dfl.iloc[:,4])

result = df.iloc[:,1:3]
expected = df.iloc[:,[1]]
assert_frame_equal(result,expected)

def test_iloc_getitem_int(self):

Expand Down