This has been answered in the comments where it was noted that the following works:

df.astype({'date': 'datetime64[ns]'})

In addition, you can set the dtype when reading in the data:

pd.read_csv('path/to/file.csv', parse_dates=['date'])
Top answer
1 of 2
36

This has been answered in the comments where it was noted that the following works:

df.astype({'date': 'datetime64[ns]'})

In addition, you can set the dtype when reading in the data:

pd.read_csv('path/to/file.csv', parse_dates=['date'])
2 of 2
8

datetime

Since you can't pass datetime format to astype(), it's a little primitive and it's better to use pd.to_datetime() instead.

df['date'] = pd.to_datetime(df['date'])

For example, if the dates in the data are of the format %d/%m/%Y such as 01/04/2020, astype() would incorrectly parse it as 2020-01-04 whereas with pd.to_datetime(), you can pass the correct format.

If you need to convert multiple columns into datetime64 (which is often the reason astype() is used), then you can apply pd.to_datetime().

df = pd.DataFrame({'date1': ['01/04/2020'], 'date2': ['02/04/2020']})
df = df.apply(pd.to_datetime, format='%d/%m/%Y')

Even with read_csv, you have some control over the format, e.g.

df = pd.read_csv('file.csv', parse_dates=['date'], dayfirst=True)

date

If you want to cast into date, then you can first cast to datetime64[ns] and then use dt.date to get a column of datetime.date objects:

df['date'] = pd.to_datetime(df['date']).dt.date

The column dtype will become object though (on which you can still perform vectorized operations such as adding days, comparing dates etc.), so if you plan to work on it a lot in pandas, it's more performative to use datetime64 instead. For example, adding a day is extremely fast on datetime64 columns, not so much on date columns:

s_dt = pd.Series(pd.date_range('1700', None, 10000, 'D'))
s_d = s_dt.dt.date

%timeit x = s_dt + pd.Timedelta(days=1)
# 344 µs ± 17.3 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

%timeit y = s_d + pd.Timedelta(days=1)
# 56.1 ms ± 11.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

With that being said, if you dump it into a database (such as sqlite), an object dtype column of datetime.date objects stored as a DATE type (whereas datetime64[ns] will be stored as TIMESTAMP).


Pandas datetime dtype is from numpy datetime64, so if you have pandas<2.0, you can use the following as well (since pandas 2.0, unitless datetime64 is not supported anymore). There's no date dtype (although you can perform vectorized operations on a column that holds datetime.date values).

df = df.astype({'date': np.datetime64})

# or (on a little endian system)
df = df.astype({'date': '<M8'})
# (on a big endian system)
df = df.astype({'date': '>M8'})
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.astype.html
pandas.DataFrame.astype — pandas 3.0.3 documentation
>>> from pandas.api.types import CategoricalDtype >>> cat_dtype = CategoricalDtype(categories=[2, 1], ordered=True) >>> ser.astype(cat_dtype) 0 1 1 2 dtype: category Categories (2, int64): [2 < 1] ... >>> ser_date = pd.Series(pd.date_range("20200101", periods=3)) >>> ser_date 0 2020-01-01 1 2020-01-02 2 2020-01-03 dtype: datetime64[us]
Discussions

how to convert pandas columns of dates formatted like "2020-01-02 23:59:56.078191" to datetime object, then to ms epoch?
Try: df[‘UT’].astype(‘int64’) More on reddit.com
🌐 r/learnpython
5
3
April 15, 2022
BUG: astype not working correctly for similar datetime format
Pandas version checks I have checked that this issue has not already been reported. I have confirmed this bug exists on the latest version of pandas. I have confirmed this bug exists on the main br... More on github.com
🌐 github.com
2
May 7, 2023
python - Convert DataFrame column type from string to datetime - Stack Overflow
How can I convert a DataFrame column of strings (in dd/mm/yyyy format) to datetime dtype? 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
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.to_datetime.html
pandas.to_datetime — pandas 3.0.3 documentation
Convert argument to datetime. This function converts a scalar, array-like, Series or DataFrame/dict-like to a pandas datetime object.
🌐
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
🌐
Pandas
pandas.pydata.org › docs › user_guide › timeseries.html
Time series / date functionality — pandas 3.0.3 documentation
DatetimeIndex can be converted to an array of Python native datetime.datetime objects using the to_pydatetime method. pandas has a simple, powerful, and efficient functionality for performing resampling operations during frequency conversion (e.g., converting secondly data into 5-minutely data).
🌐
Reddit
reddit.com › r/learnpython › how to convert pandas columns of dates formatted like "2020-01-02 23:59:56.078191" to datetime object, then to ms epoch?
r/learnpython on Reddit: how to convert pandas columns of dates formatted like "2020-01-02 23:59:56.078191" to datetime object, then to ms epoch?
April 15, 2022 -

