🔍 Code Extractor

function dt64_to_dt

Maturity: 36

Converts a NumPy datetime64 object to a Python datetime.datetime object by calculating seconds since Unix epoch.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1999 - 2004
Complexity:
simple

Purpose

This function provides a safe conversion mechanism from NumPy's datetime64 type to Python's native datetime.datetime type. It's useful when working with mixed datetime representations or when interfacing between NumPy arrays and Python datetime operations. The conversion is done by calculating the time difference from Unix epoch (1970-01-01) in seconds and reconstructing a datetime object.

Source Code

def dt64_to_dt(dt64):
    """
    Safely converts NumPy datetime64 to a datetime object.
    """
    ts = (dt64 - np.datetime64('1970-01-01T00:00:00')) / np.timedelta64(1, 's')
    return dt.datetime(1970,1,1,0,0,0) + dt.timedelta(seconds=ts)

Parameters

Name Type Default Kind
dt64 - - positional_or_keyword

Parameter Details

dt64: A NumPy datetime64 object to be converted. This should be a valid numpy.datetime64 instance representing a point in time. The function assumes the datetime64 can be converted to seconds since epoch.

Return Value

Returns a Python datetime.datetime object representing the same point in time as the input dt64. The returned datetime object will have the same temporal value but in Python's native datetime format, which can be used with standard Python datetime operations.

Dependencies

  • numpy
  • datetime

Required Imports

import numpy as np
import datetime as dt

Usage Example

import numpy as np
import datetime as dt

def dt64_to_dt(dt64):
    ts = (dt64 - np.datetime64('1970-01-01T00:00:00')) / np.timedelta64(1, 's')
    return dt.datetime(1970,1,1,0,0,0) + dt.timedelta(seconds=ts)

# Example usage
dt64 = np.datetime64('2023-06-15T14:30:00')
result = dt64_to_dt(dt64)
print(result)  # Output: 2023-06-15 14:30:00
print(type(result))  # Output: <class 'datetime.datetime'>

Best Practices

  • Ensure the input is a valid numpy.datetime64 object before calling this function
  • Be aware that this conversion may lose precision for very high-resolution datetime64 objects (e.g., nanosecond precision)
  • The function assumes the datetime64 object can be safely converted to seconds; extremely large or small values may cause overflow
  • Consider timezone implications: numpy datetime64 is typically timezone-naive, and the returned datetime.datetime will also be timezone-naive
  • For large-scale conversions, consider using pandas.to_datetime() which may be more optimized for batch operations

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function dt_to_int 66.4% similar

    Converts various datetime types (pandas, numpy, cftime, Python datetime) to an integer timestamp with a specified time unit.

    From: /tf/active/vicechatdev/patches/util.py
  • function parse_datetime_v1 62.3% similar

    Converts various date representations (string, integer, pandas Timestamp) into a numpy datetime64 object using pandas datetime parsing capabilities.

    From: /tf/active/vicechatdev/patches/util.py
  • function cast_array_to_int64 62.0% similar

    Converts a numpy array to int64 dtype while suppressing FutureWarning about datetime64 to int64 casting that is deprecated in newer numpy/pandas versions.

    From: /tf/active/vicechatdev/patches/util.py
  • function isdatetime 59.6% similar

    Determines whether a given value (array or scalar) is a recognized datetime type, checking both NumPy datetime64 arrays and Python datetime objects.

    From: /tf/active/vicechatdev/patches/util.py
  • function numpy_scalar_to_python 53.1% similar

    Converts NumPy scalar types to their equivalent native Python types (float or int), returning the original value if it's not a NumPy numeric scalar.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse