Use to_datetime. There is no need to specify the format in this case since the parser is able to figure it out.

In [51]:
pd.to_datetime(df['I_DATE'])

Out[51]:
0   2012-03-28 14:15:00
1   2012-03-28 14:17:28
2   2012-03-28 14:50:50
Name: I_DATE, dtype: datetime64[ns]

To access the date/day/time component use the dt accessor:

In [54]:
df['I_DATE'].dt.date

Out[54]:
0    2012-03-28
1    2012-03-28
2    2012-03-28
dtype: object

In [56]:    
df['I_DATE'].dt.time

Out[56]:
0    14:15:00
1    14:17:28
2    14:50:50
dtype: object

You can use strings to filter as an example:

In [59]:
df = pd.DataFrame({'date':pd.date_range(start = dt.datetime(2015,1,1), end = dt.datetime.now())})
df[(df['date'] > '2015-02-04') & (df['date'] < '2015-02-10')]

Out[59]:
         date
35 2015-02-05
36 2015-02-06
37 2015-02-07
38 2015-02-08
39 2015-02-09
Answer from EdChum on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ pandas โ€บ change-string-to-date-in-pandas-dataframe
Change String To Date In Pandas Dataframe - GeeksforGeeks
July 23, 2025 - Explanation: This code creates a DataFrame from a dictionary with date strings and numerical values, then converts the 'Date' column to datetime64[ns] using pandas.to_datetime() for proper date handling. This method is useful when dealing with custom date formats. The strptime() function from Pythonโ€™s datetime module allows precise control over how dates are parsed by specifying a format string.
Discussions

python - How to convert string to datetime format in pandas? - Stack Overflow
I have a column I_DATE of type string (object) in a dataframe called train as show below. I_DATE 28-03-2012 2:15:00 PM 28-03-2012 2:17:28 PM 28-03-2012 2:50:50 PM How to convert I_DATE from strin... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Converting string to datetime - when the year is only three digits.
It's not the format, it's pandas. It's being super helpful and converting the csv strings to ints for you. You need to tell it not to do that please with the dtype argument. https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html dtypeType name or dict of column -> type, optional Data type for data or columns. E.g. {โ€˜aโ€™: np.float64, โ€˜bโ€™: np.int32, โ€˜cโ€™: โ€˜Int64โ€™} Use str or object together with suitable na_values settings to preserve and not interpret dtype More on reddit.com
๐ŸŒ r/learnpython
5
1
May 2, 2023
python - Convert DataFrame column type from string to datetime - Stack Overflow
If your date column is a string of the format '2017-01-01' you can use pandas astype to convert it to datetime. More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - datetime to string with series in pandas - Stack Overflow
See the docs on customizing date string formats here: strftime() and strptime() Behavior. For old pandas versions <0.17.0, one can instead can call .apply with the Python standard library's datetime.strftime: More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 21, 2020
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas change string to date in dataframe
Pandas Change String to Date in DataFrame - Spark By {Examples}
May 13, 2020 - # Quick examples of change string to date # Example 1: Use pandas.to_datetime() # To convert string to datetime format df["InsertedDate"] = pd.to_datetime(df["InsertedDate"]) # Example 2: Convert and store in another column df["NewColumn"] = ...
๐ŸŒ
Data to Fish
datatofish.com โ€บ string-to-datetime-pandas
How to Convert Strings to Datetime in a pandas DataFrame
July 26, 2022 - df['date'] = pd.to_datetime(df['date'], format='%m/%d/%Y') print(df.dtypes) ... Suppose, you have the following DataFrame. import pandas as pd data = {'timestamp': ['2021-01-15 13:10:10', '2021-02-15 14:33:11', '2021-03-15 19:50:40']} df = pd.DataFrame(data) print(df) print(df.dtypes) timestamp ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ pandas โ€บ convert-the-column-type-from-string-to-datetime-format-in-pandas-dataframe
Convert Column Type from String to Datetime Format in Pandas Dataframe - GeeksforGeeks
Explanation: astype('datetime64[ns]') explicitly converts the โ€œDateโ€ column to datetime type. Change Data Type for one or more columns in Pandas Dataframe
Published ย  November 1, 2025
Find elsewhere
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-convert-strings-in-a-pandas-data-frame-to-a-date-data-type
How to Convert Strings in a Pandas Dataframe to a Date Data Type | Saturn Cloud Blog
January 25, 2024 - Specifically, the conversion of strings to date data types is a frequent requirement, essential for tasks such as time series analysis, data visualization, and more. This post aims to guide you through the process of converting strings to date data types within a Pandas data frame.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third-party โ€บ pandas โ€บ to_datetime
Python Pandas to_datetime() - Convert to DateTime | Vultr Docs
December 9, 2024 - The to_datetime() function in Python's Pandas library is a versatile tool for converting various date and time formats into pandas DateTime objects. This capability is essential for data analysis, especially when dealing with time-series data ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ converting string to datetime - when the year is only three digits.
r/learnpython on Reddit: Converting string to datetime - when the year is only three digits.
May 2, 2023 -

