Python Pandas – 返回Python datetime.date对象的numpy数组

Python Pandas – 返回Python datetime.date对象的numpy数组

要返回Python datetime.date对象的numpy数组,请在Pandas中使用 datetimeindex.date 属性。

首先,导入所需的库−

import pandas as pd

创建一个具有3个时期和频率为纳秒的DatetimeIndex−

datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=3, tz='Australia/Sydney', freq='ns')

显示DatetimeIndex−

print("DateTimeIndex...\n", datetimeindex)

仅返回没有时区信息的Timestamps的日期部分−

print("\nThe numpy array (date part)..\n",datetimeindex.date)

例子

以下是代码−

import pandas as pd

# DatetimeIndex with period 3 and frequency as us i.e. 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)]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程