0

I have a dataframe with Date_Birth in the following format: July 1, 1991 (as type object). How can I change the entire column to datetime?

Thanks

shitake
  • 1
  • 1
  • 1
    Please include a script that initializes the dataframe so that we can have complete, tested answers. It could be a single column with a few rows of strings in that format. – tdelaney Jun 20 '21 at 14:53
  • Check this Stack Overflow [thread](https://stackoverflow.com/questions/466345/converting-string-into-datetime) – Kabilan Mohanraj Jun 20 '21 at 15:02

1 Answers1

1

Use the pandas.to_datetime function. You can write out a format specifier in the strptime format or have python guess the format. In your case, guessing works.

>>> import pandas as pd
>>> df=pd.DataFrame({"Date":["July 1, 1991"]})
>>> pd.to_datetime(df["Date"], format="%B %d, %Y") 
0   1991-07-01
Name: Date, dtype: datetime64[ns]
>>> pd.to_datetime(df["Date"], infer_datetime_format=True) 
0   1991-07-01
Name: Date, dtype: datetime64[ns]
tdelaney
  • 73,364
  • 6
  • 83
  • 116