In this article, we will convert an array of Datetimes into an array of strings. we have an array whose data type is DateTime and we want to change it to string. The shape and size of the array will be the same as the input array but the data type will be different. We will begin with an introduction and then progress through the methods for converting an array of Datetimes to an array of strings. The Numpy module will be used to work with arrays. We will create an array of datetime> datatype and convert it to an array of string> datatype. A Python library for general-purpose array processing used in scientific computing is called Numerical Python (NumPy). Here, We discussed two methods with examples and converted the array of Datetimes into an array of strings.
To begin, open a terminal and run the following command to install the NumPy module:
pip install numpy
Convert an Array Datetimes into an Array of Strings by Passing Minutes Datetime unit in Python
We will convert this array into an array of strings using the np.datetime_as_string() method. This method takes the following arguments:
Syntax: numpy.datetime_as_string(arr, unit=None, timezone=’naive’, casting=’same_kind’)
- array of datetime64 – The array of UTC timestamps to format.
- unit – One of None, ‘auto’, or a datetime unit.
- timezone{‘naive’, ‘UTC’, ‘local’} or tzinfo Timezone information to use when displaying the DateTime.
- casting{‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’} – Casting to allow when changing between datetime units.
Returns: str_arrndarray: An array of strings the same shape as arr.
Example
You must first install the NumPy module before proceeding with this method. After installation import the module into your project. we have created the following array by specifying the array datatype as Datetimes. Here we specify the dtype=’m’ means the array will be of data type as Datetime. Here is the code for this method, unit – ‘m’ specifies the minutes unit. and then we will verify the conversion by printing the array into the terminal. You can see in the output we successfully converted the array of Datetimes into an array of strings.
Python3
# importing the module import numpy as np # Creating the array of datetimes array = np.array(( '2022-12-01T11:00' , '2022-12-01T12:00' , '2022-12-01T01:00' , '2022-12-01T02:00' ), dtype = 'M' ) print ( " Array data type : " , type (array)) # method converting an array into an array of strings ar = np.datetime_as_string(array, unit = 'm' ) # printing the data type print ( " Array data type : " , type (ar[ 1 ])) |
Output:
Array data type : <class 'numpy.ndarray'> Array data type : <class 'numpy.str_'>
Convert an Array of Datetimes into an Array of Strings using np.array()
Here, the first argument is a DateTime sequence object or tuple, and the second argument (dtype) specifies the data type for the resulting array. This method returns ndarray, An array object satisfying the specified requirements.
Example
To begin, we will import the NumPy module and create an array of Datetimes by specifying the data type with the <dtype=npy.datetime64> argument. Using the print statement, we are checking and confirming the datatype of this array. Datetime64 is the datatype in NumPy that allows you to create an Array of DateTime. Then, we’ll use the NumPy.array str() method, passing the array as an argument. This method will convert the array of Datetimes into a string array and then overwrite it.
Python3
# Importing the numpy module import numpy as npy # Creating an numpy array by specifying the data type as datetime arr = npy.array(( '2022-11-20T12:00' , '2022-11-20T01:00' , '2022-11-20T03:00' , '2022-11-20T04:00' ), dtype = npy.datetime64) # Printing the data type before conversion print ( "Before Conversion " , arr.dtype) # Method Converting the Array of DateTimes into Strings arr = npy.array_str(arr) # printing the data type after conversion print ( "After Conversion " , type (arr)) # print(type(arr[0])) |
Output:
Before Conversion datetime64[m] After Conversion <class 'str'>