What happens to magic strings when accessing floating point indices in Pandas?

Following:

df = pd.DataFrame(
    {
        "a":[11, 11, 22],
        "i":[1081., 1071., 22.],
    },
)
df = df.set_index("i")
# df.index = df.set_index("i").astype(int)
print df.loc[[-99999999]]

Gets:

            a
i            
-99999999 NaN

However, if the index is more reasonably populated with ints, then Pandas reasonably complains:

KeyError: u'None of [[-99999999]] are in the [index]'

What's up with that? Why do the float and int indexes behave differently and what is the rationale for inventing magic strings?

+4
source share
1 answer

This is mistake. A KeyErrorshould be raised during use .loc[list-of-labels]if none of the labels are present. In docs , it .loc[list-of-labels]will grow in the future if any tags are missing.

+1
source

Source: https://habr.com/ru/post/1691897/


All Articles