Feature request: dynamically computed columns in tables
See original GitHub issueOne feature I’d like to see in tables is the ability to create ‘derived’ columns that are evaluated on-the-fly using other columns. Take the following example:
from astropy import units as u
t = QTable()
t['spectral type'] = ['O5', 'B5', 'A5', 'F5', 'G5']
t['radius'] = [12, 3.9, 1.7, 1.3, 0.92] * u.R_sun
t['temperature'] = [45000, 15000, 8200, 6400, 5700] * u.K
t['luminosity'] = 4 * pi * t['radius'] ** 2 * sigma_sb * t['temperature'] ** 4
If I now add a row with:
t.add_row({'spectral type': 'K5',
'temperature': 4300 * u.K,
'radius': 0.72 * u.R_sun})
Then luminosity
won’t be computed for the new row. More generally, for large tables, it would be nice to have lazily evaluated new columns to avoid taking up too much memory.
Since we of course don’t want to break backward-compatibility, one way we could do this is by having a way to indicate that a column should be accessed as a reference to be used in an arithmetic operation rather than as values. This could be done for example using:
t['luminosity'] = 4 * pi * t.ref['radius'] ** 2 * sigma_sb * t.ref['temperature'] ** 4
There might be other ways to do this of course, and this is just an idea.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Discover SQL: Dynamically recomputing columns - CodeProject
Our problem is to perform a different computation for each row of a certain table, with each computation involving several columns. Computed ......
Read more >FAQ: Are Computed/Virtual columns supported in an ...
A computed/virtual column is populated dynamically and is not physically stored with the table. Computed/virtual columns are read-only.
Read more >Dynamic Columns based on values - Phil Seamark on DAX
I came across an interesting request last week from someone who wanted to dynamically control columns shown in a Power BI table visual ......
Read more >ALTER TABLE computed_column_definition (Transact-SQL)
ALTER TABLE computed_column_definition specifies the properties of a computed column that is added to a table by using ALTER TABLE.
Read more >Computed Columns | CockroachDB Docs
JSONB columns are used for storing semi-structured JSONB data. · Secondary indexes can be created on computed columns, which is especially useful when...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You don’t need to parse the syntax, since Python is doing it for you - we’d just need to define operators on columns such that e.g.
Column <op> constant
orColumn <op> Column
could return aCompositeColumn
which takes two terms (e.g. column and scalar or column and column) and an operator. The above expression can be evaluated as a nested set of suchCompositeColumn
. Maybe @maartenbreddels has some ideas here too since vaex supports lazy evaluations.Some comments on vaex:
ds['x'] = ds.y**2; ds['x'] = ds.x + 1
works in vaex, by rewriting all the virtual column expression. The old columnx
is renamed to__x
where__
denotes a ‘hidden’ column, and all other expressions get modified to refer to this new hidden column that by default does not get written out to disk.Having the whole expression system allowed me to have derivatives as well, and even better, propagation of errors/uncertainties using the whole covariance matrix (super useful for Gaia data for instance), see https://docs.vaex.io/en/latest/tutorial.html#Propagation-of-uncertainties for an example.
At this step it gets tricky because you then also need to simplify the expressions, which vaex does, not to end up with insanely large expressions. At this step you could consider relying on sympy.