Search code examples
pythondatematplotlibgenfromtxt

Import 36 columns in python for plotting, first column is date


I am trying to plot water-level hydrogaphs for multiple wells. The data are in a text file with the first column a date in the format 'yyyymmdd'. In this particular case, there are 35 other columns with float numbers.

I have been trying to use genfromtxt, but I don't want to have to define all 36 dtypes.

I tried dtype=None with converters, but then I get the message that the converter is locked and cannot be updated.


Solution

  • I'm surprised you can't use np.genfromtxt with a converter argument to transform your first column into either :

    • a np.datetime64 object (as @DSM suggests, provided you have a version of numpy recent enough (>1.6.1))
    • a np.object, with a converter as:

      converter={0:lambda d: datetime.datetime.strptime(d,"%Y%m%d")

    If you don't want to define the dtype yourself, you could use dtype=None. It's not that good of an idea, though, as this option is notably slower than giving an explicit dtype. But as the documentation tells you, you can use a tuple to define your dtype, so something like:

    dtype=tuple([np.datetime64] + [float]*35)
    

    or

    dtype=tuple([np.object] + [float]*35)
    

    could work.