Comments (2)
Thanks for the issue - to expedite resolution could you show an example of what you'd like to do with expected output please?
from polars.
Yes I can do that :)
Personally I am using this to add a column to a pl.DataFrame
where I have a custom function _annual_vol
that needs to compute the rolling volatility for every month.
So here is a use case for an internal function rolling_std()
:
import numpy as np
import datetime as dt
trading_periods = (252,)
_column_name_returns: str = "log_returns"
dates = pl.Series(
[
dt.datetime(2021, 1, 29),
dt.datetime(2021, 1, 30),
dt.datetime(2021, 1, 31),
dt.datetime(2021, 2, 1),
dt.datetime(2021, 2, 2),
dt.datetime(2021, 2, 3),
dt.datetime(2021, 2, 4),
dt.datetime(2021, 2, 5),
dt.datetime(2021, 2, 8),
dt.datetime(2021, 2, 9),
]
)
data = pl.DataFrame(
{
"log_returns": [2, 4, 6, 5, 3, 7, 2, 8, 4, 5],
"date": dates
}
)
vol = data.set_sorted("date").select(
pl.col(_column_name_returns).rolling_std(
window_size=3, min_periods=1, by="date"
)
* np.sqrt(trading_periods)
)
Here is a similar function, but I cannot use window_size="2d"
to specify a width of 2 days. I have to use an integer. When the dataset becomes larger and I would like to use "1m"
I cannot set it to just 21, becuase that can change from month to month.
import numpy as np
import datetime as dt
def annual_vol(data: pl.Series, trading_periods: int = 252) -> pl.Series:
return (trading_periods * data.mean()) ** 0.5
trading_periods = (252,)
_column_name_returns: str = "log_returns"
dates = pl.Series(
[
dt.datetime(2021, 1, 29),
dt.datetime(2021, 1, 30),
dt.datetime(2021, 1, 31),
dt.datetime(2021, 2, 1),
dt.datetime(2021, 2, 2),
dt.datetime(2021, 2, 3),
dt.datetime(2021, 2, 4),
dt.datetime(2021, 2, 5),
dt.datetime(2021, 2, 8),
dt.datetime(2021, 2, 9),
]
)
data = pl.DataFrame(
{"log_returns": [2, 4, 6, 5, 3, 7, 2, 8, 4, 5], "date": dates}
)
vol = data.set_sorted("date").select(
pl.col(_column_name_returns).rolling_map(annual_vol,
window_size="2d", min_periods=1
)
* np.sqrt(trading_periods)
)
You can get similar functionaility in Polars
using the .rolling()
& .map_elements()
functions:
vol = data.set_sorted("date").rolling(index_column="date", period="2d").agg(
pl.col("log_returns").map_elements(annual_vol)
)
BUT, I think that this should be integrated into the .rolling_map
function, since it seems redundant to have both avialable and one lacking a feature of the other?
There should be a clarification on using .rolling()
that a timedelta parameter for period
will only compute consecutive date agregations. If there is a weekend skipped and the date is not avail. in the data, whn using the rolling().agg()
logic, the date prior is not included in the calculation. This should be included, or let the user decide.
Basically, rolling_map()
should copy the functionality of rolling_*
polars functions and allow window_size
to be timedelta
or str
. :)
from polars.
Related Issues (20)
- exception thrown if converting arrow Table with struct and dictionary columns to polar dataframe
- converting pandas to Polars drops column if its name, when converted to string, matches another column's name
- pl.format should be clear it will return null when one of the arguments is null
- Off-by-one error when casting to Decimal with set precision
- Importing pyarrow after polars causes `SIGSEGV` HOT 4
- Polars assumes microseconds instead of reading numpy timedelta units HOT 1
- Cannot create Array column containing large u64 value
- Multipling a Decimal by Int returns Int type HOT 2
- Split out `Expr.top_k` from `Expr.top_k_by`
- `pl.Datetime` `time_zone` parameter has no type or value check HOT 6
- Cast from `pl.Date` to `pl.Datetime` silently returns incorrect value when new dtype cannot hold value HOT 2
- exception thrown if converting chunked arrow Table with struct and dictionary columns to polar Dataframe
- Panic when constructing Series with dtype `Duration('ms')` with large `timedelta` objects
- Can the separator of the read csv function support regular splitting? HOT 5
- Casting float to Decimal fails silently HOT 2
- Use parquet statistics when collecting column statistics from scanned parquet HOT 2
- Excessive Memory Consumption During Rolling Operations on Large DataFrames
- write_database() - Insert many rows with sql server using fast_executemany HOT 3
- fill_null doesn't support expr HOT 6
- `dt.total_nanoseconds` and `dt.total_microseconds` may overflow silently
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from polars.