Reputation: 1568
I cannot find the correct format for this datetime. I have tried several formats, %Y/%m/%d%I:%M:%S%p
is the closest format I can find for the example below.
df['datetime'] = '2019-11-13 16:28:05.779'
df['datetime'] = pd.to_datetime(df['datetime'], format="%Y/%m/%d%I:%M:%S%p")
Result:
ValueError: time data '2019-11-13 16:28:05.779' does not match format '%Y/%m/%d%I:%M:%S%p' (match)
Upvotes: 1
Views: 55
Reputation: 2702
Here is the pandas.to_datetime()
call with the correct format string: pd.to_datetime(df['datetime'], format="%Y/%m/%d %H:%M:%S")
You were missing a space, %I
is for 12-hour time (the example time you gave is 16:28
, and %p
is, to quote the docs, the Locale’s equivalent of either AM or PM.
Upvotes: -1
Reputation: 14094
Before guessing yourself have pandas make the first guess
df['datetime'] = pd.to_datetime(df['datetime'], infer_datetime_format=True)
0 2019-11-13 16:28:05.779
Name: datetime, dtype: datetime64[ns]
Upvotes: 4
Reputation: 18367
You can solve this probably by using the parameter infer_datetime_format=True
. Here's an example:
df = {}
df['datetime'] = '2019-11-13 16:28:05.779'
df['datetime'] = pd.to_datetime(df['datetime'], infer_datetime_format=True)
print(df['datetime'])
print(type(df['datetime'])
Output:
2019-11-13 16:28:05.779000
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
Upvotes: 4