Skip to content Skip to sidebar Skip to footer

To_datetime Value Error: At Least That [year, Month, Day] Must Be Specified Pandas

I am reading from two different CSVs each having date values in their columns. After read_csv I want to convert the data to datetime with the to_datetime method. The formats of the

Solution 1:

You can stack / pd.to_datetime / unstack

pd.to_datetime(dte.stack()).unstack()

enter image description here

explanationpd.to_datetime works on a string, list, or pd.Series. dte is a pd.DataFrame and is why you are having issues. dte.stack() produces a a pd.Series where all rows are stacked on top of each other. However, in this stacked form, because it is a pd.Series, I can get a vectorized pd.to_datetime to work on it. the subsequent unstack simply reverses the initial stack to get the original form of dte

Solution 2:

For me works apply function to_datetime:

print(dtd)123456002004-01-02  2004-01-02  2004-01-09  2004-01-16  2004-01-23  2004-01-3012004-01-05  2004-01-09  2004-01-16  2004-01-23  2004-01-30  2004-02-0622004-01-06  2004-01-09  2004-01-16  2004-01-23  2004-01-30  2004-02-0632004-01-07  2004-01-09  2004-01-16  2004-01-23  2004-01-30  2004-02-0642004-01-08  2004-01-09  2004-01-16  2004-01-23  2004-01-30  2004-02-06dtd=dtd.apply(pd.to_datetime)print(dtd)123456002004-01-02 2004-01-02 2004-01-09 2004-01-16 2004-01-23 2004-01-3012004-01-05 2004-01-09 2004-01-16 2004-01-23 2004-01-30 2004-02-0622004-01-06 2004-01-09 2004-01-16 2004-01-23 2004-01-30 2004-02-0632004-01-07 2004-01-09 2004-01-16 2004-01-23 2004-01-30 2004-02-0642004-01-08 2004-01-09 2004-01-16 2004-01-23 2004-01-30 2004-02-06

Solution 3:

It works for me:

dtd.apply(lambda x: pd.to_datetime(x,errors = 'coerce', format = '%Y-%m-%d'))

This way you can use function attributes like above (errors and format). See more https://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html

Solution 4:

Just would like to add - errors = 'coerce' to avoid any errors / NULL values you might have

dtd = dtd.apply(pd.to_datetime(errors='coerce'))

Post a Comment for "To_datetime Value Error: At Least That [year, Month, Day] Must Be Specified Pandas"