-
Notifications
You must be signed in to change notification settings - Fork 90
docs: DOC-1066: Vectorization guides #7499
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
Open
margaretkennedy
wants to merge
4
commits into
main
Choose a base branch
from
DOC-1066-looping
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
312 changes: 312 additions & 0 deletions
312
docs/python/getting-started/crash-course/vectorization-vs-loops.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,312 @@ | ||
| --- | ||
| title: Recipes, not loops! | ||
| --- | ||
|
|
||
| If you're coming from pandas, traditional Python, or other data processing tools, you're likely accustomed to writing loops to transform data. **Stop!** Deephaven works fundamentally differently, and understanding this difference early will save you countless hours of frustration and help you write better, faster code. | ||
|
|
||
| ## The fundamental paradigm shift: recipes, not loops | ||
|
|
||
| ### How you might be thinking | ||
|
|
||
| In pandas or traditional Python, you tell the computer **exactly how** to process each row: | ||
|
|
||
| ```python skip-test | ||
| import pandas as pd | ||
|
|
||
| # Pandas approach: explicit loop over rows | ||
| time_index = pd.date_range(start="2025-01-01 00:00:00", periods=5, freq="h") | ||
| df = pd.DataFrame( | ||
| { | ||
| "time": time_index, | ||
| "value": range(5), | ||
| } | ||
| ) | ||
|
|
||
| # Converting values with a list comprehension - WRONG for Deephaven! | ||
| df["value_squared"] = [v * v for v in df["value"]] | ||
| ``` | ||
|
|
||
| This list comprehension loops over every row, processes it, and builds a new list. You're giving **step-by-step instructions** for how to process the data. | ||
|
|
||
| ### How to think in Deephaven | ||
|
|
||
| In Deephaven, you specify **what** you want, not **how** to compute it. You write a **recipe** that describes the transformation, and the Deephaven engine figures out the optimal way to execute it: | ||
|
|
||
| ```python order=t1,t2,t3 test-set=recipe-example | ||
| from deephaven import empty_table | ||
|
|
||
| # Create a table with 5 rows of timestamps | ||
| t1 = empty_table(5).update([ | ||
| "Timestamp = now() + i * SECOND", | ||
| "TsEpochNs = epochNanos(Timestamp)" | ||
| ]) | ||
|
|
||
| # Add a column using a Deephaven recipe - NO LOOP! | ||
| t2 = t1.update("TS2 = epochNanosToInstant(TsEpochNs)") | ||
|
|
||
| # Do more time operations - still no loops | ||
| t3 = t2.update( | ||
| [ | ||
| "TS3 = epochNanosToInstant(TsEpochNs + 2*SECOND)", | ||
| "TS4 = Timestamp + 'PT2s'", | ||
| "D3 = TS3-Timestamp", | ||
| "D4 = TS4-Timestamp", | ||
| ] | ||
| ) | ||
| ``` | ||
|
|
||
| Notice: | ||
|
|
||
| - **No loops** - You write `.update("TS2 = epochNanosToInstant(TsEpochNs)")`. | ||
| - You specify **what** to compute, not **how** to iterate. | ||
| - The engine applies this recipe to all rows automatically. | ||
|
|
||
| ## Why this matters | ||
|
|
||
| ### For static data | ||
|
|
||
| Even for static, one-time calculations, the recipe approach has advantages: | ||
|
|
||
| 1. **Clearer code** - Declarative recipes are easier to read than imperative loops. | ||
| 2. **Faster execution** - The engine can optimize vectorized operations. | ||
| 3. **Less error-prone** - No manual loop management or index tracking. | ||
|
|
||
| ### For real-time data | ||
|
|
||
| This is the critical difference. **Loops execute once and stop. Recipes update automatically.** | ||
|
|
||
| ```python ticking-table order=null test-set=ticking-demo | ||
| from deephaven import time_table | ||
|
|
||
| # This table adds a new row every second | ||
| source = time_table("PT1s").update(["X = i", "XSquared = X * X", "XCubed = X * X * X"]) | ||
| ``` | ||
|
|
||
| Watch what happens: | ||
|
|
||
| - The table **keeps updating** - new rows appear every second. | ||
| - Your **recipe runs automatically** on every new row. | ||
| - You wrote it **once**, but it executes **forever**. | ||
|
|
||
| With a loop approach: | ||
|
|
||
| ```python skip-test | ||
| # This would only work ONCE and never update! | ||
| for row in source.iter_tuple(): | ||
| x = row.X | ||
| x_squared = x * x # ❌ Where would this even go? | ||
| ``` | ||
|
|
||
| ## The recipe paradigm explained | ||
|
|
||
| ### Recipes are specifications, not instructions | ||
|
|
||
| When you write: | ||
|
|
||
| ```python skip-test | ||
| t.update("Y = X * 2") | ||
| ``` | ||
|
|
||
| You're **not** saying: | ||
|
|
||
| - "Start at row 0" | ||
| - "Read X from row 0" | ||
| - "Multiply by 2" | ||
| - "Store in Y at row 0" | ||
| - "Go to row 1" | ||
| - "Repeat..." | ||
|
|
||
| You're saying: | ||
|
|
||
| - "For every row, Y should equal X times 2" | ||
|
|
||
| The engine decides: | ||
|
|
||
| - How to chunk the data for optimal performance. | ||
| - Whether to parallelize the operation. | ||
| - How to handle updates efficiently. | ||
| - What rows need recomputation when data changes. | ||
|
|
||
| ### The engine is smart about updates | ||
|
|
||
| 1. **Tracks dependencies** - It knows that `Y` depends on `X`. | ||
| 2. **Computes incrementally** - Only new or changed rows are processed. | ||
| 3. **Updates automatically** - Results update without you doing anything. | ||
|
|
||
| This is fundamentally impossible with loops! | ||
|
|
||
| ## Bridging pandas and Deephaven | ||
|
|
||
| Many users need to work with both pandas and Deephaven. Here's how to think about the transition: | ||
|
|
||
| ```python order=df,t1,m,t2,t3,df2 test-set=pandas-bridge | ||
| import pandas as pd | ||
| import deephaven.pandas as dhpd | ||
|
|
||
| # Create data in pandas | ||
| time_index = pd.date_range(start="2025-01-01 00:00:00", periods=5, freq="h") | ||
| df = pd.DataFrame( | ||
| { | ||
| "time": time_index, | ||
| "value": range(5), | ||
| } | ||
| ) | ||
|
|
||
| print("Original pandas DataFrame:") | ||
| print(df) | ||
|
|
||
| # Convert to Deephaven | ||
| t1 = dhpd.to_table(df) | ||
|
|
||
| # Check the column types | ||
| m = t1.meta_table | ||
|
|
||
| # Now use Deephaven recipes (NOT loops!) | ||
| t2 = t1.update("TsEpochNs = epochNanos(time)") | ||
|
|
||
| # More time operations using recipes | ||
| t3 = t2.update( | ||
| [ | ||
| "TS3 = epochNanosToInstant(TsEpochNs + 2*SECOND)", | ||
| "TS4 = time + 'PT2s'", | ||
| "D3 = TS3-time", | ||
| "D4 = TS4-time", | ||
| ] | ||
| ) | ||
|
|
||
| # Convert back to pandas if needed | ||
| df2 = dhpd.to_pandas(t3) | ||
| print("Result DataFrame:") | ||
| print(df2) | ||
| ``` | ||
|
|
||
| **Key principle:** Once you're in Deephaven, think in recipes. Save loops for when you convert back to pandas. | ||
|
|
||
| ## When loops ARE appropriate | ||
|
|
||
| There are valid uses for loops in Deephaven: | ||
|
|
||
| ### ✅ Extracting data from Deephaven | ||
|
|
||
| ```python order=source test-set=valid-loops | ||
| from deephaven import empty_table | ||
|
|
||
| source = empty_table(5).update(["X = i", "Y = X * 2"]) | ||
|
|
||
| # This is fine - you're extracting, not transforming | ||
| for row in source.iter_tuple(): | ||
| print(f"X={row.X}, Y={row.Y}") | ||
| ``` | ||
|
|
||
| See the [table iteration guide](../../how-to-guides/iterate-table-data.md) for details. | ||
|
|
||
| ### ✅ Control flow in your Python code | ||
|
|
||
| ```python skip-test | ||
| # Creating multiple similar tables - fine! | ||
| tables = [] | ||
| for symbol in ["AAPL", "GOOGL", "MSFT"]: | ||
| t = source.where(f"Symbol = `{symbol}`") | ||
| tables.append(t) | ||
| ``` | ||
|
|
||
| ### ❌ Transforming table columns | ||
|
|
||
| ```python skip-test | ||
| # NEVER do this! | ||
| result_data = [] | ||
| for row in source.iter_tuple(): | ||
| result_data.append(row.X * 2) # ❌ Use .update() instead! | ||
| ``` | ||
|
|
||
| ## Common patterns: Wrong vs Right | ||
|
|
||
| ### Pattern: Create a column from another column | ||
|
|
||
| ❌ **Wrong** (loop approach): | ||
|
|
||
| ```python skip-test | ||
| # Don't do this! | ||
| values = [] | ||
| for row in source.iter_tuple(): | ||
| values.append(row.X * row.X) | ||
| # Now what? How do you get this back into a table? | ||
| ``` | ||
|
|
||
| ✅ **Right** (recipe approach): | ||
|
|
||
| ```python order=result test-set=pattern1 | ||
| from deephaven import empty_table | ||
|
|
||
| result = empty_table(10).update(["X = i", "XSquared = X * X"]) | ||
| ``` | ||
|
|
||
| ### Pattern: Conditional logic | ||
|
|
||
| ❌ **Wrong** (loop approach): | ||
|
|
||
| ```python skip-test | ||
| # Don't do this! | ||
| results = [] | ||
| for row in source.iter_tuple(): | ||
| if row.X % 2 == 0: | ||
| results.append("Even") | ||
| else: | ||
| results.append("Odd") | ||
| ``` | ||
|
|
||
| ✅ **Right** (recipe with ternary operator): | ||
|
|
||
| ```python order=result test-set=pattern2 | ||
| from deephaven import empty_table | ||
|
|
||
| result = empty_table(10).update(["X = i", "Label = (X % 2 == 0) ? `Even` : `Odd`"]) | ||
| ``` | ||
|
|
||
| ### Pattern: Running calculations | ||
|
|
||
| ❌ **Wrong** (loop with accumulator): | ||
|
|
||
| ```python skip-test | ||
| # Don't do this! | ||
| running_sum = 0 | ||
| results = [] | ||
| for row in source.iter_tuple(): | ||
| running_sum += row.X | ||
| results.append(running_sum) | ||
| ``` | ||
|
|
||
| ✅ **Right** (use update_by): | ||
|
|
||
| ```python order=result test-set=pattern3 | ||
| from deephaven import empty_table | ||
| from deephaven.updateby import cum_sum | ||
|
|
||
| result = empty_table(10).update("X = i").update_by(cum_sum("SumX = X")) | ||
| ``` | ||
|
|
||
| ## Quick reference: Migration guide | ||
|
|
||
| | pandas/Python Pattern | Deephaven Recipe | | ||
| | ------------------------------ | ----------------------------------- | | ||
| | `df.apply(func)` | `.update("Y = func(X)")` | | ||
| | `for row in df.iterrows():` | ❌ Don't! Use `.update()` | | ||
| | `df['Y'] = df['X'] * 2` | `.update("Y = X * 2")` | | ||
| | `df[df['X'] > 5]` | `.where("X > 5")` | | ||
| | `df.rolling(window=10).mean()` | `.update_by(rolling_avg_tick(...))` | | ||
| | `df.groupby('G').sum()` | `.sum_by("G")` | | ||
|
|
||
| ## Next steps | ||
|
|
||
| - Read [Think like a ninja](../../conceptual/ninja.md#looping-dont-do-it) for more examples. | ||
| - Learn about [table operations](./table-ops.md) to see recipes in action. | ||
| - Understand [the query engine](../../conceptual/vectorization-and-recipes.md) for technical details. | ||
| - See [update_by operations](../../how-to-guides/rolling-aggregations.md) for powerful recipes. | ||
|
|
||
| ## Related documentation | ||
|
|
||
| - [Think like a Deephaven ninja](../../conceptual/ninja.md) | ||
| - [Table operations](./table-ops.md) | ||
| - [Query strings](./query-strings.md) | ||
| - [Table iteration (for extraction only!)](../../how-to-guides/iterate-table-data.md) | ||
| - [update_by for rolling calculations](../../how-to-guides/rolling-aggregations.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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used a plain title until I saw Chip's note in the ticket. I'll update the tutorial card depending on what we decide.