Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python Pandas - ส่งคืนอาร์เรย์ numpy ของ python datetime.time วัตถุ


ในการส่งคืนอาร์เรย์ numpy ของอ็อบเจ็กต์ python datetime.time ให้ใช้ datetimeindex.time ทรัพย์สินในหมีแพนด้า

ขั้นแรก นำเข้าไลบรารีที่จำเป็น -

import pandas as pd

สร้าง DatetimeIndex ที่มีจุด 3 และความถี่เหมือนเรา นั่นคือ นาโนวินาที เขตเวลาคือ ออสเตรเลีย/ซิดนีย์ −

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

แสดง DateTimeIndex -

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

ส่งกลับเฉพาะส่วนเวลาของการประทับเวลาโดยไม่มีข้อมูลเขตเวลา -

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

ตัวอย่าง

ต่อไปนี้เป็นรหัส -

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)

# Returns only the time part of Timestamps without timezone information
print("\nThe numpy array (time part)..\n",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')

The numpy array (date part)..
[datetime.date(2021, 10, 20) datetime.date(2021, 10, 20)
datetime.date(2021, 10, 20)]

The numpy array (time part)..
[datetime.time(2, 30, 50) datetime.time(2, 30, 50)
datetime.time(2, 30, 50)]