Repository: pandas-dev/pandas

Issue:
Matplotlib cursor position wrong after using asfreq method to change freq of DateTimeIndex from None to something
After using the `asfreq` method to change the frequency of a time-series DataFrame from `None` to something e.g. `15Min` the cursor position in matplotlib graphs of that DataFrame is no longer correct (usually shows a datetime just after the unix epoch).  The following demonstrates this (NB dt in df1 is not a constant):

```
df1 = pandas.read_csv('tseries1.csv', names=['tstamp', 'Q'], parse_dates=True, 
    index_col='tstamp').clip_lower(0).fillna(0)
df1['T'] = pandas.read_csv('tseries2.csv', names=['tstamp', 'T'], parse_dates=True, 
    index_col='tstamp', squeeze=True).clip_lower(0).fillna(0)

df2 = df1.asfreq(freq='15Min', method='ffill')
# NB df1.index.freq is None
# NB df2.index.freq is <15 * Minutes>
df1.plot()
df2.plot()
plt.show()
```

I find the Matplotlib cursor position to be invaluable when looking for features in very long time-series.

Versions:
- pandas master (commit ID 764b444)
- numpy 1.8
- matplotlib 1.3.0

Matplotlib cursor position wrong after using asfreq method to change freq of DateTimeIndex from None to something
After using the `asfreq` method to change the frequency of a time-series DataFrame from `None` to something e.g. `15Min` the cursor position in matplotlib graphs of that DataFrame is no longer correct (usually shows a datetime just after the unix epoch).  The following demonstrates this (NB dt in df1 is not a constant):

```
df1 = pandas.read_csv('tseries1.csv', names=['tstamp', 'Q'], parse_dates=True, 
    index_col='tstamp').clip_lower(0).fillna(0)
df1['T'] = pandas.read_csv('tseries2.csv', names=['tstamp', 'T'], parse_dates=True, 
    index_col='tstamp', squeeze=True).clip_lower(0).fillna(0)

df2 = df1.asfreq(freq='15Min', method='ffill')
# NB df1.index.freq is None
# NB df2.index.freq is <15 * Minutes>
df1.plot()
df2.plot()
plt.show()
```

I find the Matplotlib cursor position to be invaluable when looking for features in very long time-series.

Versions:
- pandas master (commit ID 764b444)
- numpy 1.8
- matplotlib 1.3.0



Discussion:
can post a link or the file for csvs

or post a simple way to recreate them?

Hi jreback.  Here's a self-contained example:

```
import pandas
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt

tstamps = np.array([
       datetime(2011, 10,  1,  0,  0),
       datetime(2011, 10, 10,  0, 15),
       datetime(2011, 10, 20,  0, 30), 
       datetime(2012, 11,  6, 23, 15),
       datetime(2012, 11,  8, 23, 30),
       datetime(2012, 11,  9, 23, 45)])

df_orig = pandas.DataFrame(data={'col1':np.linspace(3, 30, 6)}, index=tstamps)
df_orig.plot()
df_new = df_orig.asfreq(freq='15Min', method='ffill')
df_new.plot()
plt.show()
```

gr8 thanks.....would welcom a PR for this.....!

Crude workaround: edit `_make_plot` method in `tools/plotting.py` and clobber 

```
if not self.x_compat and self.use_index and self._use_dynamic_x()
```

so that it always evaluates to `False`.  

can post a link or the file for csvs

or post a simple way to recreate them?

Hi jreback.  Here's a self-contained example:

```
import pandas
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt

tstamps = np.array([
       datetime(2011, 10,  1,  0,  0),
       datetime(2011, 10, 10,  0, 15),
       datetime(2011, 10, 20,  0, 30), 
       datetime(2012, 11,  6, 23, 15),
       datetime(2012, 11,  8, 23, 30),
       datetime(2012, 11,  9, 23, 45)])

df_orig = pandas.DataFrame(data={'col1':np.linspace(3, 30, 6)}, index=tstamps)
df_orig.plot()
df_new = df_orig.asfreq(freq='15Min', method='ffill')
df_new.plot()
plt.show()
```

gr8 thanks.....would welcom a PR for this.....!

Crude workaround: edit `_make_plot` method in `tools/plotting.py` and clobber 

```
if not self.x_compat and self.use_index and self._use_dynamic_x()
```

so that it always evaluates to `False`.  


Resolving patch:
diff --git a/pandas/tseries/plotting.py b/pandas/tseries/plotting.py
--- a/pandas/tseries/plotting.py
+++ b/pandas/tseries/plotting.py
@@ -83,8 +83,7 @@ def tsplot(series, plotf, **kwargs):
     ax.set_xlim(left, right)
 
     # x and y coord info
-    tz = series.index.to_datetime().tz
-    ax.format_coord = lambda t, y : "t = {}    y = {:8f}".format(datetime.fromtimestamp(t, tz), y)
+    ax.format_coord = lambda t, y: "t = {}  y = {:8f}".format(Period(ordinal=int(t), freq=ax.freq), y)
 
     return lines
 


Test patch:
diff --git a/pandas/tseries/tests/test_plotting.py b/pandas/tseries/tests/test_plotting.py
--- a/pandas/tseries/tests/test_plotting.py
+++ b/pandas/tseries/tests/test_plotting.py
@@ -131,6 +131,21 @@ def test_get_datevalue(self):
         self.assertEqual(get_datevalue('1/1/1987', 'D'),
                          Period('1987-1-1', 'D').ordinal)
 
+    @slow
+    def test_ts_plot_format_coord(self):
+        def check_format_of_first_point(ax, expected_string):
+            first_line = ax.get_lines()[0]
+            first_x = first_line.get_xdata()[0].ordinal
+            first_y = first_line.get_ydata()[0]
+            self.assertEqual(expected_string, ax.format_coord(first_x, first_y))
+
+        annual = Series(1, index=date_range('2014-01-01', periods=3, freq='A-DEC'))
+        check_format_of_first_point(annual.plot(), 't = 2014  y = 1.000000')
+
+        # note this is added to the annual plot already in existence, and changes its freq field
+        daily = Series(1, index=date_range('2014-01-01', periods=3, freq='D'))
+        check_format_of_first_point(daily.plot(), 't = 2014-01-01  y = 1.000000')
+
     @slow
     def test_line_plot_period_series(self):
         for s in self.period_ser: