How to convert numpys datetime64 object to datetimes datetime object?

This recipe helps you convert numpys datetime64 object to datetimes datetime object

Recipe Objective

How to convert numpy's datetime64 object to datetime's datetime object? This is also possible in Numpy by using the datetime function, we are going to convert the dates into seconds, minutes and hours. So lets understand this with practical implementation.

Step 1 - Import libraries

import numpy as np from datetime import datetime

Step 2 - Take Sample date data

Sample_date = datetime.utcnow() format_datetime_64 = np.datetime64(Sample_date) print("This is the Sample date:", format_datetime_64)

This is the Sample date: 2020-12-30T10:45:05.103693

Here in the above for Sample date we have taken the current date function, and extracted the current date. And then we have converted that date into datetime64 format for further operations.

Step 3 - Convert into Seconds

Convert_seconds = (format_datetime_64 - np.datetime64("2000-12-30T10:41:47.427944")) / np.timedelta64(1, "s") print("This is datetime64 format", format_datetime_64,"\n") print("This is Converted into Seconds format:", Convert_seconds)

This is datetime64 format 2020-12-30T10:45:05.103693 

This is Converted into Seconds format: 631152197.675749

Step 4 - Convert into Minutes

Convert_minutes = (format_datetime_64 - np.datetime64("2000-12-30T10:41:47.427944")) / np.timedelta64(1, "m") print("This is datetime64 format", format_datetime_64,"\n") print("This is Converted into Minutes format:", Convert_minutes)

This is datetime64 format 2020-12-30T10:45:05.103693 

This is Converted into Minutes format: 10519203.294595817

Step 5 - Convert into hours

Convert_hours = (format_datetime_64 - np.datetime64("2000-12-30T10:41:47.427944")) / np.timedelta64(1, "h") print("This is datetime64 format", format_datetime_64,"\n") print("This is Converted into Hours format:", Convert_hours)

This is datetime64 format 2020-12-30T10:45:05.103693 

This is Converted into Hours format: 175320.05490993027

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS