Converting between datetime, Pandas Timestamp and Numpy datetime64


In Python, there are three commonly used datetime-related classes: datetime.datetime, pandas.Timestamp, and numpy.datetime64. While datetime is part of the Python standard library, Timestamp and datetime64 are part of the Pandas and NumPy libraries respectively. Converting between these classes can sometimes be a bit confusing, but it is a common task in data analysis and manipulation.

As an overview:

  • datetime.datetime is a class from the built-in datetime module that represents a point in time with date and time components, down to the microsecond level. It can be used for a wide range of operations such as arithmetic, formatting, and comparison.
  • pandas.Timestamp is a class from the pandas library that also represents a point in time with date and time components, down to the nanosecond level. It provides added features and convenience methods that make it easier to work with date and time data in a pandas DataFrame or Series.
  • numpy.datetime64 is a NumPy data type for working with dates and times. It provides a more efficient and flexible way of working with dates and times and is optimized for use in numerical operations.

To convert between these types we can use the following approaches.

1: Converting datetime.datetime to pandas.Timestamp:

import datetime
import pandas as pd

dt = datetime.datetime(2022, 3, 15, 10, 30, 0)
ts = pd.Timestamp(dt)
print(ts)  # Output: 2022-03-15 10:30:00

2: Converting datetime.datetime to numpy.datetime64:

import datetime
import numpy as np

dt = datetime.datetime(2022, 3, 15, 10, 30, 0)
dt64 = np.datetime64(dt)
print(dt64)  # Output: 2022-03-15T10:30:00

3: Converting pandas.Timestamp to datetime.datetime:

import datetime
import pandas as pd

ts = pd.Timestamp('2022-03-15 10:30:00')
dt = ts.to_pydatetime()
print(dt)  # Output: 2022-03-15 10:30:00

4: Converting pandas.Timestamp to numpy.datetime64:

import numpy as np
import pandas as pd

ts = pd.Timestamp('2022-03-15 10:30:00')
ts64 = np.datetime64(ts)
print(ts64)  # Output: 2022-03-15T10:30:00.000000000

5: Converting numpy.datetime64 to datetime.datetime:

import datetime
import numpy as np

dt64 = np.datetime64('2022-03-15T10:30:00')
dt = dt64.astype(datetime.datetime)
print(dt)  # Output: 2022-03-15 10:30:00

6: Converting numpy.datetime64 to pandas.Timestamp:

import pandas as pd
import numpy as np

dt64 = np.datetime64('2022-03-15T10:30:00')
ts = pd.Timestamp(dt64)
print(ts)  # Output: 2022-03-15 10:30:00

Example

Here's a single example that shows all six possible conversions between datetime.datetime, pandas.Timestamp, and numpy.datetime64:

import datetime
import pandas as pd
import numpy as np

# Create a datetime object
dt = datetime.datetime(2022, 3, 15, 10, 30, 0)

# Convert datetime object to pandas Timestamp
ts1 = pd.Timestamp(dt)
print("datetime.datetime to pandas.Timestamp:", ts1)

# Convert datetime object to numpy datetime64
dt64 = np.datetime64(dt)
print("datetime.datetime to numpy.datetime64:", dt64)

# Convert pandas Timestamp to datetime object
dt2 = ts1.to_pydatetime()
print("pandas.Timestamp to datetime.datetime:", dt2)

# Convert pandas Timestamp to numpy datetime64
ts64 = np.datetime64(ts1)
print("pandas.Timestamp to numpy.datetime64:", ts64)

# Convert numpy datetime64 to datetime object
dt3 = dt64.astype(datetime.datetime)
print("numpy.datetime64 to datetime.datetime:", dt3)

# Convert numpy datetime64 to pandas Timestamp
ts2 = pd.Timestamp(dt64)
print("numpy.datetime64 to pandas.Timestamp:", ts2)

Output:

datetime.datetime to pandas.Timestamp: 2022-03-15 10:30:00
datetime.datetime to numpy.datetime64: 2022-03-15T10:30:00
pandas.Timestamp to datetime.datetime: 2022-03-15 10:30:00
pandas.Timestamp to numpy.datetime64: 2022-03-15T10:30:00
numpy.datetime64 to datetime.datetime: 2022-03-15 10:30:00
numpy.datetime64 to pandas.Timestamp: 2022-03-15 10:30:00

This code example creates a datetime.datetime object and then converts it to a pandas.Timestamp and a numpy.datetime64 object. It then converts the pandas.Timestamp and numpy.datetime64 objects back to datetime.datetime objects, and also converts them to each other's types. Finally, it prints out the result of each conversion to the console.

Conclusion

In conclusion, we explored three different data types for representing date and time information in Python: datetime.datetime, pandas.Timestamp, and numpy.datetime64. Understanding the differences between these types and knowing how to convert between them is an essential skill for working with dates and times in Python, whether for data analysis, scientific computing, or general programming tasks.