The format you are passing is invalid. The dash between the % and the I is not supposed to be there.

df['TIME'] = pd.to_datetime(df['TIME'], format="%m/%d/%Y %I:%M:%S %p")

This will convert your TIME column to a datetime.


Alternatively, you can adjust your read_csv call to do this:

pd.read_csv('testresult.csv', parse_dates=['TIME'], 
    date_parser=lambda x: pd.to_datetime(x, format='%m/%d/%Y %I:%M:%S %p'))

Again, this uses the appropriate format with out the extra -, but it also passes in the format to the date_parser parameter instead of having pandas attempt to guess it with the infer_datetime_format parameter.

Answer from Andy on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.to_datetime.html
pandas.to_datetime — pandas 3.0.1 documentation - PyData |
This function converts a scalar, array-like, Series or DataFrame/dict-like to a pandas datetime object. ... The object to convert to a datetime. If a DataFrame is provided, the method expects minimally the following columns: "year", "month", "day". The column “year” must be specified in ...
People also ask

Why does pd.to_datetime return datetime64[ns] and not a Python datetime?
Pandas uses NumPy's datetime64[ns] type for performance. Operations on datetime64[ns] arrays are vectorized and run at C speed, while Python datetime objects require slow Python-level loops. If you need Python datetime objects, use .dt.to_pydatetime() on the Series.
🌐
docs.kanaries.net
docs.kanaries.net › topics › Pandas › pandas-to-datetime
Pandas to_datetime: Convert Strings, Timestamps, and Mixed Formats ...
How do I convert a datetime column back to a string?
Use the .dt.strftime() accessor. For example, df['date'].dt.strftime('%Y-%m-%d') converts each datetime to a string in YYYY-MM-DD format. You can use any standard strftime format codes.
🌐
docs.kanaries.net
docs.kanaries.net › topics › Pandas › pandas-to-datetime
Pandas to_datetime: Convert Strings, Timestamps, and Mixed Formats ...
What is the difference between pd.to_datetime() and pd.Timestamp()?
pd.to_datetime() is designed for converting sequences (lists, Series, DataFrame columns) of date-like values into datetime objects. It handles multiple formats, error coercion, and vectorized parsing. pd.Timestamp() creates a single timestamp from a scalar value. Use pd.to_datetime() when working with columns and pd.Timestamp() when you need a single reference point.
🌐
docs.kanaries.net
docs.kanaries.net › topics › Pandas › pandas-to-datetime
Pandas to_datetime: Convert Strings, Timestamps, and Mixed Formats ...
🌐
Statology
statology.org › home › how to specify format in pandas.to_datetime
How to Specify Format in pandas.to_datetime
February 27, 2023 - You can use the pandas.to_datetime() function to convert a string column to a datetime column in a pandas DataFrame. When using this function, you can use the format argument to specify the format that your date is in so that you avoid errors ...
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › python-pandas-to_datetime
Pandas.to_datetime()-Python - GeeksforGeeks
June 24, 2025 - Explanation: pandas.to_datetime() expects dates in ISO format (YYYY-MM-DD), so we use 'format='%d/%m/%Y' to correctly parse day-first strings.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.to_datetime.html
pandas.to_datetime — pandas 3.0.1 documentation
This function converts a scalar, array-like, Series or DataFrame/dict-like to a pandas datetime object. ... The object to convert to a datetime. If a DataFrame is provided, the method expects minimally the following columns: "year", "month", "day". The column “year” must be specified in ...
Top answer
1 of 9
435

You can use dt.strftime if you need to convert datetime to other formats (but note that then dtype of column will be object (string)):

import pandas as pd

df = pd.DataFrame({'DOB': {0: '26/1/2016', 1: '26/1/2016'}})
print (df)
         DOB
0  26/1/2016 
1  26/1/2016

df['DOB'] = pd.to_datetime(df.DOB)
print (df)
         DOB
0 2016-01-26
1 2016-01-26

df['DOB1'] = df['DOB'].dt.strftime('%m/%d/%Y')
print (df)
         DOB        DOB1
0 2016-01-26  01/26/2016
1 2016-01-26  01/26/2016
2 of 9
54

There is a difference between

  • the content of a dataframe cell (a binary value) and
  • its presentation (displaying it) for us, humans.

So the question is: How to reach the appropriate presentation of my data without changing the data / data types themselves?

Here is the answer:

  • If you use the Jupyter notebook for displaying your dataframe, or
  • if you want to reach a presentation in the form of an HTML file (even with many prepared superfluous id and class attributes for further CSS styling — you may or you may not use them),

