0
0
PandasHow-ToBeginner · 2 min read

How to Convert to Datetime in Pandas | Data Science Guide

Use pd.to_datetime() to convert a column or values to datetime in pandas, for example: df['date'] = pd.to_datetime(df['date']).
📋

Examples

Input2023-01-01
OutputTimestamp('2023-01-01 00:00:00')
Input['2023-01-01', '2023/02/01', 'March 3, 2023']
Output[Timestamp('2023-01-01 00:00:00'), Timestamp('2023-02-01 00:00:00'), Timestamp('2023-03-03 00:00:00')]
Input['2023-13-01', 'not a date'] (invalid dates)
Output[NaT, NaT]
🧠

How to Think About It

To convert data to datetime in pandas, think about using the built-in function pd.to_datetime() which can handle strings, lists, or columns. It tries to parse the input into datetime objects automatically, making it easy to work with dates.
📐

Algorithm

1
Get the input data that needs conversion (string, list, or DataFrame column).
2
Call the <code>pd.to_datetime()</code> function with the input data.
3
Handle errors or invalid dates by setting parameters if needed (like <code>errors='coerce'</code>).
4
Assign the converted datetime back to the DataFrame or variable.
5
Return or use the datetime-converted data for further analysis.
💻

Code

pandas
import pandas as pd

data = {'date': ['2023-01-01', '2023/02/01', 'March 3, 2023', 'invalid']}
df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['date'], errors='coerce')
print(df)
Output
date 0 2023-01-01 1 2023-02-01 2 2023-03-03 3 NaT
🔍

Dry Run

Let's trace converting the 'date' column with mixed date formats and an invalid entry.

1

Original Data

['2023-01-01', '2023/02/01', 'March 3, 2023', 'invalid']

2

Apply pd.to_datetime with errors='coerce'

Convert each string to datetime; invalid strings become NaT

3

Resulting DataFrame

['2023-01-01', '2023-02-01', '2023-03-03', NaT]

Original StringConverted Datetime
2023-01-012023-01-01 00:00:00
2023/02/012023-02-01 00:00:00
March 3, 20232023-03-03 00:00:00
invalidNaT
💡

Why This Works

Step 1: Automatic Parsing

pd.to_datetime() tries to understand many date formats automatically, so you don't need to specify the format for common cases.

Step 2: Handling Errors

Using errors='coerce' converts invalid or unparseable dates to NaT, which means 'Not a Time' or missing date.

Step 3: Assigning Back

You assign the converted datetime values back to your DataFrame column to replace the original strings with datetime objects.

🔄

Alternative Approaches

Specify format parameter
pandas
import pandas as pd
df = pd.DataFrame({'date': ['2023-01-01', '2023-02-01']})
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
print(df)
Faster and more reliable if you know the exact date format, but fails if format doesn't match.
Use datetime.strptime in apply
pandas
import pandas as pd
from datetime import datetime
df = pd.DataFrame({'date': ['2023-01-01', '2023-02-01']})
df['date'] = df['date'].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))
print(df)
More manual and slower; useful if you want custom parsing logic.

Complexity: O(n) time, O(n) space

Time Complexity

The function processes each element once, so time grows linearly with the number of dates.

Space Complexity

It creates a new datetime array of the same size, so space usage is linear.

Which Approach is Fastest?

Specifying the exact format with format= is fastest because pandas skips guessing the format.

ApproachTimeSpaceBest For
pd.to_datetime() autoO(n)O(n)General use, unknown formats
pd.to_datetime() with formatO(n)O(n)Known fixed date formats, faster
apply with datetime.strptimeO(n)O(n)Custom parsing, slower, more control
💡
Use errors='coerce' in pd.to_datetime() to safely convert invalid dates to NaT without errors.
⚠️
Not using errors='coerce' causes the code to break when invalid date strings are present.