Skip to content

Ginput giving wrong date for datetimeindex plot #2503

@hayd

Description

@hayd
Contributor

I rewrote this StackOverflow question to include an example. I think this looks like a bug (possibly in num2date or date2num)?


I read-in a file and plot it with pandas DataFrame. The index is DatetimeIndex, and then I use ginput(1) method to get one point, however the coordinate which I get is wrong.

The code is as follows:

import pandas as pd
from matplotlib.dates import num2date, date2num
ts = pd.date_range('2012-04-12,16:13:09', '2012-04-14,00:13:09', freq='H')
df = pd.DataFrame(index=ts)
df[0] = 20.6

I then plot and click on the graph using ginput:

df.plot()
t = pylab.ginput(n=1) #click somewhere near 13-APR-2012

However, the first item appears to be a float

In [8]: x = t[0][0] # ~ 370631.67741935479

In [9]: num2date(x)
Out[9]: datetime.datetime(1015, 10, 3, 16, 15, 29, 32253, tzinfo=<matplotlib.dates._UTC object at 0x104196550>)
# this is way out!

The docs suggest that it should be using these floats (from datetonum):

In [10]: dt = pd.to_datetime('13-4-2012', dayfirst=True)

In [11]: date2num(dt)
Out[11]: 734606.0

What is this float, and how can I convert it to a datetime?

Note: If I remove one of the rows from the dataframe this works correctly:

df1 = df.drop(ts[1], axis=0)
...

Activity

changhiskhan

changhiskhan commented on Dec 12, 2012

@changhiskhan
Contributor

I answered on stackoverflow. there's some conversion going on in the first case when the index has a regular frequency

wesm

wesm commented on Apr 9, 2013

@wesm
Member

Moved this to 0.12

jreback

jreback commented on Sep 22, 2013

@jreback
Contributor

@cpcloud want to assign yourself? (for 0.14)

ghost assigned on Sep 22, 2013
cpcloud

cpcloud commented on Sep 22, 2013

@cpcloud
Member

sure thing 😄

cpcloud

cpcloud commented on Dec 12, 2013

@cpcloud
Member

@jreback, @jtratner What should this return? I think Chang's solution is fine, not sure this is under pandas purview, per se.

removed their assignment
on Feb 21, 2014
jreback

jreback commented on Mar 11, 2014

@jreback
Contributor

@hayd close this, or just push to 0.15 as a reminder?

modified the milestones: 0.15.0, 0.14.0 on Mar 28, 2014
cpcloud

cpcloud commented on Jun 9, 2014

@cpcloud
Member

Closing, as this works.

fix

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @wesm@cpcloud@changhiskhan@jreback@hayd

        Issue actions

          Ginput giving wrong date for datetimeindex plot · Issue #2503 · pandas-dev/pandas