【发布时间】:2012-11-29 18:06:32
【问题描述】:
我读入一个文件并用 pandas DataFrame 绘制它。索引是DatetimeIndex,然后我使用ginput(1)方法得到一个点,但是我得到的坐标是错误的。
代码如下:
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
然后我使用ginput 绘制并单击图表:
df.plot()
t = pylab.ginput(n=1) #click somewhere near 13-APR-2012
但是,第一项似乎是一个浮点数
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!
文档建议它应该使用这些浮点数(来自datetonum):
In [10]: dt = pd.to_datetime('13-4-2012', dayfirst=True)
In [11]: date2num(dt)
Out[11]: 734606.0
这个浮点数是什么,如何将其转换为日期时间?
注意:如果我从数据框中删除其中一行,这将正常工作:
df1 = df.drop(ts[1], axis=0)
...
【问题讨论】:
-
我将此作为问题添加到github。
-
非常感谢您的帮助。您的编辑非常完美:)
标签: python matplotlib pandas