pydata/pandas — pull request #2922 by jreback (2013-02-25 03:49:56+00:00)
Title: ENH: add .loc attribute to provide location-based indexing

Pandas provides the .loc method in order to get purely integer based indexing. The semantics follow closely python and numpy slicing. These are 0-based indexing. When slicing, the start bounds is included, while the upper bound is excluded. Invalid selections will raise with an IndexError. Trying to use a non-integer, even a valid label will raise a ValueError

Should close #1052 (at least partially)

Includes docs and test coverage.

```
In [285]: s1 = Series(np.random.randn(8),index=range(0,16,2))

In [286]: s1
Out[286]:
0 0.341852
2 -0.547515
4 -0.180901
6 -0.355934
8 -1.313090
10 0.784062
12 -0.329752
14 -1.819607
Dtype: float64

In [287]: s1.loc[:5]
Out[287]:
0 0.341852
2 -0.547515
4 -0.180901
6 -0.355934
8 -1.313090
Dtype: float64

In [288]: s1.loc[5]
Out[288]: 0.78406202900317212

Note that setting works as well:

In [289]: s1.loc[:5] = 0

In [290]: s1
Out[290]:
0 0.000000
2 0.000000
4 0.000000
6 0.000000
8 0.000000
10 0.784062
12 -0.329752
14 -1.819607
Dtype: float64

With a DataFrame

In [291]: df1 = DataFrame(np.random.randn(8,4),index=range(0,16,2),columns=range(0,8,2))

In [292]: df1
Out[292]:
0 2 4 6
0 0.334582 1.936465 -1.120100 -0.378014
2 0.097167 0.534742 0.891384 -0.438346
4 -1.269083 0.649958 -0.350875 1.498596
6 0.088944 0.175965 0.315016 2.593186
8 -0.336016 0.153366 0.402341 0.157685
10 -0.120190 -0.997170 0.178300 -0.625006
12 -0.372354 -0.765653 -1.468988 -0.620692
14 -0.721084 -0.622696 0.648250 -1.944616

In [293]: df1.loc[:3]
Out[293]:
0 2 4 6
0 0.334582 1.936465 -1.120100 -0.378014
2 0.097167 0.534742 0.891384 -0.438346
4 -1.269083 0.649958 -0.350875 1.498596

In [294]: df1.loc[5,2]
Out[294]: 0.17829955220403318

In [295]: df1.loc[1:5,2:4]
Out[295]:
4 6
2 0.891384 -0.438346
4 -0.350875 1.498596
6 0.315016 2.593186
8 0.402341 0.157685
```