python-polars

Polars convert string of digits to list

Polars convert string of digits to list Question: So i have a polars column/series that is strings of digits. s = pl.Series("a", ["111","123","101"]) s shape: (3,) Series: ‘a’ [str] [ "111" "123" "101" ] I would like to convert each string into a list of integers. I have found a working solution but i am …

Total answers: 1

Polars Series.to_numpy() does not return ndarray

Polars Series.to_numpy() does not return ndarray Question: I was trying to convert a series to a numpy array via .to_numpy() but unlike what the documentation shows i am not getting a ndarray out but a seriesview Running exactly the example in the documentation: https://pola-rs.github.io/polars/py-polars/html/reference/series/api/polars.Series.to_numpy.html s = pl.Series("a", [1, 2, 3]) arr = s.to_numpy() arr type(arr) …

Total answers: 1

How to implement rolling mean ignoring null values

How to implement rolling mean ignoring null values Question: I am trying calculate RSI indicator. For that I need rolling-mean gain and loss. I would like to calculate rolling mean ignoring null values. So mean would be calculated by sum and count on existing values. Example: window_size = 5 df = DataFrame(price_change: { 1, 2, …

Total answers: 1

not able to apply custom function in polars

not able to apply custom function in polars Question: I have a custom function previously working pretty fine in Pandas, but somehow it won’t work in the polars apply() function when I am trying to migrate to polars. below is my code. df = df.sort("time", descending=False) latest_day = df[-1,’time’] def fetch_price(self, date): # the custom …

Total answers: 1

Adding a column based on condition in Polars

Adding a column based on condition in Polars Question: Let’s say I have a Polars dataframe like so: df = pl.DataFrame({ ‘a’: [0.3, 0.7, 0.5, 0.1, 0.9] }) And now I need to add a new column where 1 or 0 is assigned depending on whether a value in column ‘a’ is greater or less …

Total answers: 1

Polars – filling nulls in a column based on values from another

Polars – filling nulls in a column based on values from another Question: I’ve been trying to learn Polars and one thing I do with Pandas I still couldn’t find out how to do is to fill nulls from a column using a dictionary to compare the values in another column. For example: import pandas …

Total answers: 1

is there a way to implement pandas wide_to_long in Polars?

is there a way to implement pandas wide_to_long in Polars? Question: I use Pandas wide to long to stack survey data and it works beautifully with regex and stub names, is this possible to do in Polars ? e.g. in Pandas – import pandas as pd df = pd.DataFrame({ ‘famid’: [1, 1, 1, 2, 2, …

Total answers: 2

Polars Conditional Replacement From Another DataFrame

Polars Conditional Replacement From Another DataFrame Question: I have two DataFrames like this. df1 = pl.DataFrame({ "col_1": np.random.rand(), "col_2": np.random.rand(), "col_3": np.random.rand() }) ┌──────────┬─────────┬──────────┐ │ col_1 ┆ col_2 ┆ col_3 │ │ — ┆ — ┆ — │ │ f64 ┆ f64 ┆ f64 │ ╞══════════╪═════════╪══════════╡ │ 0.534349 ┆ 0.84115 ┆ 0.526435 │ └──────────┴─────────┴──────────┘ df2 …

Total answers: 1

Polars Modify Many Columns Based On Value In Another Column

Polars Modify Many Columns Based On Value In Another Column Question: Say I have a DataFrame that looks like this: df = pl.DataFrame({ "id": [1, 2, 3, 4, 5], "feature_a": np.random.randint(0, 3, 5), "feature_b": np.random.randint(0, 3, 5), "label": [1, 0, 0, 1, 1], }) ┌─────┬───────────┬───────────┬───────┐ │ id ┆ feature_a ┆ feature_b ┆ label │ │ …

Total answers: 1