Skip to content

BUG: {expanding,rolling}_{cov,corr} don't handle arguments with different index sets properly #7604

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

Merged
merged 1 commit into from
Jul 1, 2014

Conversation

seth-p
Copy link
Contributor

@seth-p seth-p commented Jun 29, 2014

Closes #7512.

@seth-p seth-p changed the title BUG: {expanding,rolling}_{cov,corr} don't handlt arguments with different index sets properly BUG: {expanding,rolling}_{cov,corr} don't handle arguments with different index sets properly Jun 29, 2014
@jreback jreback added this to the 0.14.1 milestone Jun 29, 2014
@jreback
Copy link
Contributor

jreback commented Jun 30, 2014

needs a release note in v0.14.1 (in bug fix section)

@@ -917,7 +918,7 @@ def expanding_cov(arg1, arg2=None, min_periods=1, freq=None, center=False,
min_periods = arg2
arg2 = arg1
pairwise = True if pairwise is None else pairwise
window = max(len(arg1), len(arg2))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expanding_xxx() functions need window = len(arg1) + len(arg2) rather than = max(len(arg1), len(arg2)) because once arg1 and arg2 are merged the result could be of length up to len(arg1) + len(arg2) if there is no overlap between the index (or major_axis) of arg1 and arg2. If the index of one is a subset of the index of the other, then max() will be fine; but in general when neither is a subset of the other max() will be too small.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, without this fix, see the following; whereas the final value should be 1.

In [2]: s1 = Series([7,8,10], index=[0,1,3])

In [3]: s2 = Series([7,9,10], index=[0,2,3])

In [4]: expanding_corr(s1, s2)
Out[4]:
0   NaN
1   NaN
2   NaN
3   NaN
dtype: float64

There isn't quite a test for this. I think I should add one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would u not align the 2 objects before you start, then all this is moot

eg try doing

df1, df2 = df1.align(df2)

and see what that results

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea. This is the way it was written, and I didn't want to modify things any more than necessary.
At the very least I will add a test, and will see if the .align() thing works. Probably won't get it done until tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing:

    window = len(arg1) + len(arg2)

with:

    arg1, arg2 = arg1.align(arg2)
    window = len(arg1)

in expanding_cov/corr() works for series, but not for data frames: it not only merges the index, but also the columns, thus producing the wrong result for pairwise=True calls -- e.g. if arg1.columns=['A','B'] and arg2.columns=['X','Y'], then while the result should have major/minor axes ['A','B'] and ['X','Y'], instead it ends up with both being ['A','B','X','Y'].

At any rate, since the two objects end up being aligned anyway in _flex_binary_moment(), aligning them in expanding_cov/corr is just redundant.

I updated the code to include several additional tests, but left window = len(arg1) + len(arg2) unchanged.

@jreback
Copy link
Contributor

jreback commented Jul 1, 2014

ok, just ensuring that this is a bug fix only (and not actually changing the result; which is technically wrong currently when the indices are not aligned).

jreback added a commit that referenced this pull request Jul 1, 2014
BUG: {expanding,rolling}_{cov,corr} don't handle arguments with different index sets properly
@jreback jreback merged commit bfc3a12 into pandas-dev:master Jul 1, 2014
@jreback
Copy link
Contributor

jreback commented Jul 1, 2014

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: {expanding,rolling}_{cov,corr} functions between objects with different index sets
2 participants