DataFrame.index.asof() bug #8245

Closed
ifmihai opened this Issue Sep 11, 2014 · 5 comments

Comments

Projects
None yet
3 participants

ifmihai commented Sep 11, 2014

An example is best:

2 Inn: df = P.DataFrame(index=P.date_range('2014-9-6 1:30', '2014-9-9', freq='6H'))
3 Inn: df
3 Out: 


2014-09-06 01:30:00
2014-09-06 07:30:00
2014-09-06 13:30:00
2014-09-06 19:30:00
2014-09-07 01:30:00
2014-09-07 07:30:00
2014-09-07 13:30:00
2014-09-07 19:30:00
2014-09-08 01:30:00
2014-09-08 07:30:00
2014-09-08 13:30:00
2014-09-08 19:30:00
2014-09-09 01:30:00

4 Inn: df.index.asof('2014-9-8')  # wrong
4 Out: Timestamp('2014-09-08 00:00:00')

5 Inn: df.index.asof(P.to_datetime('2014-9-8'))  # right
5 Out: Timestamp('2014-09-07 19:30:00', offset='6H')

7 Inn: P.version.version
7 Out: '0.14.1'

df.index.asof() appears to know how to handle datetime strings, but it cant

TomAugspurger added the Bug label Sep 11, 2014

TomAugspurger added this to the 0.15.0 milestone Sep 11, 2014

Contributor

TomAugspurger commented Sep 11, 2014

Thanks for the bug report. Try to make sure your examples are copy-pasteable (the assumed import for pandas is import pandas as pd).

Contributor

TomAugspurger commented Sep 11, 2014

The problems here in pandas/core/index.py under Index.asof
label is 2014-9-8, self is the DatetimeIndex.

        if label not in self:
            loc = self.searchsorted(label, side='left')
            if loc > 0:
                return self[loc - 1]
            else:
                return np.nan

        if not isinstance(label, Timestamp):
            label = Timestamp(label)
        return label

But label is in the index, so it gets converted to a Timestamp and returned:

In [8]: idx = pd.date_range('2014-9-6 1:30', '2014-9-9', freq='6H')

In [9]: '2014-9-8' in idx
Out[9]: True
Contributor

TomAugspurger commented Sep 11, 2014

@jreback am I correct in thinking that we intentionally return true for partial matches like '2014' in idx so that we can do partial matching?

If so, @ifmihai would you want to submit a pull request for a section in the documentation explaining that you should pass Timestamps to asof if you want exact matches?

TomAugspurger removed the Bug label Sep 11, 2014

Contributor

jreback commented Sep 11, 2014

@TomAugspurger yes that's right.

I think that might be a perf shortcut (e.g. its already their). Maybe need to just wrap them
Timestamp('2014-9-8') in idx -> False so this doesn't happen

Contributor

TomAugspurger commented Sep 11, 2014

OK. I'll do a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment