Numpy datetime64 to datetime and Vice-Versa implementation

Numpy datetime64 to datetime

Suppose you have time-series data and want to manipulate it. Then it’s obvious that you have to use NumPy DateTime for conversion and manipulation. In this coding article, I will show you how to convert NumPy datatime64 to DateTime and DateTime to datetime64.

Before starting the same article I thought to introduce this amazing video course on numpy for such basics and doubts. See article reading will definitely solve your problem for the short term but taking this simple video course make your basics strong in numpy.

All the coding has been done on PyCharm. So make sure you have already installed Pycharm on your system. Just follow the steps to get the same output according to this tutorial.

What is the use of datetime?

The datatime is a python module for dates and times. Using this module you can convert any date string to datetime. There are many functions in this module that allow you to manipulate date and time. You can also perform arithmetic calculations on a date using this module. Below are the use cases of the datetime module.

  1. You can find the current date and time using the datetime.datetime.now() function.
  2. Using the strptime() you can format the date and time.
  3. You can also know the timezones of the different countries using it.
  4. Find the difference between two times using the timdelta
  5. You can also compare datetime objects to know which date or time is earlier or later.

Step by Step to Convert Numpy datetime64 to DateTime

Step 1: Import all the necessary libraries.

Here we are using two libraries one is NumPy and the other is datetime. Let’s import it using the import statement.

import numpy as np
from datetime import datetime

Step 2: Create a Sample date in the format datetime64.

First of all, I am creating a single datetime64 and converting it to datetime. Then an array of datetime64 type. After that, You can create datetime64 format using the numpy.datetime64() format.

day = np.datetime64("2020-03-30")

To convert it to datetime format then you have to use astype() method and just pass the datetime as an argument.

day_changed = day.astype(datetime)

Conversion of an array of datetime64 type

Let’s create an array of days using numpy.arange() method of the format datetime64. Just copy the code and execute the same.

# range of the date
date_range = np.arange("2020-03","2020-04",dtype='datetime64[D]')
print(date_range)

To convert each of the dates in the date range you have to use the same astype() method and passing the datetime as an argument.

print(date_range.astype(datetime))

Conversion of Numpy datetime64 to datetime

 

Step by Step to Convert datetime to Numpy datetime

In this section, you will know how to convert datetime to numpy datetime. Just follow all the steps given below.

Step 1: Import all the necessary libraries.

import numpy as np
from datetime import datetime

Step 2: Create a sample date in datetime format.

today = datetime.today()

It will assign today’s date and time to the variable. If you print out the type of today then it will show in the format of datetime.

Step 3: Convert datetime to NumPy datetime format.

You can change the datetime to numpy datetime using the numpy.datetime64() method. Just pass the datetime object just like below.

# conversion of datetime to numpy datetime
numpy_date = np.datetime64(today)

To know the type of the numpy_date use the type() method.

Below is the full code for this section.

import numpy as np
from datetime import datetime


def main():

    today = datetime.today()
    print(today)
    print(type(today))

    # conversion of datetime to numpy datetime
    numpy_date = np.datetime64(today)
    print(numpy_date)
    print(type(numpy_date))


if __name__ == '__main__':
    main()

Output

Conversion of datetime to numpy datetime
Conversion of datetime to numpy datetime

That’s all, these are steps to convert datetime64 to datetime and vice-versa. Just follow it to understand it clearly. Even if you have any other queries then you can contact us for more information.

 

Other Questions

Question: How to convert datetime64 [ns utc] to datetime

To convert UTC datetime to datetime then you have to remove the timezone from the input DateTime. To do so you have to use the tz_localize() function. Use the following line in your code to convert to datetime.

df['timestamp'] = pd.to_datetime(df.timestamp).dt.tz_localize(None)
Numpy datetime64
Numpy datetime64

Thanks 

Data Science Learner Team

 

Source:

Numpy Datetime Offical Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner