Skip to content

np.log() gives unexpected output when used within .rolling() window and calculated inf is present #22874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 tasks done
Jfbarr opened this issue May 22, 2025 · 0 comments · Fixed by #22913
Closed
2 tasks done
Assignees
Labels
bug Something isn't working python Related to Python Polars

Comments

@Jfbarr
Copy link

Jfbarr commented May 22, 2025

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

import polars as pl
import numpy as np

df = pl.DataFrame(data={'A':[0, 10, 11, 12, 0, 10, 11, 12],  'counter':[1,2,3,4, 5, 6, 7, 8]})

(
    df
    .rolling(index_column='counter', period='2i')
    .agg(
        pl.col('A'),
        (np.log( pl.col('A'))).alias('log_numpy'),
        (pl.col('A')).log().alias('log_polars'),
        (np.log( pl.col('A'))).mean().alias('mean_numpy'),
        (( pl.col('A'))).log().mean().alias('mean_polars'),
        )
)

Log output

shape: (8, 6)
┌─────────┬───────────┬──────────────────────┬──────────────────────┬────────────┬─────────────┐
│ counter ┆ A         ┆ log_numpy            ┆ log_polars           ┆ mean_numpy ┆ mean_polars │
│ ---     ┆ ---       ┆ ---                  ┆ ---                  ┆ ---        ┆ ---         │
│ i64     ┆ list[i64] ┆ list[f64]            ┆ list[f64]            ┆ f64        ┆ f64         │
╞═════════╪═══════════╪══════════════════════╪══════════════════════╪════════════╪═════════════╡
│ 1       ┆ [0]       ┆ [-inf]               ┆ [-inf]               ┆ -inf       ┆ -inf        │
│ 2       ┆ [0, 10]   ┆ [-inf, 2.302585]     ┆ [-inf, 2.302585]     ┆ -inf       ┆ -inf        │
│ 3       ┆ [10, 11]  ┆ [2.302585, 2.397895] ┆ [2.302585, 2.397895] ┆ 2.35024    ┆ 2.35024     │
│ 4       ┆ [11, 12]  ┆ [2.397895, 2.484907] ┆ [2.397895, 2.484907] ┆ NaN        ┆ 2.441401    │
│ 5       ┆ [12, 0]   ┆ [2.484907, -inf]     ┆ [2.484907, -inf]     ┆ NaN        ┆ -inf        │
│ 6       ┆ [0, 10]   ┆ [-inf, 2.302585]     ┆ [-inf, 2.302585]     ┆ NaN        ┆ NaN         │
│ 7       ┆ [10, 11]  ┆ [2.302585, 2.397895] ┆ [2.302585, 2.397895] ┆ 2.35024    ┆ 2.35024     │
│ 8       ┆ [11, 12]  ┆ [2.397895, 2.484907] ┆ [2.397895, 2.484907] ┆ NaN        ┆ 2.441401    │
└─────────┴───────────┴──────────────────────┴──────────────────────┴────────────┴─────────────┘

Issue description

Applying np.log() elementwise to a list of values within a rolling window results in weird behaviour if np.inf is calculated within a window. Applying np.log() or calling .log() on the column yields the appropriate elementwise log values (0 is mapped to -inf appropriately). Calling .mean() on the numpy constructed list yields the correct value once the -inf drops out of the window, but then produces NaN for every row afterwards (until the next -inf drops out of the window, whereby a correct value is again produced before continuing with NaNs). Calling .mean() on the list of log-values constructed by calling .log() produces expected behaviour.

Expected behavior

Using np.log() and calling .log() should have the same behaviour within the .rolling() window.

Installed versions

--------Version info---------
Polars:              1.30.0
Index type:          UInt32
Platform:            Linux-5.14.0-427.42.1.el9_4.x86_64-x86_64-with-glibc2.34
Python:              3.10.8 | packaged by conda-forge | (main, Nov 22 2022, 08:26:04) [GCC 10.4.0]
LTS CPU:             False

----Optional dependencies----
Azure CLI            <not installed>
adbc_driver_manager  <not installed>
altair               5.5.0
azure.identity       <not installed>
boto3                1.34.159
cloudpickle          3.0.0
connectorx           <not installed>
deltalake            <not installed>
fastexcel            0.14.0
fsspec               2025.3.2
gevent               <not installed>
google.auth          2.40.1
great_tables         <not installed>
matplotlib           3.10.3
numpy                1.23.5
openpyxl             3.1.5
pandas               1.5.3
polars_cloud         <not installed>
pyarrow              20.0.0
pydantic             2.10.2
pyiceberg            <not installed>
sqlalchemy           1.4.4
torch                2.5.1+cu124
xlsx2csv             0.8.4
xlsxwriter           <not installed>
@Jfbarr Jfbarr added bug Something isn't working python Related to Python Polars needs triage Awaiting prioritization by a maintainer labels May 22, 2025
@deanm0000 deanm0000 self-assigned this May 23, 2025
@deanm0000 deanm0000 removed the needs triage Awaiting prioritization by a maintainer label May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Related to Python Polars
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants