1

my code:

def parsetime(v): 
    return np.datetime64( 'filename.txt',
                           datetime.datetime.strptime( v, '%H:%M' )
                           )

hor = np.genfromtxt( 'filename.txt',
                      skip_header = 3,
                      usecols     = ( 1, 3, 4, 5, 6, 7, 8, 9 ),
                      converters  = { 1: parsetime }
                      )

Got this:

TypeError: Invalid object for specifying NumPy datetime metadata

Dataset:

*****************************************************************************************
 Date__(UT)__HR:MN     Obsrv-lon Obsrv-lat Solar-lon Solar-lat      phi  PAB-LON  PAB-LAT
*****************************************************************************************
 2017-Feb-11 00:00  m    4.87809   1.86737   5.04236   0.27627   1.5995 142.1040  -0.7968
 2017-Feb-11 00:05  m    4.86722   1.86711   5.00023   0.27616   1.5965 142.1233  -0.7972
 2017-Feb-11 00:10  m    4.85641   1.86690   4.95810   0.27604   1.5941 142.1426  -0.7975
 2017-Feb-11 00:11  m    4.85426   1.86686   4.94968   0.27602   1.5937 142.1464  -0.7976
 2017-Feb-11 00:12  m    4.85211   1.86682   4.94125   0.27599   1.5933 142.1503  -0.7977
 2017-Feb-11 00:13  m    4.84997   1.86679   4.93283   0.27597   1.5930 142.1541  -0.7977
 2017-Feb-11 00:14  m    4.84782   1.86676   4.92440   0.27595   1.5926 142.1580  -0.7978
user3666197
  • 1
  • 6
  • 50
  • 92
hawk
  • 21
  • 4

1 Answers1

1

Better define converters-{}

lambda-s are great for this:

converters  = { 1: lambda aPieceOfSTRING: parsetime( aPieceOfSTRING ),
                }

as @hpaulj promoted, better solve also the so far present error in the, here only lambdified function, best excluding that at all, as was shown earlier today already in your previous, still un-accepted / un-confirmed, question:

 converters  = { 1: lambda aS: datetime.datetime.strptime( aS or "23:59", '%H:%M' ),
                 }      
user3666197
  • 1
  • 6
  • 50
  • 92
  • How does using this lambda help? It doesn't add any functionality or correct the problem in `parsetime`. – hpaulj Feb 26 '18 at 13:20
  • The **`lambda`**-constructors are common in defining the `numpy.genfromtxt()` details in `convertors` attribute. You are right in the error, so far present in the `def parsetime( v ):` syntax. Nevertheless, I am almost sure that you cannot be unaware that the very `lambda`-based mechanism of passing the argument and solving also a missing value conversion automation is a `numpy.genfromtxt()` rock-solid standard. Enjoy the day :o) ! – user3666197 Feb 26 '18 at 13:44