pandas
data manipulation
datetime
python
data analysis

Combine Date and Time columns using pandas

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Combining separate date and time columns in pandas is usually a one-line operation, but the correct approach depends on the input types. If both columns are strings, concatenation plus pd.to_datetime is usually enough. If one column already contains datetime.date, datetime.time, or timezone-aware values, you need to be more deliberate to avoid dtype surprises.

The Common String-Column Case

If your dataframe stores both parts as strings, the simplest pattern is:

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "date": ["2026-03-01", "2026-03-02"],
5    "time": ["09:15:00", "18:45:00"],
6})
7
8df["timestamp"] = pd.to_datetime(df["date"] + " " + df["time"])
9print(df)
10print(df.dtypes)

This works because pandas can parse the combined string into a single datetime64 column.

Be Explicit About Parsing

If the input format is stable, specify it. That makes parsing faster and reduces accidental misinterpretation.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "date": ["01/03/2026", "02/03/2026"],
5    "time": ["09:15", "18:45"],
6})
7
8df["timestamp"] = pd.to_datetime(
9    df["date"] + " " + df["time"],
10    format="%d/%m/%Y %H:%M"
11)
12
13print(df["timestamp"])

This is especially important when day-month ordering might be ambiguous.

When the Columns Already Have Date and Time Types

Sometimes the date column is already parsed but the time column is stored separately. In that case, converting both sides to strings is acceptable, but you should know what you are normalizing.

python
1import pandas as pd
2import datetime as dt
3
4df = pd.DataFrame({
5    "date": [dt.date(2026, 3, 1), dt.date(2026, 3, 2)],
6    "time": [dt.time(9, 15), dt.time(18, 45)],
7})
8
9df["timestamp"] = pd.to_datetime(df["date"].astype(str) + " " + df["time"].astype(str))
10print(df["timestamp"])

This is often good enough and keeps the code simple.

Time Zones and Localization

Combining date and time is only part of the story. If the result represents a real-world timestamp, you may also need a timezone.

python
df["timestamp_utc"] = df["timestamp"].dt.tz_localize("UTC")
print(df["timestamp_utc"])

If the source values are local times from a known region, localize first and convert later if needed. Do not attach time zones casually after the fact without knowing what the original data meant.

Why This Can Go Wrong

Most bugs come from hidden assumptions:

  • the date format is not what you thought
  • the time column contains missing values
  • one column already has timezone information
  • the business meaning is local time, but the pipeline treats it as UTC

That is why a small explicit parsing example is safer than relying on pandas to infer everything in production pipelines.

Validation After Combining

A quick validation step is worth it:

python
print(df[["date", "time", "timestamp"]])
print(df["timestamp"].isna().sum())

If parsing produced unexpected NaT values, fix the source format or pass errors="coerce" only if you are ready to handle bad rows explicitly.

Keep the Original Columns Until You Trust the Result

In data-cleaning pipelines, do not immediately drop the source date and time columns after building the timestamp. Keeping them during validation makes it much easier to inspect bad rows, confirm timezone assumptions, and explain parsing issues to other team members before the final cleanup step.

Common Pitfalls

  • Concatenating columns without understanding their current types.
  • Letting pandas infer ambiguous date formats silently.
  • Ignoring missing or malformed time values.
  • Treating naive datetimes as if they were already in UTC.
  • Forgetting to validate the combined column after parsing.

Summary

  • For string columns, combine them and pass the result to pd.to_datetime.
  • Use an explicit format when the input pattern is known.
  • If the columns are already typed, normalizing them to strings can still be a practical solution.
  • Handle time zones deliberately after combining the values.
  • Validate the resulting timestamp column so bad input is caught early.

Course illustration
Course illustration

All Rights Reserved.