DataFrameOperations 類別
pandas DataFrame CRUD 操作的命名空間。
透過 client.dataframe 存取。 提供以 DataFrame 為導向的封裝器,包覆記錄層級的 CRUD 操作。
範例:
import pandas as pd
client = DataverseClient(base_url, credential)
# Query records as a DataFrame
df = client.dataframe.get("account", select=["name"], top=100)
# Create records from a DataFrame
new_df = pd.DataFrame([{"name": "Contoso"}, {"name": "Fabrikam"}])
new_df["accountid"] = client.dataframe.create("account", new_df)
# Update records
new_df["telephone1"] = ["555-0100", "555-0200"]
client.dataframe.update("account", new_df, id_column="accountid")
# Delete records
client.dataframe.delete("account", new_df["accountid"])
方法
| create |
從 pandas DataFrame 建立紀錄。 Tip 所有列都會在同一個 CreateMultiple 請求中傳送。 非常 對於大型資料幀,建議分成較小的批次以避免 請求暫停。 |
| delete |
透過傳遞 PANDAS 一串 GUID 來刪除記錄。 |
| get |
擷取記錄並返回為單一 pandas DataFrame。 當 Tip 對於大型表格,請使用頂置或篩選器來限制結果集。 |
| sql |
執行 SQL 查詢,並將結果以 pandas DataFrame 回傳。 將記錄清單委派並 sql 轉換成單一資料框架。 |
| update |
更新 pandas DataFrame 的紀錄。 DataFrame 中的每一列代表一次更新。 該欄位 Tip 所有資料列都會在單一 UpdateMultiple 請求中傳送(或 單一 PATCH 顯示一列)。 對於非常大的資料框架,考慮一下 分成較小的批次以避免請求逾時。 |
create
從 pandas DataFrame 建立紀錄。
Tip
所有列都會在同一個 CreateMultiple 請求中傳送。 非常
對於大型資料幀,建議分成較小的批次以避免
請求暫停。
create(table: str, records: DataFrame) -> Series
參數
| 名稱 | Description |
|---|---|
|
table
必要
|
表的結構名稱(例如 |
|
records
必要
|
DataFrame 中每一列都是要建立的記錄。 |
傳回
| 類型 | Description |
|---|---|
|
一系列已建立的記錄 GUID,與輸入 DataFrame 索引對齊。 |
例外狀況
| 類型 | Description |
|---|---|
|
如果 |
|
|
如果 |
範例
從資料框架建立紀錄:
import pandas as pd
df = pd.DataFrame([
{"name": "Contoso", "telephone1": "555-0100"},
{"name": "Fabrikam", "telephone1": "555-0200"},
])
df["accountid"] = client.dataframe.create("account", df)
delete
透過傳遞 PANDAS 一串 GUID 來刪除記錄。
delete(table: str, ids: Series, use_bulk_delete: bool = True) -> str | None
參數
| 名稱 | Description |
|---|---|
|
table
必要
|
表的結構名稱(例如 |
|
ids
必要
|
一系列記錄 GUID 可刪除。 |
|
use_bulk_delete
|
當(預設值)和 預設值: True
|
傳回
| 類型 | Description |
|---|---|
|
str,
|
透過 BulkDelete 刪除多筆紀錄時的 BulkDelete 工作 ID; |
例外狀況
| 類型 | Description |
|---|---|
|
如果 |
|
|
如果 |
範例
使用系列刪除紀錄:
import pandas as pd
ids = pd.Series(["guid-1", "guid-2", "guid-3"])
client.dataframe.delete("account", ids)
get
擷取記錄並返回為單一 pandas DataFrame。
當 record_id 提供時,回傳一個單列資料幀。
當 record_id 為 None 時,內部會迭代所有頁面並回傳一個整合後的資料框架。
Tip
對於大型表格,請使用頂置或篩選器來限制結果集。
get(table: str, record_id: str | None = None, select: List[str] | None = None, filter: str | None = None, orderby: List[str] | None = None, top: int | None = None, expand: List[str] | None = None, page_size: int | None = None, count: bool = False, include_annotations: str | None = None) -> DataFrame
參數
| 名稱 | Description |
|---|---|
|
table
必要
|
表的結構名稱(例如 |
|
record_id
|
可選的 GUID 用來擷取特定紀錄。 若無,則查詢多筆紀錄。 預設值: None
|
|
select
|
可選的屬性邏輯名稱清單可供檢索。 預設值: None
|
|
filter
|
可選配 OData 濾波器串。 欄位名稱必須使用精確的小寫邏輯名稱。 預設值: None
|
|
orderby
|
可選的屬性排序清單。 預設值: None
|
|
top
|
可選地設定最多回傳紀錄數量。 預設值: None
|
|
expand
|
可選的導航屬性清單可擴充(大小寫區分)。 預設值: None
|
|
page_size
|
每頁可選擇記錄數量以供分頁。 預設值: None
|
|
count
|
若 預設值: False
|
|
include_annotations
|
標頭的 OData 註解模式 預設值: None
|
傳回
| 類型 | Description |
|---|---|
|
包含所有匹配紀錄的資料框架。 當沒有紀錄相符時,會回傳一個空的資料框架。 |
例外狀況
| 類型 | Description |
|---|---|
|
如果 |
範例
擷取單一記錄作為資料框架:
df = client.dataframe.get("account", record_id=account_id, select=["name", "telephone1"])
print(df)
帶有篩選功能的查詢:
df = client.dataframe.get("account", filter="statecode eq 0", select=["name"])
print(f"Got {len(df)} active accounts")
結果大小限制:
df = client.dataframe.get("account", select=["name"], top=100)
sql
執行 SQL 查詢,並將結果以 pandas DataFrame 回傳。
將記錄清單委派並 sql 轉換成單一資料框架。
sql(sql: str) -> DataFrame
參數
| 名稱 | Description |
|---|---|
|
sql
必要
|
支援 SQL SELECT 陳述式。 |
傳回
| 類型 | Description |
|---|---|
|
包含所有結果列的資料框架。 當沒有任何資料列相符時,會回傳一個空的資料框架。 |
例外狀況
| 類型 | Description |
|---|---|
|
如果 |
範例
SQL 查詢至 DataFrame:
df = client.dataframe.sql(
"SELECT TOP 100 name, revenue FROM account "
"WHERE statecode = 0 ORDER BY revenue"
)
print(f"Got {len(df)} rows")
print(df.head())
向 DataFrame 進行彙總查詢:
df = client.dataframe.sql(
"SELECT a.name, COUNT(c.contactid) as cnt "
"FROM account a "
"JOIN contact c ON a.accountid = c.parentcustomerid "
"GROUP BY a.name"
)
update
更新 pandas DataFrame 的紀錄。
DataFrame 中的每一列代表一次更新。 該欄位 id_column 指定包含記錄 GUID。
Tip
所有資料列都會在單一 UpdateMultiple 請求中傳送(或
單一 PATCH 顯示一列)。 對於非常大的資料框架,考慮一下
分成較小的批次以避免請求逾時。
update(table: str, changes: DataFrame, id_column: str, clear_nulls: bool = False) -> None
參數
| 名稱 | Description |
|---|---|
|
table
必要
|
表的結構名稱(例如 |
|
changes
必要
|
DataFrame 中,每列包含一個記錄 GUID 及需更新的欄位。 |
|
id_column
必要
|
包含記錄 GUID 的資料框架欄位名稱。 |
|
clear_nulls
|
當(預設)時 預設值: False
|
例外狀況
| 類型 | Description |
|---|---|
|
如果 |
|
|
若 |
範例
更新每列不同值的紀錄:
import pandas as pd
df = pd.DataFrame([
{"accountid": "guid-1", "telephone1": "555-0100"},
{"accountid": "guid-2", "telephone1": "555-0200"},
])
client.dataframe.update("account", df, id_column="accountid")
將相同的變更廣播給所有紀錄:
df = pd.DataFrame({"accountid": ["guid-1", "guid-2", "guid-3"]})
df["websiteurl"] = "https://example.com"
client.dataframe.update("account", df, id_column="accountid")
透過設定 clear_nulls=True: 來清除欄位:
df = pd.DataFrame([{"accountid": "guid-1", "websiteurl": None}])
client.dataframe.update("account", df, id_column="accountid", clear_nulls=True)