Skip to content

API/DOC: unclear scope of index.asof #8815

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

From http://stackoverflow.com/questions/26918045/python-pandas-typeerror-cannot-compare-type-timestamp-with-type-float

The docstring of Index.asof says:

In [73]: pd.Index.asof?
Type:        instancemethod
String form: <unbound method Index.asof>
File:        c:\anaconda\lib\site-packages\pandas\core\index.py
Definition:  pd.Index.asof(self, label)
Docstring:
For a sorted index, return the most recent label up to and including
the passed label. Return NaN if not found

while asof only works for a time series.

  • Clarify the docs that asof only works for DatetimeIndex (and PeriodIndex?)

  • Why is this actually restricted to timestamps? This functions seems also possibly useful to me for other index types? Something like

    In [74]: idx = pd.Index([1,2,3,8,9])
    
    In [75]: idx.asof(5)    # gives now TypeError
    3
    

    Instead of having to do:

    In [78]: idx[idx.searchsorted(5) - 1]
    Out[78]: 3
    

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions