to_datetime Value Error: at least that [year, month, day] must be specified Pandas
For me works apply
function to_datetime
:
print (dtd)
1 2 3 4 5 6
0
0 2004-01-02 2004-01-02 2004-01-09 2004-01-16 2004-01-23 2004-01-30
1 2004-01-05 2004-01-09 2004-01-16 2004-01-23 2004-01-30 2004-02-06
2 2004-01-06 2004-01-09 2004-01-16 2004-01-23 2004-01-30 2004-02-06
3 2004-01-07 2004-01-09 2004-01-16 2004-01-23 2004-01-30 2004-02-06
4 2004-01-08 2004-01-09 2004-01-16 2004-01-23 2004-01-30 2004-02-06
dtd = dtd.apply(pd.to_datetime)
print (dtd)
1 2 3 4 5 6
0
0 2004-01-02 2004-01-02 2004-01-09 2004-01-16 2004-01-23 2004-01-30
1 2004-01-05 2004-01-09 2004-01-16 2004-01-23 2004-01-30 2004-02-06
2 2004-01-06 2004-01-09 2004-01-16 2004-01-23 2004-01-30 2004-02-06
3 2004-01-07 2004-01-09 2004-01-16 2004-01-23 2004-01-30 2004-02-06
4 2004-01-08 2004-01-09 2004-01-16 2004-01-23 2004-01-30 2004-02-06
You can stack
/ pd.to_datetime
/ unstack
pd.to_datetime(dte.stack()).unstack()
explanation
pd.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
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