1

I create an pandas series, with a index of type float:

In [558]:  fls=pd.Series({.1:'a',.2:'b',.3:'c',.4:'d'})

Then I thought: let's use the implicit index:

In [559]: fls[1:3]
Out[559]: Series([], dtype: object)

Why is the result an empty series?

jpp
  • 159,742
  • 34
  • 281
  • 339
ericj
  • 2,138
  • 27
  • 44
  • your current index is `Float64Index([0.1, 0.2, 0.3, 0.4], dtype='float64')` – RomanPerekhrest Nov 25 '18 at 12:01
  • It's a FloatIndex so in order to avoid ambiguity with indexing like `fls[0.1:0.3]` it doesn't do positional indexing. You need to explicitly use iloc. – ayhan Nov 25 '18 at 12:19
  • Is this typical for floating indexes? Because if the index is of type string or integer, the implicit index works. So is ser=pd.Series({11:'a',12:'b',13:'c',14:'d'}), the ser[1:3] gives the 12 and 13 entries. Also when ser=pd.Series({'a':11,'b':12,'c':13,'d':14}), then ser[1:3] gives the 'b' and 'c' entries. – ericj Nov 25 '18 at 12:27
  • If the index is string then there is no ambiguity. You can use `ser['a':'c']` or `ser[1:3]` It is clear that in the first one you are indexing by label and in the second one by position. If the index is integer, it only does positional indexing (you would have to do ser.loc[11:13] for example). If it is float index, in order to allow easy access to ranges (like `fls[0:1]`) it doesn't do positional indexing. – ayhan Nov 25 '18 at 12:35

1 Answers1

1

Use iloc for integer positional indexing

print(fls.iloc[1:3])

# 0.2    b
# 0.3    c
# dtype: object

Positional indexing begins at 0. Unlike label-based indexing, but consistent with list slicing, the last position is excluded.

Use loc for label-based indexing

print(fls.loc[0.1:0.3])

# 0.1    a
# 0.2    b
# 0.3    c
# dtype: object

There's only one index

There's no such thing as an "implicit index" or "explicit index". There's only one index:

print(fls.index)

# Float64Index([0.1, 0.2, 0.3, 0.4], dtype='float64')

Pandas provides methods to query this index by position (iloc) or by label (loc).

jpp
  • 159,742
  • 34
  • 281
  • 339
  • How can it be that there is only one index if you look at the following: if ser=pd.Series({11:'a',12:'b',13:'c',14:'d'}), then ser[11:13] gives the 11,12 and 13 entries, but ser[1:3] also works although it only gives the 12 and 13 entries. Also when ser=pd.Series({'a':11,'b':12,'c':13,'d':14}), then ser['a':'c'] gives the 'a','b' and 'c' entries but ser[1:3] also works, although it only gives the 'b' and 'c' entries. So the other index is also an index? – ericj Nov 26 '18 at 21:19
  • @ericj, One index can have many values (also known as labels). Very literally you can do `fls.index.values` to get the values from your index. – jpp Nov 26 '18 at 22:23