Skip to content

Commit 1cae09c

Browse files
committed
ENH: can pass sequence of integers to DataFrame.{irow/icol} and Series.iget, GH #654"
1 parent 4d22954 commit 1cae09c

File tree

5 files changed

+58
-13
lines changed

5 files changed

+58
-13
lines changed

RELEASE.rst

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ pandas 0.7.0
157157
console-formatted DataFrames (can read the row of index names, etc.)
158158
- Can pass list of group labels (without having to convert to an ndarray
159159
yourself) to ``groupby`` in some cases (GH #659)
160+
- Use ``kind`` argument to Series.order for selecting different sort kinds
161+
(GH #668)
160162

161163
**Bug fixes**
162164

@@ -225,6 +227,7 @@ pandas 0.7.0
225227
- Could not create a new column in a DataFrame from a list of tuples
226228
- Fix bugs preventing SparseDataFrame and SparseSeries working with groupby
227229
(GH #666)
230+
- Use sort kind in Series.sort / argsort (GH #668)
228231

229232
Thanks
230233
------

pandas/core/frame.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -1220,38 +1220,48 @@ def set_value(self, index, col, value):
12201220

12211221
def irow(self, i):
12221222
"""
1223-
Retrieve the i-th row of the DataFrame by location as a Series. Can
1224-
also pass a slice object
1223+
Retrieve the i-th row or rows of the DataFrame by location
12251224
12261225
Parameters
12271226
----------
1228-
i : int or slice
1227+
i : int, slice, or sequence of integers
1228+
1229+
Notes
1230+
-----
1231+
If slice passed, the resulting data will be a view
12291232
12301233
Returns
12311234
-------
1232-
row : Series
1235+
row : Series (int) or DataFrame (slice, sequence)
12331236
"""
12341237
if isinstance(i, slice):
12351238
return self[i]
12361239
else:
12371240
label = self.index[i]
1238-
return self.xs(label)
1241+
if isinstance(label, Index):
1242+
return self.reindex(label)
1243+
else:
1244+
return self.xs(label)
12391245

12401246
def icol(self, i):
12411247
"""
1242-
Retrieve the i-th column of the DataFrame by location as a Series. Can
1243-
also pass a slice object
1248+
Retrieve the i-th column or columns of the DataFrame by location
12441249
12451250
Parameters
12461251
----------
1247-
i : int or slice
1252+
i : int, slice, or sequence of integers
1253+
1254+
Notes
1255+
-----
1256+
If slice passed, the resulting data will be a view
12481257
12491258
Returns
12501259
-------
1251-
column : Series
1260+
column : Series (int) or DataFrame (slice, sequence)
12521261
"""
12531262
label = self.columns[i]
12541263
if isinstance(i, slice):
1264+
# need to return view
12551265
lab_slice = slice(label[0], label[-1])
12561266
return self.ix[:, lab_slice]
12571267
else:

pandas/core/series.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -483,21 +483,24 @@ def get(self, label, default=None):
483483

484484
def iget_value(self, i):
485485
"""
486-
Return the i-th value in the Series by location
486+
Return the i-th value or values in the Series by location
487487
488488
Parameters
489489
----------
490-
i : int or slice
490+
i : int, slice, or sequence of integers
491491
492492
Returns
493493
-------
494-
value : scalar
494+
value : scalar (int) or Series (slice, sequence)
495495
"""
496496
if isinstance(i, slice):
497497
return self[i]
498498
else:
499499
label = self.index[i]
500-
return self[label]
500+
if isinstance(label, Index):
501+
return self.reindex(label)
502+
else:
503+
return self[label]
501504

502505
iget = iget_value
503506

pandas/tests/test_frame.py

+20
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,17 @@ def test_irow(self):
847847
expected = df.ix[8:14]
848848
assert_frame_equal(result, expected)
849849

850+
# verify slice is view
851+
result[2] = 0.
852+
exp_col = df[2].copy()
853+
exp_col[4:8] = 0.
854+
assert_series_equal(df[2], exp_col)
855+
856+
# list of integers
857+
result = df.irow([1, 2, 4, 6])
858+
expected = df.reindex(df.index[[1, 2, 4, 6]])
859+
assert_frame_equal(result, expected)
860+
850861
def test_icol(self):
851862
df = DataFrame(np.random.randn(4, 10), columns=range(0, 20, 2))
852863

@@ -863,6 +874,15 @@ def test_icol(self):
863874
expected = df.ix[:, 8:14]
864875
assert_frame_equal(result, expected)
865876

877+
# verify slice is view
878+
result[8] = 0.
879+
self.assert_((df[8] == 0).all())
880+
881+
# list of integers
882+
result = df.icol([1, 2, 4, 6])
883+
expected = df.reindex(columns=df.columns[[1, 2, 4, 6]])
884+
assert_frame_equal(result, expected)
885+
866886
def test_iget_value(self):
867887
for i, row in enumerate(self.frame.index):
868888
for j, col in enumerate(self.frame.columns):

pandas/tests/test_series.py

+9
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,15 @@ def test_iget(self):
342342
expected = s.ix[2:4]
343343
assert_series_equal(result, expected)
344344

345+
# test slice is a view
346+
result[:] = 0
347+
self.assert_((s[1:3] == 0).all())
348+
349+
# list of integers
350+
result = s.iget([0, 2, 3, 4, 5])
351+
expected = s.reindex(s.index[[0, 2, 3, 4, 5]])
352+
assert_series_equal(result, expected)
353+
345354
def test_getitem_regression(self):
346355
s = Series(range(5), index=range(5))
347356
result = s[range(5)]

0 commit comments

Comments
 (0)