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.
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. 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

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)

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.