how to convert string array of mixed data types
<p>Let's say I have read and loaded a file into a 2D matrix of mixed data as strings(an example has been provided below)</p>

<pre><code># an example row of the matrix
['529997' '46623448' '2122110124' '2310' '2054' '2' '66' '' '2010/11/03-12:42:08' '26' 'CLEARING' '781' '30' '3' '0' '0' '1']
</code></pre>

<p>I want to convert this chunk of data into their data types to be able to do statistical analysis on it with numpy and scipy.</p>

<p>The datatype for all of the columns is integer <strong>except</strong> the 8th index this is DateTime and the 10th index is pure string.</p>

<h3>Question:</h3>

<p>What is the easiest way to this conversation?</p>

<hr>

<h1>EDIT</h1>

<p>Performance is very important than readability, I have to convert <strong>4.5m</strong> rows of data and then process them!</p>

<p>I have developed the following function to convert the 4.5m rows of the matrix, the invalid data type exception is also taken into consideration too. Although it can be improved with parallelizing the process, but it did the job OK for me, for what it worth, I am going to post it here.</p>

<pre><code>def cnvt_data(mat):
    from datetime import datetime

    _date = lambda x: datetime.strptime(x, "%Y/%m/%d-%H:%M:%S")
    # only necessary because '' should be treated as 0
    _int  = lambda x: int('0' + x)

    # specify the type parsers for each column
    parsers = 8 * [_int] + [_date, _int, str] + 6 * [_int]

    def try_parse(parse, value, _def):
        try:
            return parse(value), True
        except ValueError:
            return _def, False

    matrix = [];

    for idx in range(len(mat)):
        try:
            row = mat[idx]
            matrix.append(np.asarray([parse(input) for parse, input in zip(parsers, row)]))
        except ValueError:
            l = [];
            matrix.append([])
            for _idx, args in enumerate(zip(parsers, row)):
                val, pres = try_parse(args[0], args[1], 0)
                matrix[-1].append(val)
                if(not pres): l.append(_idx);
            print "\r[Error] value error @row %d @indices(%s): replaced with 0" %(idx, ', '.join(str(x) for x in l))

        print "\r[.] %d%% converted" %(idx * 100/len(mat)),

    print "\r[+] 100% converted."

    return matrix
</code></pre>

python arrays numpy converter mixed