use styling. Styling doesn't change data / data types of columns of your dataframe.

Now I show you how to reach it in the Jupyter notebook — for a presentation in the form of HTML file see the note near the end of this answer.

I will suppose that your column DOB already has the datetime64 type (you have shown that you know how to reach it). I prepared a simple dataframe (with only one column) to show you some basic styling:

  • Not styled:

    df
    
          DOB
0  2019-07-03
1  2019-08-03
2  2019-09-03
3  2019-10-03
  • Styling it as mm/dd/yyyy:

    df.style.format({"DOB": lambda t: t.strftime("%m/%d/%Y")})
    
          DOB
0  07/03/2019
1  08/03/2019
2  09/03/2019
3  10/03/2019
  • Styling it as dd-mm-yyyy:

    df.style.format({"DOB": lambda t: t.strftime("%d-%m-%Y")}) 
    
          DOB
0  03-07-2019
1  03-08-2019
2  03-09-2019
3  03-10-2019

Be careful!
The returning object is NOT a dataframe — it is an object of the class Styler, so don't assign it back to df:

Don't do this:

df = df.style.format({"DOB": lambda t: t.strftime("%m/%d/%Y")})    # Don't do this!

(Every dataframe has its Styler object accessible by its .style property, and we changed this df.style object, not the dataframe itself.)


Questions and Answers:

  • Q: Why your Styler object (or an expression returning it) used as the last command in a Jupyter notebook cell displays your (styled) table, and not the Styler object itself?

  • A: Because every Styler object has a callback method ._repr_html_() which returns an HTML code for rendering your dataframe (as a nice HTML table).

    Jupyter Notebook IDE calls this method automatically to render objects which have it.


Note:

You don't need the Jupyter notebook for styling (i.e., for nice outputting a dataframe without changing its data / data types).

A Styler object has a method render(), too, if you want to obtain a string with the HTML code (e.g., for publishing your formatted dataframe on the Web, or simply present your table in the HTML format):

df_styler = df.style.format({"DOB": lambda t: t.strftime("%m/%d/%Y")})
HTML_string = df_styler.render()
Find elsewhere
🌐
Educative
educative.io › answers › what-is-the-pandastodatetime-method
What is the pandas.to_datetime() method?
The pandas.to_datetime() method in Python converts data into datetime objects.
🌐
Kanaries
docs.kanaries.net › topics › Pandas › pandas-to-datetime
Pandas to_datetime: Convert Strings, Timestamps, and Mixed Formats – Kanaries
February 26, 2025 - import pandas as pd try: pd.to_datetime('15/01/2025', format='%Y-%m-%d') except ValueError as e: print(e) # time data "15/01/2025" doesn't match format "%Y-%m-%d"
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › to_datetime
Python Pandas to_datetime() - Convert to DateTime | Vultr Docs
December 9, 2024 - Start with a simple date string. Use to_datetime() to convert it to a DateTime object. ... import pandas as pd date_str = '2023-01-01' date_time_obj = pd.to_datetime(date_str) print(date_time_obj) Explain Code
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › how to format pandas datetime?
How to Format Pandas Datetime? - Spark By {Examples}
February 10, 2026 - With pandas built-in function pd.to_datetime(), we can convert the date time string object into a datetime64 object. It will automatically change the default format of the string date time into the default format of datetime64.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 1.5 › reference › api › pandas.to_datetime.html
pandas.to_datetime — pandas 1.5.2 documentation
DataFrame/dict-like are converted to Series with datetime64 dtype. For each row a datetime is created from assembling the various dataframe columns. Column keys can be common abbreviations like [‘year’, ‘month’, ‘day’, ‘minute’, ‘second’, ‘ms’, ‘us’, ‘ns’]) or ...
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.20 › generated › pandas.to_datetime.html
pandas.to_datetime — pandas 0.20.3 documentation
December 19, 2023 - >>> pd.to_datetime('13000101', format='%Y%m%d', errors='ignore') datetime.datetime(1300, 1, 1, 0, 0) >>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce') NaT
🌐
Reddit
reddit.com › r/learnpython › i need some help in understanding datetime within a pandas dataframe: data type and visualization
r/learnpython on Reddit: I need some help in understanding datetime within a pandas dataframe: data type and visualization
December 21, 2022 -

Howdy!

I'm having some difficult in working with datetime within a pandas dataframe, specifically a dataframe that's being imported from csv, excel and/or sql, which have dates already written on it.

