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
Python Pandas - Return numpy array of python datetime.date objects
To return a numpy array of Python datetime.date objects from a Pandas DatetimeIndex, use the date property. This property extracts only the date part from timestamps, removing timezone information and returning standard Python date objects.
Syntax
datetimeindex.date
Where datetimeindex is a Pandas DatetimeIndex object.
Creating a DatetimeIndex
First, let's create a DatetimeIndex with timezone information ?
import pandas as pd
# Create DatetimeIndex with 3 periods and 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 Date Objects
Now extract the date part using the date property ?
import pandas as pd
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=3, tz='Australia/Sydney', freq='ns')
# Extract numpy array of datetime.date objects
date_array = datetimeindex.date
print("The numpy array (date part)...")
print(date_array)
print(f"\nType of array: {type(date_array)}")
print(f"Type of first element: {type(date_array[0])}")
The numpy array (date part)... [datetime.date(2021, 10, 20) datetime.date(2021, 10, 20) datetime.date(2021, 10, 20)] Type of array: <class 'numpy.ndarray'> Type of first element: <class 'datetime.date'>
Complete Example
Here's a comprehensive example showing the conversion process ?
import pandas as pd
# DatetimeIndex with period 3 and frequency as nanoseconds
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=3, tz='Australia/Sydney', freq='ns')
# Display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# Returns only the date part of Timestamps without timezone information
print("\nThe numpy array (date part)..\n", datetimeindex.date)
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')
The numpy array (date part)..
[datetime.date(2021, 10, 20) datetime.date(2021, 10, 20)
datetime.date(2021, 10, 20)]
Key Points
- The
dateproperty returns a numpy array of Pythondatetime.dateobjects - Timezone information is removed from the original timestamps
- Time components (hour, minute, second) are discarded, keeping only the date
- Each element in the returned array is a standard Python
datetime.dateobject
Conclusion
The date property of a DatetimeIndex provides an efficient way to extract date objects from timestamp data. This is useful when you only need the date portion without time or timezone information.
Advertisements
