[Tutor] suggestions for splitting file based on date

Peter Otten __peter__ at web.de
Fri Jul 19 22:00:44 CEST 2013


Sivaram Neelakantan wrote:

> I've got some stock indices data that I plan to plot using matplotlib.
> The data is simply date, idx_close_value and my plan is to plot the
> last 30 day, 90, 180 day & all time graphs of the indices.
> 
> a) I can do the date computations using the python date libs
> b) plotting with matplotlib, I can get that done
> 
> what is the best way to split the file into the last 30 day recs, 90
> day recs when the data is in increasing time order?  My initial
> thinking is to first reverse the file, append to various 30/90/180 day
> lists for every rec > computed date for the corresponding date
> windows.
> 
> Is that the way to go or is there a better way?

I'd start with a single list for the complete data, reverse that using the 
aptly named method and then create the three smaller lists using slicing. 

For example:

>>> stock_data = range(10)
>>> stock_data.reverse()
>>> stock_data
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> stock_data[:3] # the last three days
[9, 8, 7]

On second thought I don't see why you want to reverse the data. If you omit 
that step you need to modify the slicing:

>>> stock_data = range(10)
>>> stock_data[-3:] # the last three days
[7, 8, 9]




More information about the Tutor mailing list