Pandas Index | get_loc method
Start your free 7-days trial now!
Pandas Index.get_loc(~)
method returns the location of the given value in the source Index
.
Parameters
1. key
| label
The value you want to know the location of.
2. method
link | None
or string
| optional
How to deal with the case when key
is not present in the source Index
:
Method | Description |
---|---|
| Throw an error. |
| Return the integer index of the previous index value. |
| Return the integer index of the next index value. |
| Return the integer index of the nearest index value. As a tie-breaker, the larger index value will take precedence. |
By default, method=None
.
Your Index
must be monotonic, otherwise specifying method
will throw an error.
3. tolerance
link | int
or float
| optional
The maximum difference allowed between the key
and the selected index value:
abs(index[loc] - key) <= tolerance
This is only relevant if method is not None
. By default, no tolerance is set.
Return Value
The return type depends on the following cases:
Case | Return Type |
---|---|
If a single match is found. |
|
If multiple matches are found and the source |
|
Otherwise | a Numpy array of booleans |
Examples
Single match
To get the integer index of an unique value:
idx = pd.Index(["a","b","c"])idx.get_loc("c")
2
Here, the integer index of the value "c"
is 2
.
Multiple matches
When index is monotonic
To get the integer indexes of a value that is non-unique in a monotonic Index
:
idx = pd.Index(["a","b","c","c"])slice = idx.get_loc("c")slice
slice(2, 4, None)
Since idx
is monotonically increasing, the return type here is a slice
object. Note that you can access the properties of slice
like so:
When index is not monotonic
To get the locations of a value that is non-unique in a non-monotonic Index
:
idx = pd.Index(["a","b","c","b"])idx.get_loc("b")
array([False, True, False, True])
Here, the return type is a Numpy array of booleans where True
indicates the presence of the value.
Specifying method
By default, when the supplied value does not exist in the Index
, a KeyError
is thrown:
idx = pd.Index([3,5,8])idx.get_loc(6)
KeyError: 6
In such cases when the value is not present in Index
, we can specify method
to get the integer index of a neighbouring value. Note that our Index
must be monotonic, otherwise specifying method
will throw an error.
ffill
When method="ffill"
, then the integer index of the previous value will be returned:
idx = pd.Index([3,5,8])idx.get_loc(7, method="ffill")
1
Here, the previous value of 7
is 5
, so the integer index of 5
is returned.
bfill
When method="bfill"
, then the integer index of the next value will be returned:
idx = pd.Index([3,5,8])idx.get_loc(6, method="bfill")
2
Here, the next value of 6
is 8
, so the integer index of 8
is returned.
nearest
When method="nearest"
, then the integer index of the nearest value will be returned:
idx = pd.Index([3,5,8])idx.get_loc(6, method="nearest")
1
Here, the nearest value to 6
is 5
(as opposed to 8
), so the integer index of 5
is returned.
Specifying tolerance
When an exact match is not found, then setting tolerance
imposes a maximum acceptable difference between the supplied value and the selected index value:
idx = pd.Index([3,5,8])idx.get_loc(6, method="nearest", tolerance=0.5)
KeyError: 6
Here, we get an error because the nearest index value is 5
, and the difference between 5
and the supplied value 6
is larger than the specified tolerance of 0.5
.