Search code examples
pythonpandasdatetimedate-conversion

Converting string date to a date and dropping the time in a dataframe


I am trying to convert a string date and time (ex: "6/30/2015 0:00") to just a date in this format: %Y/%m/%d. I am trying to do this for all values in a dataframe column. I almost have it but can't seem to get rid of the time part. I also need to apply this method to another column that might have null/blank values. This is what I have tried, any suggestions on how to get this to work?

cnms_df['STATUS_DATE'] = pd.to_datetime(cnms_df['STATUS_DATE'], format="%Y/%m/%d")

ValueError: unconverted data remains: 0:00

***Sample data (does not include all fields; which are 30+ long) enter image description here

Here is a sample series of the first 5 values for STATUS_DATE:

0    6/30/2015 0:00
1    6/24/2015 0:00
2    6/24/2015 0:00
3    6/24/2015 0:00
4    6/24/2015 0:00
Name: STATUS_DATE, dtype: object

Solution

  • Try:

    cnms_df['STATUS_DATE'] = pd.to_datetime(cnms_df['STATUS_DATE'][:cnms_df['STATUS_DATE'].index(' ')[0]], format="%Y/%m/%d")
    

    Assuming your dates are consistent, it's just a matter of chopping off the time segment using split. Hope that helps.