I have a pandas dataframe with a column of dates formatted like "2020-01-02 23:59:56.078191"

How would I convert that column to a column of to datetime objects, then to ms epoch (float)?

Attempt:

df['UT'] = pd.to_datetime(df['UT'], format = '%Y-%m-%d %H:%M:%S.%f')

which works to get the datetime, then

df['UT'] = df['UT'].timestamp('ms')

which gives the error:

'AttributeError: 'Series' object has no attribute 'timestamp'

🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas convert column to datetime
Pandas Convert Column To DateTime - Spark By {Examples}
June 26, 2025 - By using pandas to_datetime() & astype() functions you can convert column to DateTime format (from String and Object to DateTime). If your DataFrame
Find elsewhere
🌐
GitHub
github.com › pandas-dev › pandas › issues › 53127
BUG: astype not working correctly for similar datetime format · Issue #53127 · pandas-dev/pandas
May 7, 2023 - import pandas as pd a = pd.Series(["2007-07-07 01:01:01.10", "2007-07-07 01:01:01"]) a.astype("datetime64[ns]") This code runs fine in pandas 1.5.3 If same column has multiple format for date time, when we use as type it fails. ValueError: time data "2007-07-07 01:01:01" doesn't match format "%Y-%m-%d %H:%M:%S.%f", at position 1.
Author   pandas-dev
🌐
Medium
medium.com › data-science › 3-practical-differences-between-astype-and-to-datetime-in-pandas-fe2c0bfc7678
3 Practical Differences Between astype() and to_datetime() in Pandas
July 28, 2023 - Convert Pandas dataframe column type from string to datetime format using pd.to_datetime() function. change datatype of multiple columns in pandas using astype()
🌐
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. Python · import pandas as pd a = { 'dt': pd.to_datetime(['2024-01-01', '2024-01-02', '2024-01-03']), ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas change string to date in dataframe
Pandas Change String to Date in DataFrame - Spark By {Examples}
June 26, 2025 - To convert a pandas DataFrame column from string to date type (datetime64) format, you can use the pandas.to_datetime() function or the DataFrame.astype()
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas convert integer to datetime type
Pandas Convert Integer to Datetime Type - Spark By {Examples}
June 25, 2025 - In Pandas, you can convert an integer column representing dates or times into a DateTime type using the pd.to_datetime() function. To convert integer to
🌐
Saturn Cloud
saturncloud.io › blog › converting-object-column-in-pandas-dataframe-to-datetime-a-data-scientists-guide
Converting Object Column in Pandas Dataframe to Datetime: A Comprehensive Guide | Saturn Cloud Blog
June 19, 2023 - In this article, we discussed why datetime format is necessary in pandas dataframes and how to convert object columns to datetime format using the pd.to_datetime() method. We also discussed some common challenges you may face during this process, such as non-standard date formats and missing or invalid dates, and their solutions.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.dt.year.html
pandas.Series.dt.year — pandas 3.0.3 documentation
) >>> datetime_series 0 2000-12-31 1 2001-12-31 2 2002-12-31 dtype: datetime64[us] >>> datetime_series.dt.year 0 2000 1 2001 2 2002 dtype: int32
🌐
Saturn Cloud
saturncloud.io › blog › how-to-convert-datetime-to-timestamp-in-python-pandas-using-the-dt-accessor
How to Convert Datetime to Timestamp in Python Pandas using the dt Accessor | Saturn Cloud Blog
November 1, 2023 - Convert the datetime column to a pandas datetime object using the to_datetime() function. Use the astype() function to convert the datetime column to an integer.
🌐
Python Forum
python-forum.io › thread-42483.html
Working with dates in pandas
July 21, 2024 - Hi everyone, I am having a confusing issue in pandas which I'll highlight in the code below. You'll need to update the path to your own one. You can access the data from the http link. import numpy as
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Index.astype.html
pandas.Index.astype — pandas 3.0.3 documentation
>>> idx = pd.Index([1, 2, 3]) >>> idx Index([1, 2, 3], dtype='int64') >>> idx.astype("float") Index([1.0, 2.0, 3.0], dtype='float64')
🌐
Delft Stack
delftstack.com › home › howto › python pandas › how to convert dataframe column to datetime in pandas
How to Convert Pandas Column to Datetime | Delft Stack
February 2, 2024 - Use the apply() Method to Convert Pandas Multiple Columns to Datetime · Use the Series.astype() Method to Convert Pandas DataFrame Column to Datetime · We will introduce methods to convert a Pandas column to datetime. We use the same DataFrame below in the following examples.
🌐
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.