Repository: statsmodels/statsmodels

Issue:
[tsa] Why is the data sorted in the seasonal plot
At first, it seems counter intuitive to sort the Series when plotting a seasonal_plot (month- or quarter-, e.g.), what is the reason for this ? https://github.com/statsmodels/statsmodels/blob/master/statsmodels/graphics/tsaplots.py#L190

The result of a month plot makes more sense (to me) when it is not sorted. Maybe it should be optional ?

I've also added a trendline to each group, which adds some info on the behaviour of the changes through the years: this is an example of 30 years temperature data, monthly plotted: 

![temp](https://cloud.githubusercontent.com/assets/916937/5216481/3a248400-7634-11e4-8e71-af0a86700ef1.png)

What do you reckon ?



Discussion:
I don't know the motivation of this, but making sorting optional sounds good to me.
@jseabold  is the author

Just based on looking at your plot. Would it also make sense to add seasonal demeaning to align all month plots by mean?
or is that a deseasonalized_plot instead of a seasonal plot?

@ThomasLecocq Do you want to prepare a PR?

in my case I don't want the demeaning because I want to show the seasonality... By the way, extending this with `seaborn` is also big fun (not the same data):

![index](https://cloud.githubusercontent.com/assets/916937/5228994/d9887942-7711-11e4-9aac-18ad58a37591.png)

@josef-pkt  yes, I will prepare a PR !

That doesn't look right to me either. I think that's likely a bug (or changed behavior in pandas). I think it should be `df = df.sort_index()`, if anything.

PR would be welcome here. Adding the bug label, because I think this is wrong.

any PR for 0.7, or move to 0.8 ?

When is 0.7 planned? I could work on that before the end of this week, but not today...

my guess is between one and two weeks from now, assuming no unexpected problems.
If the PR looks reasonably "safe", then it could be merged in the last minute.

have not forgotten.. will do it tomorrow !

Thanks, there is still work for a few days left before we can start with the release

@josef-pkt I am not really sure why df.sort_index() is even needed? When you run groupby in pandas, doesn't it sort by index?

@kiyoto  I don't know or I'm not sure, I never went much beyond the use-a-recipe stage with pandas.  Asking on the mailing list might provide an answer.

djeeeeeez... again completely forgotten this one... pff... will do it asap, really...


Resolving patch:
diff --git a/statsmodels/graphics/tsaplots.py b/statsmodels/graphics/tsaplots.py
--- a/statsmodels/graphics/tsaplots.py
+++ b/statsmodels/graphics/tsaplots.py
@@ -212,18 +212,26 @@ def seasonal_plot(grouped_x, xticklabels, ylabel=None, ax=None):
     grouped_x : iterable of DataFrames
         Should be a GroupBy object (or similar pair of group_names and groups
         as DataFrames) with a DatetimeIndex or PeriodIndex
+    xticklabels : list of str
+        List of season labels, one for each group.
+    ylabel : str
+        Lable for y axis
+    ax : Matplotlib AxesSubplot instance, optional
+        If given, this subplot is used to plot in instead of a new figure being
+        created.
     """
     fig, ax = utils.create_mpl_ax(ax)
     start = 0
     ticks = []
     for season, df in grouped_x:
         df = df.copy() # or sort balks for series. may be better way
-        sort_values(df, inplace=True)
+        df.sort_index()
         nobs = len(df)
         x_plot = np.arange(start, start + nobs)
         ticks.append(x_plot.mean())
         ax.plot(x_plot, df.values, 'k')
-        ax.hlines(df.values.mean(), x_plot[0], x_plot[-1], colors='k')
+        ax.hlines(df.values.mean(), x_plot[0], x_plot[-1], colors='r',
+                  linewidth=3)
         start += nobs
 
     ax.set_xticks(ticks)


Test patch:
diff --git a/statsmodels/graphics/tests/test_tsaplots.py b/statsmodels/graphics/tests/test_tsaplots.py
--- a/statsmodels/graphics/tests/test_tsaplots.py
+++ b/statsmodels/graphics/tests/test_tsaplots.py
@@ -1,10 +1,11 @@
 from statsmodels.compat.python import lmap, map
 import numpy as np
 import pandas as pd
-from numpy.testing import dec
+from numpy.testing import dec, assert_equal
 
 import statsmodels.api as sm
-from statsmodels.graphics.tsaplots import plot_acf, plot_pacf, month_plot, quarter_plot
+from statsmodels.graphics.tsaplots import (plot_acf, plot_pacf, month_plot,
+                                           quarter_plot, seasonal_plot)
 import statsmodels.tsa.arima_process as tsp
 
 
@@ -136,3 +137,19 @@ def test_plot_quarter():
     dta.index = pd.PeriodIndex((x[0] for x in map(parser, dates)),
                                    freq='Q')
     quarter_plot(dta.unemp)
+
+@dec.skipif(not have_matplotlib)
+def test_seasonal_plot():
+    rs = np.random.RandomState(1234)
+    data = rs.randn(20,12)
+    data += 6*np.sin(np.arange(12.0)/11*np.pi)[None,:]
+    data = data.ravel()
+    months = np.tile(np.arange(1,13),(20,1))
+    months = months.ravel()
+    df = pd.DataFrame([data,months],index=['data','months']).T
+    grouped = df.groupby('months')['data']
+    labels = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
+    fig = seasonal_plot(grouped, labels)
+    ax = fig.get_axes()[0]
+    output = [tl.get_text() for tl in ax.get_xticklabels()]
+    assert_equal(labels, output)