Python Forum
Convert df column to datetime format - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Convert df column to datetime format (/thread-21781.html)



Convert df column to datetime format - LarsCordes - Oct-14-2019

Hi all,

I'm trying to convert a dataframe column to a datetime format using
pd.to_datetime(dataframe.column,infer_datetime_format=True)


The column contains some 2,000 dates formatted YYYY-mm-dd. However, when I pass the relevant column, I receive the following error:

ValueError: to assemble mappings requires at least that [year, month, day] be specified: [day,month,year] is missing

Is there something different I need to do in order to ensure the df column is formatted correctly before passing to the to_datetime function?

Thanks for any help, this is really frustrating me!


RE: Convert df column to datetime format - perfringo - Oct-14-2019

You should pass date format parameter to pandas.to_datetime.

YYYY-mm-dd -> '%Y-%m-%d' -> format='%Y-%m-%d'


RE: Convert df column to datetime format - LarsCordes - Oct-14-2019

(Oct-14-2019, 11:59 AM)perfringo Wrote: You should pass date format parameter to pandas.to_datetime.

YYYY-mm-dd -> '%Y-%m-%d' -> format='%Y-%m-%d'

Thanks, but I have tried it specifying these formats in a few different ways, and it continues to return the same error "ValueError: to assemble mappings requires at least that [year, month, day] be specified: [day,month,year] is missing"

The pandas.to_datetime documentation states that it should take an argument as 'integer, float, string, datetime, list, tuple, 1-d array, Series'. Does a df.column meet any of these definitions, or does it need to be converted into a Series or np.array or something first?