I think that the issue is due to pandas somehow having an american default format of mm/dd/yyyy while I mostly use dd/mm/yyyy or yyyy-mm-dd (with time when needed), and working with datetime class or string class.

I've had cases when the excel had a dd/mm/yyyy date format (can't be sure if string or number) but pandas (vscode with jupyter) insisted on showing as yyyy-mm-dd as datetime column even though each value was apparently a string. It was weird because a similar column that should have been formatted the same had its data shown as some other format. I remember that I had applied a formula to transform it to datetime but it wouldn't reset back to the original format even after I restarted the kernal, as if the formula I applied was a permanent change or something.

So I have some questions:

  1. Can I have a datetime variable (that works with any datetime comparison/formulas) that is expressed/formatted as any format possible (like dd/mm/yyyy or yyyy-mm-dd)? This is important if I have to apply a filter to a dataframe, to define what format should I use and if I can use string or dataframe (df[df['date']=="21/12/2022'] or something like df[df['date']==datetime(2022,12,21)])

  2. I always have to export the data to a file (csv or xlsx) as an intermediate step. Should I export as a datetime or should I convert datetime to string? I'd rather have the data be exported in a way that is visually and systematically understood as a date (so a dd/mm/yyyy format but that excel/sql/other knows that it is a date)

I'm really lost and I've spent the whole day yesterday juggling variables and date formats just to simply compare the values between two different columns. I'm almost requesting an ELI5 because I'm that lost. Like I don't quite understand the difference or when to use datetime.strftime and datetime.strptime

Should I have some standard steps when working with dates within a dataframe, like always formating from string/datetime (default by pd.read) to datetime and then always converting to a specific format when exporting the dataframe (pd.to_)? What is the norm?

Can anyone give me some pointers to understand these things?

Cheers!

🌐
Programiz
programiz.com › python-programming › pandas › methods › to_datetime
Pandas to_datetime()
August 20, 2023 - The to_datetime() method returns a datetime object. import pandas as pd # create a Series with date strings in 'YYYYMMDD' format date_series = pd.Series(['20201010', '20201111', '20201212'])
🌐
Reddit
reddit.com › r/learnpython › how to set date format in pandas.to_datetime accurately? or another way or specifying to use mm/dd/yyyy?
r/learnpython on Reddit: How to set date format in pandas.to_datetime accurately? Or another way or specifying to use mm/dd/yyyy?
July 1, 2021 -

I work for a European company which uses US format dates. There are some dates that go into the database as a future date and shouldn't. These are obviously dd/mm/yyyy dates being read as mm/dd//yyyy.

I've set up a small script to see which files are using dd/mm/yyyy dates, it's not working as I expected. As a test:

print(pd.to_datetime('29/1/21'))

print(pd.to_datetime('1/29/21'))

produces:

2021-01-29 00:00:00

2021-01-29 00:00:00

________

print(pd.to_datetime('29/1/2021', dayfirst=True))

print(pd.to_datetime('1/29/2021', dayfirst=True))

also produces the same result, which is probably down to this warning in https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

Warning: dayfirst=True is not strict, but will prefer to parse with day first (this is a known bug, based on dateutil behavior).

What is the best way of setting region to US (note I am in Europe), strictly using mm/dd/yyyy and forcing an error if there is a European date like 13/1/2021

🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.22 › generated › pandas.to_datetime.html
pandas.to_datetime — pandas 0.22.0 documentation
>>> pd.to_datetime('13000101', format='%Y%m%d', errors='ignore') datetime.datetime(1300, 1, 1, 0, 0) >>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce') NaT
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to change the datetime format in pandas
How to Change the Datetime Format in Pandas | Towards Data Science
January 17, 2025 - In the example dataframe, we provide the dates as strings and pandas will automatically parse the column as a object dtype. In order to do so, we simply need to call the to_datetime() method and assign the result back to the original column. Additionally, we specify the format of the dates ...
🌐
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.to_datetime.html
pyspark.pandas.to_datetime — PySpark 4.1.1 documentation
April 12, 2023 - >>> ps.to_datetime('13000101', format='%Y%m%d', errors='ignore') datetime.datetime(1300, 1, 1, 0, 0) >>> ps.to_datetime('13000101', format='%Y%m%d', errors='coerce') NaT
🌐
Programiz
programiz.com › python-programming › pandas › datetime
Pandas DateTime (With Examples)
May 21, 2024 - By default, Pandas' to_datetime() function expects the date string to be in the YYYY-MM-DD format.