

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert an array of datetimes into an array of strings with UTC timezone in Python
To convert an array of datetimes into an array of strings, use the numpy.datetime_as_string() method in Python Numpy. The method returns an array of strings the same shape as the input array. The first parameter is the array of UTC timestamps to format. The second parameter is the "timezone", the Timezone information to use when displaying the datetime. If ‘UTC’, end with a Z to indicate UTC time.
Steps
At first, import the required library −
import numpy as np
Create an array of datetime. The 'M' type specifies datetime −
arr = np.arange('2022-02-20T03:25', 6*60, 60, dtype='M8[m]')
Displaying our array −
print("Array... ",arr)
Get the datatype −
print(" Array datatype... ",arr.dtype)
Get the dimensions of the Array −
print(" Array Dimensions... ",arr.ndim)
Get the shape of the Array −
print(" Our Array Shape... ",arr.shape)
Get the number of elements of the Array −
print(" Number of elements in the Array... ",arr.size)
To convert an array of datetimes into an array of strings, use the numpy.datetime_as_string() method in Python Numpy. The method returns an array of strings the same shape as the input array. The first parameter is the array of UTC timestamps to format. The second parameter is the "timezone", the Timezone information to use when displaying the datetime. If ‘UTC’, end with a Z to indicate UTC time. If ‘local’, convert to the local timezone first, and suffix with a +-#### timezone offset. If a tzinfo object, then do as with ‘local’, but use the specified timezone −
print(" Result... ",np.datetime_as_string(arr, timezone = 'UTC'))
Example
import numpy as np # Create an array of datetime # The 'M' type specifies datetime arr = np.arange('2022-02-20T03:25', 6*60, 60, dtype='M8[m]') # Displaying our array print("Array... ",arr) # Get the datatype print(" Array datatype... ",arr.dtype) # Get the dimensions of the Array print(" Array Dimensions... ",arr.ndim) # Get the shape of the Array print(" Our Array Shape... ",arr.shape) # Get the number of elements of the Array print(" Number of elements in the Array... ",arr.size) # To convert an array of datetimes into an array of strings, use the numpy.datetime_as_string() method in Python Numpy # The method returns an array of strings the same shape as the input array print(" Result... ",np.datetime_as_string(arr, timezone = 'UTC'))
Output
Array... ['2022-02-20T03:25' '2022-02-20T04:25' '2022-02-20T05:25' '2022-02-20T06:25' '2022-02-20T07:25' '2022-02-20T08:25'] Array datatype... datetime64[m] Array Dimensions... 1 Our Array Shape... (6,) Number of elements in the Array... 6 Result... ['2022-02-20T03:25Z' '2022-02-20T04:25Z' '2022-02-20T05:25Z' '2022-02-20T06:25Z' '2022-02-20T07:25Z' '2022-02-20T08:25Z']
- Related Questions & Answers
- Convert an array of datetimes into an array of strings with pytz timezone object in Python
- Convert an array of datetimes into an array of strings in Python
- Convert an array of datetimes into an array of strings passing units in Python
- Convert an array of datetimes into an array of strings passing hour datetime unit in Python
- Convert an array of datetimes into an array of strings passing minutes datetime unit in Python
- Convert an array of objects into plain object in JavaScript
- Update an array of strings nested within an array of objects in MongoDB
- Convert JS array into an object - JavaScript
- How do you convert an array of ObjectIds into an array of embedded documents with a field containing the original array element value?
- Python - Ways to convert array of strings to array of floats
- How to convert an object into an array in JavaScript?
- Convert array into array of subarrays - JavaScript
- How convert an array of Strings in a single String in java?
- How to convert an array into a complex array JavaScript?
- How to convert an array into JavaScript string?