python-polars

Multi filter by 2 columns and display some best results with Polars

Multi filter by 2 columns and display some best results with Polars Question: I have df for my work with 3 main columns: cid1, cid2, cid3, and more columns cid4, cid5, etc. cid1 and cid2 is int, another columns is float. ┌──────┬──────┬──────┬──────┬──────┬──────────┐ │ cid1 ┆ cid2 ┆ cid3 ┆ cid4 ┆ cid5 ┆ cid6 │ …

Total answers: 2

Faster way to add a row in between a time series Python

Faster way to add a row in between a time series Python Question: I have a dataframe that has one of the columns as ‘date’. It contains datetime value in the format 2020-11-04 09:15:00+05:30 for 45 days. The data for a day starts at 9:15:00 and ends at 18:30:00. Apart from the date, there is …

Total answers: 2

python polars: df partition with pivot and concat

python polars: df partition with pivot and concat Question: my goal was to groupby/partition by one column (a below), create a string identifier (b and c columns) then use this b_c identifier as a name for a column in a pivoted data frame. Code below works OK as far as I can tell, but the …

Total answers: 1

Is there a way to utilize polars mapping to make this code more efficient?

Is there a way to utilize polars mapping to make this code more efficient? Question: I have some polars code that functionally can do what I want, but I feel it is an inefficient implementation at best. I feel that there must be some way to achieve the same result with .map(), but I can’t …

Total answers: 1

AttributeError: 'Expr' object has no attribute 'map_dict' polars Document example error

AttributeError: 'Expr' object has no attribute 'map_dict' polars Document example error Question: Taking the example straight from the docs: country_code_dict = { "CA": "Canada", "DE": "Germany", "FR": "France", None: "Not specified", } df = pl.DataFrame( { "country_code": ["FR", None, "ES", "DE"], } ).with_row_count() df.with_columns( pl.col("country_code") .map_dict(country_code_dict, default="unknown") .alias("remapped") ) here: https://pola-rs.github.io/polars/py-polars/html/reference/expressions/api/polars.Expr.map_dict.html Gives the error: AttributeError: …

Total answers: 2

Python Polars Rolling Count

Python Polars Rolling Count Question: There are some known rolling functions in polars, namely rolling_mean(), rolling_apply() and rolling_max(). However, if I would like to get a count on the number of occurrence of a value in each window, how should that be done? Let’s say we now have a LazyFrame: df = pl.LazyFrame({"Date": ["2023-01-01", "2023-01-02", …

Total answers: 2

Python Polars Window Function With Literal Type

Python Polars Window Function With Literal Type Question: Say I have a DataFrame with an id column like this: ┌─────┐ │ id │ │ — │ │ i64 │ ╞═════╡ │ 1 │ │ 1 │ │ 1 │ │ 2 │ │ 2 │ │ 3 │ │ 3 │ └─────┘ I want to …

Total answers: 1

polars – Fill null over Groups

polars – Fill null over Groups Question: I am trying to fill null timestamps over groups, my dataframe looks like this start stop group 0 2021-12-08 06:40:53.734941+01:00 2022-05-16 10:16:18.717146+02:00 1 1 2021-12-08 06:40:55.191598+01:00 null 1 2 2021-12-08 10:39:12.421402+01:00 2022-05-16 10:16:19.816922+02:00 2 3 2021-12-08 10:39:12.634873+01:00 2022-05-16 10:16:19.817304+02:00 1 4 2021-12-08 10:49:47.392815+01:00 2022-05-16 10:16:20.178050+02:00 5 The stop …

Total answers: 1

how to limit the display width in polars so that wide dataframes are printed in a legible way?

how to limit the display width in polars so that wide dataframes are printed in a legible way? Question: Consider the following example pd.set_option(‘display.width’, 50) pl.DataFrame(data = np.random.randint(0,20, size = (10, 42)), columns = list(‘abcdefghijklmnopqrstuvwxyz123456789ABCDEFG’)).to_pandas() You can see how nicely the columns are formatted, breaking a line after column k so that the full dataframe …

Total answers: 1