Adding Time to the Mix in Python – Combining Dates and Time for Data Science 2026

Dates And Time Mar 19, 2026

Adding Time to the Mix in Python – Combining Dates and Time for Data Science 2026

Most real-world data doesn’t come as pure dates — it includes time as well. Adding time to the mix means combining date objects with time information to create full datetime objects. This is essential for accurate timestamps, time-based feature engineering, scheduling, and any analysis that requires both date and time precision.

TL;DR — How to Add Time to Dates

  • Use datetime.combine(date, time)
  • Use datetime(year, month, day, hour, minute, second)
  • Parse strings that contain both date and time
  • Always make the result timezone-aware

1. Basic Ways to Add Time to a Date

from datetime import date, time, datetime
from zoneinfo import ZoneInfo

# Method 1: Combine a date with a time object
d = date(2026, 3, 19)
t = time(14, 30, 0)                    # 2:30 PM
dt = datetime.combine(d, t, tzinfo=ZoneInfo("UTC"))

print(dt)                              # 2026-03-19 14:30:00+00:00

2. Direct Creation of datetime

# Create full datetime in one step
event = datetime(2026, 3, 19, 14, 30, 0, tzinfo=ZoneInfo("UTC"))
print(event)

3. Real-World Data Science Examples

import pandas as pd

# Create a small demo file so this example runs as-is
# (in your project, sales_data.csv is your real export)
import numpy as np
rng = np.random.default_rng(42)
n = 60
pd.DataFrame({
    "order_id": range(1001, 1001 + n),
    "order_date": pd.date_range("2026-01-01", periods=n, freq="3D"),
    "customer_id": rng.integers(101, 121, n),
    "customer_name": rng.choice([" ana", "ben ", "chen", "dana"], n),
    "product_id": rng.integers(1, 9, n),
    "region": rng.choice(["North", "South", "East", "West"], n),
    "category": rng.choice(["Electronics", "Computers", "Books", "Home"], n),
    "payment_method": rng.choice(["card", "cash", "wallet"], n),
    "segment": rng.choice(["consumer", "business"], n),
    "quantity": rng.integers(1, 6, n),
    "amount": rng.uniform(50, 2500, n).round(2),
    "discount": rng.choice([0.0, 0.05, 0.1, 0.2], n),
}).assign(
    price=lambda d: (d["amount"] / d["quantity"]).round(2),
    profit=lambda d: (d["amount"] * rng.uniform(0.05, 0.3, n)).round(2),
).assign(
    ship_date=lambda d: d["order_date"] + pd.Timedelta(days=2),
    status=lambda d: np.where(d["amount"] > 2000, "priority", "standard"),
).to_csv("sales_data.csv", index=False)

df = pd.read_csv("sales_data.csv")

df["order_time"] = "09:30:00"   # demo time column used in example 2

# Example 1: Add default time (midnight) to date-only column
df["order_datetime"] = pd.to_datetime(df["order_date"]) + pd.Timedelta(hours=0)

# Example 2: Combine separate date and time columns
df["full_timestamp"] = pd.to_datetime(
    df["order_date"].astype(str) + " " + df["order_time"].astype(str)
)

# Example 3: Add time to a date series with timezone
df["order_datetime_utc"] = pd.to_datetime(df["order_date"]).dt.tz_localize("UTC") + pd.Timedelta(hours=14, minutes=30)

4. Best Practices in 2026

  • Always combine date and time with an explicit timezone
  • Use pandas pd.to_datetime() for vectorized operations on large datasets
  • Store the original date and time columns separately for flexibility
  • Prefer datetime.combine() when you already have separate date and time objects
  • Use pd.Timedelta for adding time to pandas datetime columns

Dates Plus Times

A datetime combines a date with a time of day. datetime.combine joins separate date and time objects, and passing tzinfo makes the result time-zone aware.

In pandas, separate date and time columns are usually joined as strings and parsed once with pd.to_datetime, or a time offset is added as a Timedelta. Normalising with .dt.normalize() goes the other way and resets times to midnight.

Decide early whether timestamps are naive or aware, and keep the whole column consistent.

Worked Example: Combining and Normalising

Date and time columns are combined, shifted and normalised.

from datetime import date, time, datetime
import pandas as pd

print(datetime.combine(date(2026, 3, 19), time(9, 45)))

df = pd.DataFrame({"day": ["2026-03-19", "2026-03-20"], "clock": ["09:45", "17:10"]})
df["at"] = pd.to_datetime(df["day"] + " " + df["clock"])
df["plus_90"] = df["at"] + pd.Timedelta(minutes=90)
df["day_start"] = df["at"].dt.normalize()
print(df[["at", "plus_90", "day_start"]])

combine creates 2026-03-19 09:45. In pandas the strings become full timestamps, adding 90 minutes moves 17:10 to 18:40, and normalize resets both to midnight.

Common Mistakes

  • Parsing date and time columns separately and adding strings later.
  • Mixing naive and aware timestamps.
  • Forgetting that normalize keeps the date and drops the time.
  • Using string concatenation on non-string columns.

Quick Summary

  • combine joins date and time.
  • Parse combined strings once with to_datetime.
  • Timedelta shifts timestamps.
  • normalize resets to midnight.

Conclusion

Adding time to the mix transforms simple dates into full timestamps that reflect real-world events. In 2026 data science, combining dates and times correctly with timezone awareness is critical for accurate analysis, scheduling, and feature engineering. Use datetime.combine(), direct construction, and pandas methods to keep your time-based data clean and reliable.

Next steps:

  • Look for any date-only columns in your datasets and add appropriate time components to create full datetime features

Related Tutorials

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices