With pandas -
pd.to_datetime("2/3/13")
Timestamp('2013-02-03 00:00:00')
With pandas
, you can (most of the time) infer the format without having to manually specify it. However, it would seem pointless to import such a heavy module to use it only for a simple datetime conversion.
For that reason, here's an equally simple python way using strptime
-
from datetime import datetime as dt
dt.strptime("2/3/13", '%d/%m/%y')
datetime.datetime(2013, 3, 2, 0, 0)
Or, even simpler (you don't have to pass a format string), using dateutil
-
from dateutil import parser
parser.parse('2/3/13/').date()
datetime.date(2013, 2, 3)
pandas actually uses the dateutil
library under the hood (– unutbu). If your date strings are consistent in structure, I'd recommend using one of these.