-
Notifications
You must be signed in to change notification settings - Fork 14
Add client.dataframe namespace for pandas DataFrame CRUD operations #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fccabbc
Add client.dataframe namespace for pandas DataFrame CRUD operations
5523b9d
Add advanced examples: pro-dev quick start and data science risk asse…
8a26fd7
Expose primary_name_attribute in TableInfo metadata (fixes #148)
4ea8c5a
Fix @odata.bind key casing in prodev example (use lowercase nav prope…
8630e5a
Optimize get() with list accumulation, validate empty create rows, fi…
dd8b74e
Fix typing annotation, optimize weighted_pipeline aggregation, clarif…
e8f6a48
Use primary_id_attribute for updates, return typed empty DF, redact L…
17d4747
Add tests for empty-DF-with-columns and all-NaN create, prompt before…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,4 @@ Thumbs.db | |
|
|
||
| # Claude local settings | ||
| .claude/*.local.json | ||
| .claude/*.local.md | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| """ | ||
| PowerPlatform Dataverse Client - DataFrame Operations Walkthrough | ||
|
|
||
| This example demonstrates how to use the pandas DataFrame extension methods | ||
| for CRUD operations with Microsoft Dataverse. | ||
|
|
||
| Prerequisites: | ||
| pip install PowerPlatform-Dataverse-Client | ||
| pip install azure-identity | ||
| """ | ||
|
|
||
| import sys | ||
| import uuid | ||
|
|
||
| import pandas as pd | ||
| from azure.identity import InteractiveBrowserCredential | ||
|
saurabhrb marked this conversation as resolved.
|
||
|
|
||
| from PowerPlatform.Dataverse.client import DataverseClient | ||
|
|
||
|
|
||
| def main(): | ||
| # -- Setup & Authentication ------------------------------------ | ||
| base_url = input("Enter Dataverse org URL (e.g. https://yourorg.crm.dynamics.com): ").strip() | ||
| if not base_url: | ||
| print("[ERR] No URL entered; exiting.") | ||
| sys.exit(1) | ||
| base_url = base_url.rstrip("/") | ||
|
|
||
| print("[INFO] Authenticating via browser...") | ||
| credential = InteractiveBrowserCredential() | ||
|
|
||
| with DataverseClient(base_url, credential) as client: | ||
| _run_walkthrough(client) | ||
|
|
||
|
|
||
| def _run_walkthrough(client): | ||
| table = input("Enter table schema name to use [default: account]: ").strip() or "account" | ||
| print(f"[INFO] Using table: {table}") | ||
|
|
||
| # Unique tag to isolate test records from existing data | ||
| tag = uuid.uuid4().hex[:8] | ||
| test_filter = f"contains(name,'{tag}')" | ||
| print(f"[INFO] Using tag '{tag}' to identify test records") | ||
|
|
||
| select_cols = ["name", "telephone1", "websiteurl", "lastonholdtime"] | ||
|
|
||
| # -- 1. Create records from a DataFrame ------------------------ | ||
| print("\n" + "-" * 60) | ||
| print("1. Create records from a DataFrame") | ||
| print("-" * 60) | ||
|
|
||
| new_accounts = pd.DataFrame( | ||
| [ | ||
| { | ||
| "name": f"Contoso_{tag}", | ||
| "telephone1": "555-0100", | ||
| "websiteurl": "https://contoso.com", | ||
| "lastonholdtime": pd.Timestamp("2024-06-15 10:30:00"), | ||
| }, | ||
| {"name": f"Fabrikam_{tag}", "telephone1": "555-0200", "websiteurl": None, "lastonholdtime": None}, | ||
| { | ||
| "name": f"Northwind_{tag}", | ||
| "telephone1": None, | ||
| "websiteurl": "https://northwind.com", | ||
| "lastonholdtime": pd.Timestamp("2024-12-01 08:00:00"), | ||
| }, | ||
| ] | ||
| ) | ||
| print(f" Input DataFrame:\n{new_accounts.to_string(index=False)}\n") | ||
|
|
||
| # create_dataframe returns a Series of GUIDs aligned with the input rows | ||
| new_accounts["accountid"] = client.dataframe.create(table, new_accounts) | ||
| print(f"[OK] Created {len(new_accounts)} records") | ||
| print(f" IDs: {new_accounts['accountid'].tolist()}") | ||
|
|
||
|
saurabhrb marked this conversation as resolved.
|
||
| # -- 2. Query records as a DataFrame ------------------------- | ||
| print("\n" + "-" * 60) | ||
| print("2. Query records as a DataFrame") | ||
| print("-" * 60) | ||
|
|
||
| df_all = client.dataframe.get(table, select=select_cols, filter=test_filter) | ||
| print(f"[OK] Got {len(df_all)} records in one DataFrame") | ||
| print(f" Columns: {list(df_all.columns)}") | ||
| print(f"{df_all.to_string(index=False)}") | ||
|
|
||
| # -- 3. Limit results with top ------------------------------ | ||
| print("\n" + "-" * 60) | ||
| print("3. Limit results with top") | ||
| print("-" * 60) | ||
|
|
||
| df_top2 = client.dataframe.get(table, select=select_cols, filter=test_filter, top=2) | ||
| print(f"[OK] Got {len(df_top2)} records with top=2") | ||
| print(f"{df_top2.to_string(index=False)}") | ||
|
|
||
| # -- 4. Fetch a single record by ID ---------------------------- | ||
| print("\n" + "-" * 60) | ||
| print("4. Fetch a single record by ID") | ||
| print("-" * 60) | ||
|
|
||
| first_id = new_accounts["accountid"].iloc[0] | ||
| print(f" Fetching record {first_id}...") | ||
| single = client.dataframe.get(table, record_id=first_id, select=select_cols) | ||
| print(f"[OK] Single record DataFrame:\n{single.to_string(index=False)}") | ||
|
|
||
| # -- 5. Update records from a DataFrame ------------------------ | ||
| print("\n" + "-" * 60) | ||
| print("5. Update records with different values per row") | ||
| print("-" * 60) | ||
|
|
||
| new_accounts["telephone1"] = ["555-1100", "555-1200", "555-1300"] | ||
| print(f" New telephone numbers: {new_accounts['telephone1'].tolist()}") | ||
| client.dataframe.update(table, new_accounts[["accountid", "telephone1"]], id_column="accountid") | ||
| print("[OK] Updated 3 records") | ||
|
|
||
| # Verify the updates | ||
| verified = client.dataframe.get(table, select=select_cols, filter=test_filter) | ||
| print(f" Verified:\n{verified.to_string(index=False)}") | ||
|
|
||
| # -- 6. Broadcast update (same value to all records) ----------- | ||
| print("\n" + "-" * 60) | ||
| print("6. Broadcast update (same value to all records)") | ||
| print("-" * 60) | ||
|
|
||
| broadcast_df = new_accounts[["accountid"]].copy() | ||
| broadcast_df["websiteurl"] = "https://updated.example.com" | ||
| print(f" Setting websiteurl to 'https://updated.example.com' for all {len(broadcast_df)} records") | ||
| client.dataframe.update(table, broadcast_df, id_column="accountid") | ||
| print("[OK] Broadcast update complete") | ||
|
|
||
| # Verify all records have the same websiteurl | ||
| verified = client.dataframe.get(table, select=select_cols, filter=test_filter) | ||
| print(f" Verified:\n{verified.to_string(index=False)}") | ||
|
|
||
| # Default: NaN/None fields are skipped (not overridden on server) | ||
| print("\n Updating with NaN values (default: clear_nulls=False, fields should stay unchanged)...") | ||
| sparse_df = pd.DataFrame( | ||
| [ | ||
| {"accountid": new_accounts["accountid"].iloc[0], "telephone1": "555-9999", "websiteurl": None}, | ||
| ] | ||
| ) | ||
| client.dataframe.update(table, sparse_df, id_column="accountid") | ||
| verified = client.dataframe.get(table, select=select_cols, filter=test_filter) | ||
| print(f" Verified (Contoso telephone1 updated, websiteurl unchanged):\n{verified.to_string(index=False)}") | ||
|
|
||
| # Opt-in: clear_nulls=True sends None as null to clear the field | ||
| print("\n Clearing websiteurl for Contoso with clear_nulls=True...") | ||
| clear_df = pd.DataFrame([{"accountid": new_accounts["accountid"].iloc[0], "websiteurl": None}]) | ||
| client.dataframe.update(table, clear_df, id_column="accountid", clear_nulls=True) | ||
| verified = client.dataframe.get(table, select=select_cols, filter=test_filter) | ||
| print(f" Verified (Contoso websiteurl should be empty):\n{verified.to_string(index=False)}") | ||
|
|
||
| # -- 7. Delete records by passing a Series of GUIDs ------------ | ||
| print("\n" + "-" * 60) | ||
| print("7. Delete records by passing a Series of GUIDs") | ||
| print("-" * 60) | ||
|
|
||
| print(f" Deleting {len(new_accounts)} records...") | ||
| client.dataframe.delete(table, new_accounts["accountid"], use_bulk_delete=False) | ||
| print(f"[OK] Deleted {len(new_accounts)} records") | ||
|
|
||
| # Verify deletions - filter for our tagged records should return 0 | ||
| remaining = client.dataframe.get(table, select=select_cols, filter=test_filter) | ||
| print(f" Verified: {len(remaining)} test records remaining (expected 0)") | ||
|
|
||
| print("\n" + "=" * 60) | ||
| print("[OK] DataFrame operations walkthrough complete!") | ||
| print("=" * 60) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.