Skip to content

Ginput giving wrong date for datetimeindex plot #2503

@hayd

Description

@hayd

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)
...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions