pandas
python
date manipulation
datetime
data analysis

Keep only date part when using pandas.to_datetime

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

When you convert values with pandas.to_datetime, you often end up with full timestamps even if you only care about the date. The right next step depends on whether you want a true Python date object or whether you want to keep pandas' efficient datetime64 type while zeroing out the time component.

Convert first, then choose the date-only representation

Start by parsing the column normally:

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "timestamp": [
5        "2026-03-07 14:15:00",
6        "2026-03-08 09:30:45",
7        "2026-03-09 21:05:10",
8    ]
9})
10
11df["timestamp"] = pd.to_datetime(df["timestamp"])
12print(df)

At this point, the column is a pandas datetime column. Now you need to decide what "keep only the date part" should mean in your workflow.

Use .dt.date when you want Python date objects

If you literally want date objects with no time component:

python
df["date_only"] = df["timestamp"].dt.date
print(df)
print(df["date_only"].dtype)

This is easy to read and gives values that behave like ordinary Python dates.

The tradeoff is that .dt.date converts the column to object dtype, which is less efficient than staying in pandas' native datetime representation.

So .dt.date is a good choice when:

  • you need Python date objects specifically
  • you are exporting or serializing dates
  • performance is not a major concern for that column

Use .dt.normalize() or .dt.floor("D") to keep datetime dtype

If you want to drop the time portion but keep a datetime-like pandas type, normalize the timestamp to midnight:

python
df["date_midnight"] = df["timestamp"].dt.normalize()
print(df)
print(df["date_midnight"].dtype)

Or:

python
df["date_floor"] = df["timestamp"].dt.floor("D")
print(df)

These methods keep the column as datetime64, which is often better for:

  • grouping
  • joining
  • filtering by date ranges
  • vectorized datetime operations

This is usually the best answer when you still want pandas-style date handling rather than plain Python objects.

Example difference in practice

Here is a minimal comparison:

python
1import pandas as pd
2
3s = pd.to_datetime(pd.Series(["2026-03-07 14:15:00"]))
4
5print(s.dt.date)
6print(s.dt.normalize())

The first gives a date object. The second gives a timestamp at midnight. They may print similarly, but they are not the same type and do not behave the same way in later operations.

That distinction matters a lot in real data pipelines.

If you only need the date for display

Sometimes you do not need a date type at all. You only want a formatted string:

python
df["date_string"] = df["timestamp"].dt.strftime("%Y-%m-%d")
print(df["date_string"])

This is useful for output, but it should usually be the last step. Strings are less useful than datetime values for computation.

If you convert too early to strings, filtering and sorting by date become more awkward than they need to be.

Time zones still matter

If your timestamps are timezone-aware, dropping the time portion does not remove the importance of timezone handling. Convert to the correct timezone before extracting the date:

python
df["timestamp"] = pd.to_datetime(df["timestamp"], utc=True)
df["local_date"] = df["timestamp"].dt.tz_convert("America/Toronto").dt.date

Otherwise, a timestamp near midnight UTC might land on the wrong calendar date for the user's local timezone.

Common Pitfalls

The biggest mistake is using .dt.date without realizing it converts the column to object dtype. That can make later pandas operations slower or less convenient.

Another common issue is formatting to strings too early when the data still needs to be filtered, grouped, or merged by date.

People also forget timezone conversion. The "date part" depends on timezone whenever the timestamps are timezone-aware.

Finally, if you want to preserve a datetime column but remove time-of-day noise, .dt.normalize() or .dt.floor("D") is usually a better fit than .dt.date.

Summary

  • Use pd.to_datetime(...) first, then decide how you want to represent the date-only result.
  • Use .dt.date for Python date objects.
  • Use .dt.normalize() or .dt.floor("D") to keep pandas datetime dtype.
  • Use .dt.strftime(...) only when you need display strings.
  • Handle timezone conversion before extracting the final date when timezone-aware data is involved.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.