|
|
|
|
|
by brahbrah
1167 days ago
|
|
(Taken from an old comment of mine) If you were to say “pandas in long format only” then yes that would be correct, but the power of pandas comes in its ability to work in a long relational or wide ndarray style. Pandas was originally written to replace excel in financial/econometric modeling, not as a replacement for sql. Models written solely in the long relational style are near unmaintainable for constantly evolving models with hundreds of data sources and thousands of interactions being developed and tuned by teams of analysts and engineers.
For example, this is how some basic operations would look. Bump prices in March 2023 up 10%: # pandas
prices_df.loc['2023-03'] *= 1.1
# polars
polars_df.with_column(
pl.when(pl.col('timestamp').is_between(
datetime('2023-03-01'),
datetime('2023-03-31'),
include_bounds=True
)).then(pl.col('val') * 1.1)
.otherwise(pl.col('val'))
.alias('val')
)
Add expected temperature offsets to base temperature forecast at the state county level: # pandas
temp_df + offset_df
# polars
(
temp_df
.join(offset_df, on=['state', 'county', 'timestamp'], suffix='_r')
.with_column(
( pl.col('val') + pl.col('val_r')).alias('val')
)
.select(['state', 'county', 'timestamp', 'val'])
)
Now imagine thousands of such operations, and you can see the necessity of pandas in models like this. |
|
I typical do the type of column operation in your example only on subsets of data, and typically I do it in SQL using DuckDB. Interop between Polars and DuckDB is virtually zero cost so I seamlessly move between the two. And to be honest I don’t remember the last time I needed to do this but that’s just the nature of my work and not a generalized statement.
But yes if you are still in a world where you need to perform Excel like operations then I agree.