 # Numpy datetime64 to datetime and Vice-Versa implementation

Suppose you have time-series data and want to manipulate it. Then its 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.

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.

## 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))`` ## Step by Step to Convert datetime to Numpy datetime

In this section, you will know how to convert datetime to numpy datetime. Juse 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 printout 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

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 query then you can contact us for more information.

Thanks

Data Science Learner Team

Source:

Numpy Datetime Offical Documentation 