Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataFrame.index.asof() bug #8245

Closed
ifmihai opened this issue Sep 11, 2014 · 5 comments · Fixed by #8246
Closed

DataFrame.index.asof() bug #8245

ifmihai opened this issue Sep 11, 2014 · 5 comments · Fixed by #8246
Labels
Bug Datetime Datetime data dtype
Milestone

Comments

@ifmihai
Copy link

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 TomAugspurger added this to the 0.15.0 milestone Sep 11, 2014
@TomAugspurger
Copy link
Contributor

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

@TomAugspurger
Copy link
Contributor

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

@TomAugspurger
Copy link
Contributor

@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 TomAugspurger removed the Bug label Sep 11, 2014
@jreback
Copy link
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

@jreback jreback added Bug Datetime Datetime data dtype labels Sep 11, 2014
@TomAugspurger
Copy link
Contributor

OK. I'll do a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants