quandl / quandl-python

MIT License
1.38k stars 338 forks source link

Sort dates ascending for compatibility with Pandas timeseries conventions #3

Closed jlowin closed 11 years ago

jlowin commented 11 years ago

Quandl.com uses a descending convention for sorting dates (newest information at the top). However, Pandas uses an ascending convention. Therefore, with data imported via the current function, Pandas timeseries methods like data.diff(), data.pct_change(), or data.shift() give unexpected results (looking forward in time, rather than back) and advanced timeseries indexing like data.ix['Nov 2010' : 'May 2011'] fails with an error.

This PR sorts imported data in ascending order by default in order to keep compatibility with Pandas, but lets users revert to descending order by passing ascending=False. This way equivalency can be maintained with manually downloaded datasets (that are in descending order).

Please note that this is a very small PR -- only 5 lines of functionality in the first commit. The second commit makes a number of minor PEP8/formatting fixes that clean up the code but add no functionality. However, it touches many lines, possibly making this appear like a much larger PR than it actually is.

RaymondMcT commented 11 years ago

Quandl's API gives the choice of whether you'd like to have the data returned ascending or descending so there's no need to flip the array around within python. You can just append this to the url string.

sort_order=asc|desc

Example:

http://www.quandl.com/api/v1/datasets/DOE/RWTC.csv?collapse=quarterly&sort_order=asc

jlowin commented 11 years ago

@RaymondMcT Great! That makes this even cleaner.