Skip to content

COMPAT: issues w.r.t. deprecating float indexers #12322

Closed
@jreback

Description

@jreback

from #12246

@jorisvandenbossche

Some after merge feedback:

I took a look and I also like the logic now as how you explained it in your last post (for label based indexing: if the label evaluates as equal, then it is interpreted as the existing label)

A few things:

  • The whatsnew needs to be updated (as it still says it raises now for all cases, as you started the PR)
maybe mention that it only is about positional indexing? Otherwise people could think FloatIndex is removed.
  • (minor) When using iloc with a float (so the case it still should raise an error), the error message is not fully correct:

    In [76]: s2 = s.copy()
    
    In [77]: s2.iloc[2.0] = 10
    TypeError: cannot do label indexing on <class 'pandas.indexes.range.RangeIndex'>
    with these indexers [2.0] of <type 'float'>
    

    This is not 'label' indexing, but 'integer' or 'positional' indexing

  • The behaviour of using a float indexer with ix on a non-numerical index has changed:

    In [78]: s3 = pd.Series(range(3), index=list('abc'))
    
    In [79]: s4 = s3.copy()
    
    In [80]: s4.ix[1.0] = 10
    
    In [81]: s4
    Out[81]:
    a       0
    b       1
    c       2
    1.0    10
    dtype: int64
    
    In [82]: s4.index
    Out[82]: Index([u'a', u'b', u'c', 1.0], dtype='object')
    
    In [83]: pd.__version__
    Out[83]: '0.17.1+350.g5f7c9e9'
    

    and with 0.16.2

    In [1]: pd.__version__
    Out[1]: '0.16.2'
    
    In [2]: s3 = pd.Series(range(3), index=list('abc'))
    
    In [3]: s4 = s3.copy()
    
    In [4]: s4.ix[1.0] = 10
    C:\Anaconda\lib\site-packages\pandas\core\index.py:805: FutureWarning: scalar i
    dexers for index type Index should be integers and not floating point
    type(self).__name__),FutureWarning)
    
    In [5]: s4
    Out[5]:
    a     0
    b    10
    c     2
    dtype: int64
    
    In [6]: s4.index
    Out[6]: Index([u'a', u'b', u'c'], dtype='object')
    
    

    The change is logical, as before the float was interpreted as a positional indexer (and for this the warning was raised). But now a float cannot be a positional indexer anymore, so it is interpreted as a new label.
    To be clear, I think this change is OK, but just wanted to point out a case where this change will not raise an error but alter your results (worth mentioning in the whatsnew docs?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Compatpandas objects compatability with Numpy or Python functions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions