There is a DataFrame Views, which contains the Series: Email, Role, Topics, Date. Date contains strings like '2016/01/09'. Using the datetime.strptime method, I want to convert these strings to dates, but instead of the result I need, I get the replacement of the string values ​​by one value in the date format: '2016/01/09', which is the first argument of the method. The question is how to write so that the lines change only the format, not the value?

views.Date = datetime.strptime('2016/01/09', '%Y/%m/%d') 

    1 answer 1

    Use the vectorized to_datetime () method:

    Example:

     import pandas as pd In [22]: df Out[22]: email date 0 aaa@aaa.aaa 2016/01/09 1 bbb@bbb.bbb 2016/11/11 2 ccc@ccc.ccc 2016/12/30 In [23]: df.dtypes Out[23]: email object date object # dtype: object (string) dtype: object In [24]: df['date'] = pd.to_datetime(df['date']) In [25]: df Out[25]: email date 0 aaa@aaa.aaa 2016-01-09 1 bbb@bbb.bbb 2016-11-11 2 ccc@ccc.ccc 2016-12-30 In [26]: df.dtypes Out[26]: email object date datetime64[ns] # dtype: datetime64 dtype: object 
    • MaxU, thank you very much, solved the problem in your way! - Denis Novik
    • @DenisNovik, always happy to help :) - MaxU