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
Python Pandas - Return numpy array of python datetime.time objects
To return numpy array of python datetime.time objects, use the datetimeindex.time property in Pandas. This property extracts only the time component from datetime objects, discarding date and timezone information.
Syntax
DatetimeIndex.time
This property returns a numpy array containing datetime.time objects.
Creating a DatetimeIndex
First, let's create a DatetimeIndex with timezone information ?
import pandas as pd
# Create DatetimeIndex with nanosecond frequency
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=3, tz='Australia/Sydney', freq='ns')
print("DateTimeIndex...")
print(datetimeindex)
DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:50+11:00',
'2021-10-20 02:30:50.000000001+11:00',
'2021-10-20 02:30:50.000000002+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='N')
Extracting Time Components
Use the .time property to extract only the time part without timezone information ?
import pandas as pd
# Create DatetimeIndex
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=3, tz='Australia/Sydney', freq='ns')
# Extract time components
time_array = datetimeindex.time
print("Time components:")
print(time_array)
print(f"Type: {type(time_array)}")
print(f"Element type: {type(time_array[0])}")
Time components: [datetime.time(2, 30, 50) datetime.time(2, 30, 50, 0, 1) datetime.time(2, 30, 50, 0, 2)] Type: <class 'numpy.ndarray'> Element type: <class 'datetime.time'>
Complete Example
Here's a comprehensive example showing both date and time extraction ?
import pandas as pd
# Create DatetimeIndex with timezone
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=3, tz='Australia/Sydney', freq='ns')
print("DateTimeIndex:")
print(datetimeindex)
# Extract date components
print("\nDate components:")
print(datetimeindex.date)
# Extract time components
print("\nTime components:")
print(datetimeindex.time)
DateTimeIndex:
DatetimeIndex(['2021-10-20 02:30:50+11:00',
'2021-10-20 02:30:50.000000001+11:00',
'2021-10-20 02:30:50.000000002+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='N')
Date components:
[datetime.date(2021, 10, 20) datetime.date(2021, 10, 20)
datetime.date(2021, 10, 20)]
Time components:
[datetime.time(2, 30, 50) datetime.time(2, 30, 50, 0, 1)
datetime.time(2, 30, 50, 0, 2)]
Key Points
- The
.timeproperty returns a numpy array ofdatetime.timeobjects - Timezone information is stripped from the result
- Microsecond and nanosecond precision is preserved when present
- Each element in the array is a standard Python
datetime.timeobject
Conclusion
The DatetimeIndex.time property provides an efficient way to extract time components from datetime data. It returns a numpy array of Python datetime.time objects while preserving precision and removing timezone information.
