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

Python Pandas - ดึงนาโนวินาทีจาก DateTimeIndex ด้วย nanosecond property


คำอธิบาย

ใช้ property DateTimeIndex.nanosecond ดึงค่านาโนวินาที (0-999,999,999) ของแต่ละ timestamp

ตัวอย่างโค้ด

import pandas as pd

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

print("DateTimeIndex...\n", datetimeindex)
print("DateTimeIndex frequency...\n", datetimeindex.freq)
print("\nGetting the nanoseconds..\n", datetimeindex.nanosecond)

ผลลัพธ์

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', '2021-10-20 02:30:50.000000003+11:00',
               '2021-10-20 02:30:50.000000004+11:00', '2021-10-20 02:30:50.000000005+11:00'],
              dtype='datetime64[ns, Australia/Sydney]', freq='N')

Getting the nanoseconds..
Int64Index([0, 1, 2, 3, 4, 5], dtype='int64')

Properties ส่วนประกอบเวลา

  • .hour, .minute, .second
  • .microsecond (ไมโครวินาที, 10^-6)
  • .nanosecond (นาโนวินาที, 10^-9)