How to Convert NumPy datetime64 to Timestamp?

When working with dates and times in Python, NumPy's datetime64 data type provides efficient storage for temporal data. However, you may need to convert these objects to pandas Timestamp format to access pandas' extensive time-series functionality.

Converting NumPy datetime64 to Timestamp unlocks powerful capabilities for time-series analysis, data manipulation, and visualization. This conversion enables working with time-indexed data, performing date arithmetic, and applying various time-related operations.

Using pd.Timestamp()

The most direct approach is using pandas' Timestamp() constructor, which seamlessly converts NumPy datetime64 objects ?

import numpy as np
import pandas as pd

# Create a NumPy datetime64 object
np_datetime = np.datetime64('2023-05-22T12:30:00')

# Convert NumPy datetime64 to Timestamp
timestamp = pd.Timestamp(np_datetime)

print("Original datetime64:", np_datetime)
print("Converted Timestamp:", timestamp)
print("Type:", type(timestamp))
Original datetime64: 2023-05-22T12:30:00
Converted Timestamp: 2023-05-22 12:30:00
Type: <class 'pandas._libs.tslibs.timestamps.Timestamp'>

Using pd.to_datetime()

The pd.to_datetime() method provides an alternative approach, especially useful when converting multiple datetime64 objects ?

import numpy as np
import pandas as pd

# Create a NumPy datetime64 object
np_datetime = np.datetime64('2023-05-22T12:30:00')

# Convert using to_datetime
timestamp = pd.to_datetime(np_datetime)

print("Original datetime64:", np_datetime)
print("Converted Timestamp:", timestamp)

# Convert multiple datetime64 objects
np_array = np.array(['2023-05-22T12:30:00', '2023-05-23T14:45:30'], dtype='datetime64')
timestamps = pd.to_datetime(np_array)

print("\nMultiple conversions:")
print(timestamps)
Original datetime64: 2023-05-22T12:30:00
Converted Timestamp: 2023-05-22 12:30:00

Multiple conversions:
DatetimeIndex(['2023-05-22 12:30:00', '2023-05-23 14:45:30'], dtype='datetime64[ns]', freq=None)

Comparison

Method Best For Return Type
pd.Timestamp() Single datetime64 objects Timestamp
pd.to_datetime() Arrays or multiple objects Timestamp or DatetimeIndex

Working with Converted Timestamps

Once converted, you can leverage pandas' powerful datetime functionality ?

import numpy as np
import pandas as pd

# Convert datetime64 to Timestamp
np_datetime = np.datetime64('2023-05-22T12:30:00')
timestamp = pd.Timestamp(np_datetime)

# Access timestamp components
print("Year:", timestamp.year)
print("Month:", timestamp.month)
print("Day:", timestamp.day)
print("Hour:", timestamp.hour)

# Perform date arithmetic
next_week = timestamp + pd.Timedelta(days=7)
print("Next week:", next_week)
Year: 2023
Month: 5
Day: 22
Hour: 12
Next week: 2023-05-29 12:30:00

Conclusion

Both pd.Timestamp() and pd.to_datetime() effectively convert NumPy datetime64 objects to pandas Timestamps. Use pd.Timestamp() for single conversions and pd.to_datetime() for arrays or batch processing.

Updated on: 2026-03-27T09:35:46+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements