Conversion to datetime object leads to crash of numpy functions
Question: My python script produces an strange error and I have no clue why. Maybe
anybody else has an idea. Try to provide a example everybody else should be
able to run.

    
    
    import datetime 
    import numpy as np
    
    date = np.array([20120203123054, 20120204123054]) #date format: YYYYMMDDhhmmss
    longitude = np.array([52., 53.])
    latitude = np.array([-22.0, -23.0])
    
    # Loop to convert date into datetime object 
    date_new = []
    for j in range(len(date)):  
        date_string = str(date[j])
        dt=datetime.datetime.strptime(date_string[:],'%Y%m%d%H%M%S')
        date_new.append(dt) 
    
    data = np.array([date, longitude, latitude]) 
    data_new = np.array([date_new, longitude, latitude]) 
    
    #function to calculate distance between two locations (fixed location: 
    #longitude=50.,latitude=-20.)
    def calculate_distance(longi, lati):
        distance = []
        latitude = ((50. + lati)/2)* 0.01745
        dx = 111.3 * np.cos(latitude) * (50. - longi)
        dy = 111.3 * (-20. - lati)
        d = np.sqrt(dx * dx + dy * dy)
        distance.append(d)  
        return distance
    
    #call function
    calculate_distance(data[1], data[2]) # Script works!
    calculate_distance(data_new[1], data_new[2]) # Script doesn't work!  
                                                 # see Traceback below
    

Why does it crash?

    
    
    Traceback(most recent call last):
    File "data_analysis.py" line 85,
    dx = 111.3 * cos(latitude) * (lon_station - longitude)
    AttributeError: cos
    


Answer: `numpy` arrays don't play so well with heterogeneous types. When you made
`data_new`, you were storing different kinds of Python objects in it, and so
that forced the `dtype` to be `object` to be broad enough to handle
everything.

    
    
    >>> data
    array([[  2.01202031e+13,   2.01202041e+13],
           [  5.20000000e+01,   5.30000000e+01],
           [ -2.20000000e+01,  -2.30000000e+01]])
    >>> data.dtype
    dtype('float64')
    >>> data_new
    array([[datetime.datetime(2012, 2, 3, 12, 30, 54),
            datetime.datetime(2012, 2, 4, 12, 30, 54)],
           [52.0, 53.0],
           [-22.0, -23.0]], dtype=object)
    >>> data_new.dtype
    dtype('O')
    

Since taking the cosine of an arbitrary object doesn't make sense, this isn't
implemented:

    
    
    >>> np.cos(data[1])
    array([-0.16299078, -0.91828279])
    >>> np.cos(data_new[1])
    Traceback (most recent call last):
      File "<ipython-input-87-00101e4be00a>", line 1, in <module>
        np.cos(data_new[1])
    AttributeError: cos
    

You can return to `float` type (unfortunately it doesn't look like you can use
a `view` with object arrays):

    
    
    >>> np.cos(data_new[1].astype(float))
    array([-0.16299078, -0.91828279])
    

FWIW I prefer [`pandas`](http://pandas.pydata.org) DataFrames when working
with this kind of mixed-type data, but people's preferences vary. I don't know
enough about numpy structured arrays to know if you can mix object dtypes and
more primitive dtypes, although I didn't think so.

