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

Python – วิธีตรวจสอบวันที่ที่ขาดหายไปใน Pandas


เพื่อตรวจสอบวันที่ขาดหายไป เริ่มต้นด้วยการกำหนด dictionary ของรายการที่มีบันทึกวันที่ เช่น Date of Purchase ในตัวอย่าง:

# dictionary of lists 
d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 
     'Date_of_purchase': ['2020-10-10', '2020-10-12', '2020-10-17', '2020-10-16', '2020-10-19', '2020-10-22']}

สร้าง DataFrame จาก dictionary:

dataFrame = pd.DataFrame(d)

ตั้งค่า Date_of_purchase เป็น index:

dataFrame = dataFrame.set_index('Date_of_purchase')

ใช้ to_datetime() แปลงสตริงเป็น DateTime object:

dataFrame.index = pd.to_datetime(dataFrame.index) 

แสดงวันที่ที่เหลือในช่วงที่กำหนด:

k = pd.date_range( start="2020-10-10", end="2020-10-22").difference(dataFrame.index);

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

import pandas as pd 
# dictionary of lists 
d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 
     'Date_of_purchase': ['2020-10-10', '2020-10-12', '2020-10-17', '2020-10-16', '2020-10-19', '2020-10-22'] } 
# creating dataframe from the above dictionary of lists 
dataFrame = pd.DataFrame(d) 
print("DataFrame...\n", dataFrame) 
# Date_of_purchase set as index 
dataFrame = dataFrame.set_index('Date_of_purchase') 
# using to_datetime() to convert string to DateTime object 
dataFrame.index = pd.to_datetime(dataFrame.index) 
# remaining dates displayed as output 
print("\nDisplaying remaining dates from a range of dates...") 
k = pd.date_range(start="2020-10-10", end="2020-10-22").difference(dataFrame.index); 
print(k);

ผลลัพธ์

DataFrame... 
      Car Date_of_purchase 
0  BMW  2020-10-10 
1  Lexus  2020-10-12 
2  Audi  2020-10-17 
3 Mercedes  2020-10-16 
4 Jaguar  2020-10-19 
5 Bentley  2020-10-22 
Displaying remaining dates from a range of dates... 
DatetimeIndex(['2020-10-11', '2020-10-13', '2020-10-14', '2020-10-15', '2020-10-18', '2020-10-20', '2020-10-21'], dtype='datetime64[ns]', freq=None)