conversion of np.array(dtype='str') in an np.array(dtype='datetime')
Question: I have a very simple python question. I need to transform the string values
within an np.array into datetime values. The string values contain the
following format: ('%Y%m%d'). Does any one know how to this? Here my test
data:

    
    
    date_str = np.array([['20121002', '20121002', '20121002'],
                         ['20121003', '20121003', '20121003'],
                         ['20121004', '20121004', '20121004']])
    

I try to convert this array with the pandas library. Here is my code:

    
    
    import pandas as pd    
    pd.to_datetime(date_str, format="%d%m%Y")
    

Please help me there should be a very simple way to convert this and note that
I'm a python beginner.


Answer: You can create a DataFrame, then [`apply`](http://pandas.pydata.org/pandas-
docs/stable/generated/pandas.DataFrame.apply.html) to it
[`pd.to_datetime`](http://pandas.pydata.org/pandas-
docs/stable/generated/pandas.to_datetime.html):

    
    
    In [68]: pd.DataFrame(date_str).apply(pd.to_datetime)
    Out[68]: 
               0          1          2
    0 2012-10-02 2012-10-02 2012-10-02
    1 2012-10-03 2012-10-03 2012-10-03
    2 2012-10-04 2012-10-04 2012-10-04
    

In order to verify the type of the result, here's the type of the first
column, for example:

    
    
    In [73]: pd.DataFrame(date_str).apply(pd.to_datetime).iloc[:, 0].dtype
    Out[73]: dtype('<M8[ns]')
    

