The pandas.to_datetime
function transforms a string to a date object. The string representation of the date can take multiple formats.
To correctly parse these strings, pandas.to_datetime
provides several arguments to setup the parsing, such as dayfirst
or
yearfirst
. For example setting dayfirst
to True
indicates to pandas.to_datetime
that the date and
time will be represented as a string with the shape day month year time
. Similarly with yearfirst
, the string should have
the following shape year month day time
.
These two arguments are not strict, meaning if the shape of the string is not the one expected by pandas.to_datetime
, the function
will not fail and try to figure out which part of the string is the day, month or year.
In the following example the dayfirst
argument is set to True
but we can clearly see that the month
part of
the date would be incorrect. In this case pandas.to_datetime
will ignore the dayfirst
argument, and parse the date as the
22nd of January.
import pandas as pd
pd.to_datetime(["01-22-2000 10:00"], dayfirst=True)
No issue will be raised in such a case, which could lead to bugs later in the program. Either the user made a mistake by setting
dayfirst
to True
or the month part of the date is incorrect.