Convert Array of Datetimes into Array of Strings in Python
Last Updated :
28 Apr, 2025
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'>
Similar Reads
Converting string into DateTime in Python The goal is to convert a date string like "2021/05/25" into a Python-recognized DateTime object such as 2021-05-25 00:00:00. This enables accurate and consistent date operations like comparisons, calculations and formatting when working with time-related data from sources like files or user input. L
2 min read
How to convert Python's .isoformat() string back into datetime object In this article, we will learn how to convert Pythons .isoFormat() string back into a datetime object. Here we are using the current time and for that, we will store the current time in the current_time variable. The function now() of this module, does the job perfectly. Example: Getting current tim
1 min read
Create Python Datetime from string In this article, we are going to see how to create a python DateTime object from a given string. For this, we will use the datetime.strptime() method. The strptime() method returns a DateTime object corresponding to date_string, parsed according to the format string given by the user. Syntax:Â datet
4 min read
How to convert DateTime to integer in Python Python provides a module called DateTime to perform all the operations related to date and time. It has a rich set of functions used to perform almost all the operations that deal with time. It needs to be imported first to use the functions and it comes along with python, so no need to install it s
2 min read
Convert any Dates in Spreadsheets using Python In this article, we are going to see how to convert any Dates in Spreadsheets using Python. Used file: This file comprises a single column entitled 'Date' and stores random dates of 2021 in some different forms of format. Approach:We'll begin by importing the pandas library.Let's have a look at the
3 min read
Convert string to datetime in Python with timezone Converting a string to a datetime in Python with timezone means parsing a date-time string and creating a timezone-aware datetime object. For example, a string like '2021-09-01 15:27:05.004573 +0530' can be converted to a Python datetime object that accurately represents the date, time and timezone.
2 min read
Convert Date To Datetime In Python When you're programming, dealing with dates and times is important, and Python provides tools to manage them well. This article is about changing dates into date times in Python. We'll explore methods that can help you switch between these two types of data. Whether you're building a website or work
3 min read
Convert datetime string to YYYY-MM-DD-HH:MM:SS format in Python In this article, we are going to convert the DateTime string into the %Y-%m-%d-%H:%M:%S format. For this task strptime() and strftime() function is used. strptime() is used to convert the DateTime string to DateTime in the format of year-month-day hours minutes and seconds Syntax: datetime.strptime(
2 min read
Convert Python datetime to epoch Epoch time is a way to represent time as the number of seconds that have passed since January 1, 1970, 00:00:00 UTC. It is also known as Unix time or POSIX time and it serves as a universal point of reference for representing dates and times. Its used in various applications like file timestamps, da
2 min read
How to convert datetime to date in Python In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f
3 min read