Pandas: combine date and time columns into one datetime column
Codemia Knowledge Hub ·
To combine separate date and time columns in pandas, concatenate them with a space and pass the result to pd.to_datetime. If the columns hold datetime.date and datetime.time objects, convert them with .astype(str) first. Specify a format when you know it, and handle time zones deliberately.
Combining string columns
When both parts are stored as strings:
pandas parses the combined string into a single datetime64 column.
Passing an explicit format
If the input format is stable, specify it. Parsing is faster and less likely to misread values, which matters most when day and month order could be ambiguous:
Combining datetime.date and datetime.time columns
When the columns are already typed, converting both to strings is acceptable and keeps the code simple, as long as you know what you are normalizing:
Adding a time zone
If the result represents a real-world timestamp, you may also need a time zone:
If the source values are local times from a known region, localize to that region first and convert later if needed. Don't attach a time zone after the fact without knowing what the original data meant, and don't treat naive datetimes as if they were already UTC.
What usually goes 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 explicit parsing is safer in production pipelines than letting pandas infer everything.
Validating the combined column
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 date and time columns until you trust the result. Having them during validation makes it much easier to inspect bad rows, confirm time zone assumptions and explain parsing issues to teammates before the final cleanup.
Related reading
- Combine results from batch RDD with streaming RDD in Apache Spark
- Combine two columns of text in pandas dataframe
- Combining bag of words and other features in one model using sklearn and pandas
- Combining feature extraction classes in scikit-learn
- Combine several images horizontally with Python
- Combining conda environment.yml with pip requirements.txt
- Combining random forests built with different training sets in R
- Combining two Series into a DataFrame in pandas
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.