Q: Pandas: keeping dates in order when using groupby or pivot table I have a pandas DataFrame (named "df1") with the following structure (although I have many months of daily data):
           date  WeightedReturn
0    15/07/2015        0.005128
1    15/07/2015        0.002844
2    15/07/2015        0.003055
3    15/07/2015       -0.001481
4    15/07/2015       -0.000741
5    15/07/2015       -0.000741
6    16/07/2015       -0.004253
7    16/07/2015       -0.001712
8    16/07/2015       -0.001712
9    21/07/2015       -0.000178
10   21/07/2015       -0.000089
11   21/07/2015       -0.00008

and I wish to create a new DataFrame from this which acts as a pivot table and merges the date fields and sums weighted returns for that particular date to get something like this:
  date        WeightedReturn
0 15/07/2015    0.00806425
1 16/07/2015    -0.007676
2 21/07/2015    -0.000356

I have tried using the "groupby function":
df2 = df1.groupby('date').sum()

This (sort of) works but the output then sorts the dates incorrectly as follows:
            WeightedReturn
date                      
01/09/2015        0.004803
02/09/2015        0.005144
03/08/2015       -0.000120
03/09/2015       -0.025164
04/08/2015        0.003956
04/09/2015        0.008942
05/08/2015       -0.01323

Which as you can see isn't in chronological order.
So I tried to use a pivot table function but I get very confused reading the documentation regarding it's use.
I have tried:
df2 = pandas.pivot_table(df1, values="Weighted Return", index="date",aggfunc=np.sum)

I get an error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\pandas\tools\pivot.py", line 147, in pivot_table
    table = table[values[0]]
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1791, in __getitem__
    return self._getitem_column(key)
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1798, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 1084, in _get_item_cache
    values = self._data.get(item)
  File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 2851, in get
    loc = self.items.get_loc(item)
  File "C:\Python27\lib\site-packages\pandas\core\index.py", line 1578, in get_loc
    return self._engine.get_loc(_values_from_object(key))
  File "pandas\index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas\index.c:3811)
  File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3691)
  File "pandas\hashtable.pyx", line 697, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12336)
  File "pandas\hashtable.pyx", line 705, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12287)
KeyError: 'Weighted Return'

Could some fine fellow point out where I am going wrong please?

A: If you don't want groupby to sort the values (its default behaviour), just pass in sort=False:
>>> df.groupby('date', sort=False).sum()
            WeightedReturn
date                      
15/07/2015        0.008064
16/07/2015       -0.007677
21/07/2015       -0.000347

The values will be ordered as they first appear in the column. Alternatively you can convert the date column to the datetime64 type and then use groupby as before: at the moment you just have date strings which get sorted lexicographically.
The error with your pivot table is because you entered the column name as "Weighted Return" (note the space) instead of "WeightedReturn". However, pivot_table will always return a sorted index, which goes back to your original problem. 
