
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Convert an array of datetimes into an array of strings passing hour datetime unit 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 "units" parameter sets the datetime unit to change the precision. We have passed the hours unit
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-20T02:10', 6*60, 60, dtype='M8[m]')
Displaying our array −
print("Array...\n",arr)
Get the datatype: −
print("\nArray datatype...\n",arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...\n",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...\n",arr.shape)
Get the number of elements of the Array −
print("\nNumber of elements in the Array...\n",arr.size)
To convert an array of datetimes into an array of strings, use the numpy.datetime_as_string() method in Python Numpy −
print("\nResult...\n",np.datetime_as_string(arr, unit ='h'))
Example
import numpy as np # Create an array of datetime # The 'M' type specifies datetime arr = np.arange('2022-02-20T02:10', 6*60, 60, dtype='M8[m]') # Displaying our array print("Array...\n",arr) # Get the datatype print("\nArray datatype...\n",arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n",arr.ndim) # Get the shape of the Array print("\nOur Array Shape...\n",arr.shape) # Get the number of elements of the Array print("\nNumber of elements in the Array...\n",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 "units" parameter sets the datetime unit to change the precision # We have passed the hours unit print("\nResult...\n",np.datetime_as_string(arr, unit ='h'))
Output
Array... ['2022-02-20T02:10' '2022-02-20T03:10' '2022-02-20T04:10' '2022-02-20T05:10' '2022-02-20T06:10' '2022-02-20T07:10'] Array datatype... datetime64[m] Array Dimensions... 1 Our Array Shape... (6,) Number of elements in the Array... 6 Result... ['2022-02-20T02' '2022-02-20T03' '2022-02-20T04' '2022-02-20T05' '2022-02-20T06' '2022-02-20T07']
- Related Articles
- Convert an array of datetimes into an array of strings passing minutes datetime unit 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 in Python
- Convert an array of datetimes into an array of strings with UTC timezone in Python
- Convert an array of datetimes into an array of strings with pytz timezone object in Python
- Convert an array of objects into plain object in JavaScript
- Python - Ways to convert array of strings to array of floats
- Update an array of strings nested within an array of objects in MongoDB
- How convert an array of Strings in a single String in java?
- How to convert an object into an array in JavaScript?
- Convert JS array into an object - JavaScript
- How to Convert Hashtable into an Array?
- How to convert an array into a complex array JavaScript?
- How to convert an array into JavaScript string?
- How to convert an array of characters into a string in C#?

Advertisements