datetime
Timestamp
datetime64
date conversion
datetime manipulation

Converting between datetime, Timestamp and datetime64

Master System Design with Codemia

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

Introduction

In Python data work, time values often move between three representations: standard-library datetime, pandas Timestamp, and NumPy datetime64. The conversions are simple once you know which library owns the value and whether timezone information must be preserved. Most mistakes come from silent timezone loss or unit mismatches, not from the conversion syntax itself.

What Each Type Is Good At

The three types overlap, but they are optimized for different jobs:

  • 'datetime.datetime is general-purpose Python application code.'
  • 'pandas.Timestamp is rich tabular time handling with indexes and time zones.'
  • 'numpy.datetime64 is compact and efficient for vectorized array operations.'

You usually convert because data enters in one form and analysis or APIs need another.

datetime to Timestamp

Pandas can wrap a normal Python datetime directly.

python
1from datetime import datetime, timezone
2import pandas as pd
3
4dt = datetime(2026, 3, 7, 12, 30, tzinfo=timezone.utc)
5ts = pd.Timestamp(dt)
6
7print(dt)
8print(ts)
9print(type(ts))

This preserves timezone data when the original datetime is timezone-aware.

Timestamp to datetime

Use .to_pydatetime() when you need a plain Python object.

python
1import pandas as pd
2
3ts = pd.Timestamp("2026-03-07T12:30:00Z")
4dt = ts.to_pydatetime()
5
6print(ts)
7print(dt)
8print(type(dt))

This is common when passing values into standard Python libraries or external APIs that do not understand pandas types.

datetime to numpy.datetime64

NumPy accepts many ISO-like strings and Python datetimes, but you should be explicit about precision when possible.

python
1from datetime import datetime
2import numpy as np
3
4dt = datetime(2026, 3, 7, 12, 30, 15)
5ndt = np.datetime64(dt, "s")
6
7print(ndt)
8print(type(ndt))

The unit such as "s", "ms", or "ns" matters because datetime64 stores time with a fixed resolution.

numpy.datetime64 to Timestamp

Pandas can convert a NumPy datetime value directly.

python
1import numpy as np
2import pandas as pd
3
4ndt = np.datetime64("2026-03-07T12:30:15", "ns")
5ts = pd.Timestamp(ndt)
6
7print(ndt)
8print(ts)

This is especially useful when NumPy arrays feed into DataFrames.

Timestamp to numpy.datetime64

Pandas timestamps expose a NumPy-friendly representation.

python
1import pandas as pd
2
3ts = pd.Timestamp("2026-03-07 12:30:15")
4ndt = ts.to_datetime64()
5
6print(ndt)
7print(type(ndt))

This is efficient when you want array operations without bringing a full Series along.

Time Zone Caveats

This is the part that breaks most code. numpy.datetime64 does not behave like a fully timezone-aware pandas Timestamp. If your workflow crosses time zones, normalize deliberately before converting.

A practical safe pattern is:

  1. keep source values timezone-aware in pandas
  2. convert to UTC
  3. only then move into NumPy if needed
python
1import pandas as pd
2
3ts = pd.Timestamp("2026-03-07 07:30:15", tz="America/Toronto")
4ts_utc = ts.tz_convert("UTC")
5print(ts_utc)

Do not assume a naive datetime means UTC. In many systems it means "unknown local time", which is much more dangerous.

Vectorized Conversion in Columns

For real datasets, conversions often happen on whole columns, not one value at a time.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "raw": ["2026-03-07 12:00:00", "2026-03-08 08:15:00"]
5})
6
7df["ts"] = pd.to_datetime(df["raw"], utc=True)
8df["np"] = df["ts"].astype("datetime64[ns]")
9
10print(df)

This is the normal pandas path for ETL and analytics work.

Common Pitfalls

  • Converting naive datetimes without deciding what timezone they represent.
  • Forgetting that datetime64 precision depends on the chosen unit.
  • Assuming all conversions preserve timezone metadata equally.
  • Mixing scalar conversions and column conversions without checking resulting dtype.
  • Comparing values from different libraries before normalizing them to a shared timezone and precision.

Summary

  • 'datetime, Timestamp, and datetime64 solve related but different problems.'
  • Use pandas as the safest bridge when values move between Python and NumPy time types.
  • Be explicit about timezone handling before conversion.
  • Pay attention to datetime64 units such as seconds or nanoseconds.
  • Validate the resulting type and dtype instead of assuming conversion preserved everything.

Course illustration
Course illustration

All Rights Reserved.