0

I expect I have a typo but I can't see it.

df = pd.to_datetime(gdf['startdate'], format="%m/%d/%Y %H:%M:%S")

This gives the eroor:

ValueError: time data '16/06/2020 09:01:31' does not match format '%m/%d/%Y %H:%M:%S' (match)

gdf['startdate'] looks like this:

0      08/06/2020 13:31:14
1      08/06/2020 14:42:45
2      08/06/2020 14:34:13
3      09/06/2020 12:20:41
4      09/06/2020 15:31:36
              ...         

144    29/07/2020 11:36:34
145    30/07/2020 12:31:17
146    31/07/2020 14:27:36
147    31/07/2020 08:21:14
148    06/08/2020 12:08:38
Name: startdate, Length: 149, dtype: object

1 Answer 1

2

I switched around %d and %m in your formatting:

gdf['startdate'] = pd.to_datetime(
    gdf['startdate'], 
    format="%d/%m/%Y %H:%M:%S",
)

If a value in your column is not like a datetime at all and causing errors, you can use errors='coerce' to set those to NaT:

gdf['startdate'] = pd.to_datetime(
    gdf['startdate'], 
    format="%d/%m/%Y %H:%M:%S",
    errors='coerce',
)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but now this error ValueError: time data ' ' does not match format '%d/%m/%Y %H:%M:%S' (match)
The error is quite informative: I guess one of the values in your column is not in that format, i guess it has format ' ' as the error says, you can use parameter errors='coerce' if you want to convert this to NaT.
thanks, that forces it through, although when I try df.date() I get: AttributeError: 'Series' object has no attribute 'date' Ultimately I'm trying to just get the date information cutting out the h:m:s
df['startdate'] is what you need or? in your example there's no column 'date'
Ah, got it! gdf['startdate'].dt.strftime("%d/%m/%Y")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.