Skip to content
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

single-element rolling_std returns 0 (rather than null) #11140

Closed
2 tasks done
MarcoGorelli opened this issue Sep 15, 2023 · 4 comments · Fixed by #11750
Closed
2 tasks done

single-element rolling_std returns 0 (rather than null) #11140

MarcoGorelli opened this issue Sep 15, 2023 · 4 comments · Fixed by #11750
Assignees
Labels
A-temporal Area: date/time functionality bug Something isn't working P-low Priority: low python Related to Python Polars

Comments

@MarcoGorelli
Copy link
Collaborator

MarcoGorelli commented Sep 15, 2023

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

In [9]: df = pl.DataFrame({'ts': [datetime(2020, 1, 1)], 'val': [1]}).sort('ts')

In [10]: df.select(pl.col('val').rolling_std('1d', by='ts', closed='right'))
Out[10]:
shape: (1, 1)
┌─────┐
│ val │
│ --- │
│ f64 │
╞═════╡
│ 0.0 │
└─────┘

In [11]: df.select(pl.col('val').std())
Out[11]:
shape: (1, 1)
┌──────┐
│ val  │
│ ---  │
│ f64  │
╞══════╡
│ null │
└──────┘

Log output

No response

Issue description

The first example above returns 0, the second None

Expected behavior

Both should return None?

Noticed this while trying out #11134

Installed versions


INSTALLED VERSIONS
------------------
commit           : 0f437949513225922d851e9581723d82120684a6
python           : 3.10.6.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.10.102.1-microsoft-standard-WSL2
Version          : #1 SMP Wed Mar 2 00:30:59 UTC 2022
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_GB.UTF-8
LOCALE           : en_GB.UTF-8

pandas           : 2.0.3
numpy            : 1.25.1
pytz             : 2023.3
dateutil         : 2.8.2
setuptools       : 67.6.1
pip              : 23.1.2
Cython           : None
pytest           : 7.3.1
hypothesis       : 6.82.4
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 3.1.2
IPython          : 8.13.2
pandas_datareader: None
bs4              : 4.12.2
bottleneck       : None
brotli           : None
fastparquet      : 2023.7.0
fsspec           : 2023.6.0
gcsfs            : None
matplotlib       : 3.7.1
numba            : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : 12.0.1
pyreadstat       : None
pyxlsb           : None
s3fs             : None
scipy            : 1.11.0
snappy           : None
sqlalchemy       : 2.0.20
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
zstandard        : None
tzdata           : 2023.3
qtpy             : None
pyqt5            : None
None

@MarcoGorelli MarcoGorelli added bug Something isn't working python Related to Python Polars labels Sep 15, 2023
@stinodego
Copy link
Member

I believe this is related to #3717

Temporal windows just don't work correctly.

@stinodego stinodego added the accepted Ready for implementation label Oct 14, 2023
@github-project-automation github-project-automation bot moved this to Ready in Backlog Oct 14, 2023
@MarcoGorelli
Copy link
Collaborator Author

MarcoGorelli commented Oct 14, 2023

not totally sure it's related to that one, I think the might just be a missing early return here - will take a look

Temporal windows just don't work correctly.

I'd like to think the default case with n>0 rows should be quite robust, there's extensive hypothesis tests for those - we should probably extend all that testing to cover the rest of the params too


note to future self: might need to look here:

unsafe fn update(&mut self, start: usize, end: usize) -> T {
// if we exceed the end, we have a completely new window
// so we recompute
let recompute_sum = if start >= self.last_end || self.last_recompute > 128 {
self.last_recompute = 0;
true
} else {
self.last_recompute += 1;
// remove elements that should leave the window
let mut recompute_sum = false;
for idx in self.last_start..start {
// safety
// we are in bounds
let leaving_value = self.slice.get_unchecked(idx);
if T::is_float() && leaving_value.is_nan() {
recompute_sum = true;
break;
}
self.sum_of_squares -= *leaving_value * *leaving_value;
}
recompute_sum
};
self.last_start = start;
// we traverse all values and compute
if T::is_float() && recompute_sum {
self.sum_of_squares = self
.slice
.get_unchecked(start..end)
.iter()
.map(|v| *v * *v)
.sum::<T>();
} else {
for idx in self.last_end..end {
let entering_value = *self.slice.get_unchecked(idx);
self.sum_of_squares += entering_value * entering_value;
}
}
self.last_end = end;
self.sum_of_squares
}
}

@MarcoGorelli MarcoGorelli self-assigned this Oct 14, 2023
@MarcoGorelli MarcoGorelli added the A-temporal Area: date/time functionality label Oct 14, 2023
@MarcoGorelli
Copy link
Collaborator Author

ok found it, looks like it's currently hard-coded to return 0 in this case:

let denom = count - NumCast::from(self.ddof).unwrap();
if end - start == 1 {
T::zero()
} else if denom <= T::zero() {
//ddof would be greater than # of observations
T::infinity()
} else {
let out = (sum_of_squares - count * mean * mean) / denom;

it'd be quite a refactor, but I think it probably should be

        if denom <= T::zero() {
            None
        }

and then update would return Option<T>

@MarcoGorelli
Copy link
Collaborator Author

Interesting, the same thing happens in group_by_rolling:

In [46]: df = pl.DataFrame({'ts': [datetime(2020, 1, 1), datetime(2020, 1, 2), datetime(2020, 1, 3)], 'val': [1, 2, 3]}
    ...: ).sort('ts')
    ...:
    ...: print(df.select(pl.col('val').rolling_std('1d', by='ts', closed='right')))
    ...:
    ...: print(df.group_by_rolling('ts', period='1s', closed='right').agg(pl.col('val'), val_std=pl.col('val').std()))
shape: (3, 1)
┌─────┐
│ val │
│ --- │
│ f64 │
╞═════╡
│ 0.0 │
│ 0.0 │
│ 0.0 │
└─────┘
shape: (3, 3)
┌─────────────────────┬───────────┬─────────┐
│ tsvalval_std │
│ ---------     │
│ datetime[μs]        ┆ list[i64] ┆ f64     │
╞═════════════════════╪═══════════╪═════════╡
│ 2020-01-01 00:00:00 ┆ [1]       ┆ 0.0     │
│ 2020-01-02 00:00:00 ┆ [2]       ┆ 0.0     │
│ 2020-01-03 00:00:00 ┆ [3]       ┆ 0.0     │
└─────────────────────┴───────────┴─────────┘

and there's tests that compare them, so they'll need fixing together

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-temporal Area: date/time functionality bug Something isn't working P-low Priority: low python Related to Python Polars
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants