Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Convert an array of datetimes into an array of strings passing units 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.
Syntax
numpy.datetime_as_string(arr, unit=None, timezone='naive', casting='same_kind')
Parameters
The key parameters are ?
- arr − Array of datetime64 values to convert
- unit − Datetime unit for precision ('Y', 'M', 'D', 'h', 'm', 's', 'ms', etc.)
- timezone − Timezone handling ('naive', 'UTC', or 'local')
Creating a DateTime Array
First, let's create an array of datetime values using np.arange() ?
import numpy as np
# Create an array of datetime with 'M8[m]' dtype (minutes precision)
arr = np.arange('2022-02-20T02:10', 6*60, 60, dtype='M8[m]')
print("Original datetime array:")
print(arr)
print("\nDatatype:", arr.dtype)
print("Shape:", arr.shape)
Original datetime 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'] Datatype: datetime64[m] Shape: (6,)
Converting to String with Different Units
Using Seconds Precision
import numpy as np
arr = np.arange('2022-02-20T02:10', 6*60, 60, dtype='M8[m]')
# Convert to strings with seconds precision
string_array_seconds = np.datetime_as_string(arr, unit='s')
print("With seconds unit:")
print(string_array_seconds)
With seconds unit: ['2022-02-20T02:10:00' '2022-02-20T03:10:00' '2022-02-20T04:10:00' '2022-02-20T05:10:00' '2022-02-20T06:10:00' '2022-02-20T07:10:00']
Using Different Units
import numpy as np
arr = np.arange('2022-02-20T02:10', 6*60, 60, dtype='M8[m]')
# Different unit precisions
print("Minutes unit:")
print(np.datetime_as_string(arr, unit='m'))
print("\nHours unit:")
print(np.datetime_as_string(arr, unit='h'))
print("\nDays unit:")
print(np.datetime_as_string(arr, unit='D'))
Minutes unit: ['2022-02-20T02:10' '2022-02-20T03:10' '2022-02-20T04:10' '2022-02-20T05:10' '2022-02-20T06:10' '2022-02-20T07:10'] Hours unit: ['2022-02-20T02' '2022-02-20T03' '2022-02-20T04' '2022-02-20T05' '2022-02-20T06' '2022-02-20T07'] Days unit: ['2022-02-20' '2022-02-20' '2022-02-20' '2022-02-20' '2022-02-20' '2022-02-20']
Common Use Cases
| Unit | Format | Best For |
|---|---|---|
| 'D' | YYYY-MM-DD | Date-only display |
| 'h' | YYYY-MM-DDTHH | Hourly precision |
| 'm' | YYYY-MM-DDTHH:MM | Minute precision |
| 's' | YYYY-MM-DDTHH:MM:SS | Full timestamp |
Conclusion
Use numpy.datetime_as_string() to convert datetime arrays to string format. The unit parameter controls precision, allowing you to format timestamps according to your specific requirements.
Advertisements
