[SciPy-user] Sort by first n columns in an array

Neil Crighton neilcrighton@gmail....
Wed Jul 8 18:14:36 CDT 2009

Dharhas Pothina <Dharhas.Pothina <at> twdb.state.tx.us> writes:

> I am reading in an array with np.genfromtxt and I need to sort it by the first
6 columns (ie year,month etc). I
> do not know the number of column before reading the array in. 
> >From the documentation for sorting it looks like I can use the 'order'
keyword to do what I need if I have a
> structured array with names for each column. I haven't been able to work out
how to convert the array I have
> to an ordered array though. Also as far as I can tell I can't set the dtype
with field names in np.genfromtxt
> without previously knowing the number of columns in the data file.
> I don't want to use the timeseries toolkit for this since it is on a machine
that hasn't got that package installed.
> I know I must be missing something simple. Any help is appreciated.
> - dharhas

Something like this should work:

data = np.genfromtxt(filename, dtype=None, usecols=(0,1,2),
data.sort(order=['year', 'month', 'day'])


More information about the SciPy-user mailing list