Hi, so I'm working on a genealogy project. I have ancestors back in the 400s onward.

I've imported them as a csv using pandas and need to convert just the years to datetime.

import pandas as pd

df = pd.read_csv('final_ancestry.csv')

df.Year1=df.Year1.astype(str)
df.Year2=df.Year2.astype(str)

pd.to_datetime(df.Year1, format='%Y')

I keep getting "ValueError: time data '406' does not match format '%Y' (match)". I know the format is YYYY, but in the csv, I actually put it as 0406, etc. My next thought is to save it as a txt file and import the file that way, but is there something I'm missing?

I also don't know if I really need to convert to string, but it wasn't working as the INT64 that it imported as, so I thought I'd try string.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ pandas โ€บ pandas-convert-date-datetime-to-string-format
Pandas Convert Date (Datetime) To String Format - GeeksforGeeks
June 12, 2025 - The astype(str) method converts the datetime column directly into strings. This approach is simple but does not allow formatting customization. ... import pandas as pd a = { 'dt': pd.to_datetime(['2024-01-01', '2024-01-02', '2024-01-03']), 'x': ...
๐ŸŒ
DataScientYst
datascientyst.com โ€บ convert-string-to-datetime-pandas
How to Convert String to DateTime in Pandas
February 18, 2022 - To convert string column to DateTime in Pandas and Python we can use: (1) method: pd.to_datetime() pd.to_datetime(df['date']) (2) method .astype('datetime64[ns]') df['timestamp'].astype('datetime64[ns]') Let's check the most popular cases of ...
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.Series.dt.strftime.html
pandas.Series.dt.strftime โ€” pandas 3.0.1 documentation
January 24, 2023 - Formats supported by the C strftime API but not by the python string format doc (such as โ€œ%Rโ€, โ€œ%rโ€) are not officially supported and should be preferably replaced with their supported equivalents (such as โ€œ%H:%Mโ€, โ€œ%I:%M:%S %pโ€). Note that PeriodIndex support additional directives, detailed in Period.strftime. ... Date format string (e.g. โ€œ%%Y-%%m-%%dโ€). ... NumPy ndarray of formatted strings. ... Convert the given argument to datetime.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ converting-strings-datetime-objects
Convert String to DateTime in Python: Complete Guide with Examples | DataCamp
June 26, 2025 - While Pythonโ€™s built-in datetime module is powerful, many data scientists prefer using the pandas library for datetime conversions due to its simplicity and ability to handle entire columns of dates efficiently using DataFrames. import pandas as pd # Converting a column of strings to datetime objects date_series = pd.to_datetime(['2023-02-28', '2023-03-01', '2023-03-02']) print(date_series)
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to convert string to datetime in pandas
How to Convert String to Datetime in Pandas
June 8, 2018 - This tutorial explains how to convert one or more string columns to datetime in a pandas DataFrame, including examples.
๐ŸŒ
Seaborn Line Plots
marsja.se โ€บ home โ€บ programming โ€บ python โ€บ pandas convert column to datetime โ€“ object/string, integer, csv & excel
Pandas Convert Column to datetime - object/string, integer, CSV & Excel
November 20, 2023 - First, we will look at converting objects (i.e., strings) to datetime using the to_datetime() method. When working with to_datetime(), one neat thing is that we can work with the format parameter.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ pandas โ€บ python-pandas-data-frame-exercise-41.php
Pandas: Convert DataFrame column type from string to datetime - w3resource
April 6, 2025 - import pandas as pd import numpy as np s = pd.Series(['3/11/2000', '3/12/2000', '3/13/2000']) print("String Date:") print(s) r = pd.to_datetime(pd.Series(s)) df = pd.DataFrame(r) print("Original DataFrame (string to datetime):") print(df) ...