Skip to content

BUG: Fix index order for Index.intersection() #15583

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
wants to merge 23 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix error: line too long
  • Loading branch information
Albert Villanova del Moral authored and jreback committed Mar 25, 2017
commit 784fe75997c88a2298f2cea634e6f22a9fcc1af6
3 changes: 2 additions & 1 deletion pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,8 @@ def intersection(self, other):
not other.offset.isAnchored() or
(not self.is_monotonic or not other.is_monotonic)):
result = Index.intersection(self, other)
result = self._simple_new(result._values, name=result.name, tz=result.tz)
result = self._simple_new(result._values, name=result.name,
Copy link
Contributor

Choose a reason for hiding this comment

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

use _shallow_copy rather than _simple_new

Copy link
Contributor Author

@albertvillanova albertvillanova Mar 13, 2017

Choose a reason for hiding this comment

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

If I use _shallow_copy, the result will inherit all self attributes (e.g., freq) and this might not be the expected result. For example, this test would fail:

base = date_range('6/1/2000', '6/30/2000', freq='D', name='idx')

rng4 = date_range('7/1/2000', '7/31/2000', freq='D', name='idx') 
expected4 = DatetimeIndex([], name='idx')

because result would inherit self freq = 'D'.

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe you misunderstand, simply pass the new freq

result = self._shallow_copy(...., freq=....)

We don't use _simple_new at all (outside of a few specific places), this is not one of them. This is all documented next to these functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Might I simply use the constructor DatetimeIndex()? I ask this because I think the result freq is not known in advance.

Copy link
Contributor

Choose a reason for hiding this comment

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

no, use _shallow_copy with freq=None, which will not infer it, and then you can reset the freq after

tz=result.tz)
if result.freq is None:
result.offset = to_offset(result.inferred_freq)
Copy link
Member

Choose a reason for hiding this comment

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

Is this change related to the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, as the simple result of Index.intersection inherits all attributes from other, and this is not the expected result. Therefore, it is necessary to create a new DatetimeIndex using only three of the Index.intersection attributes: _values, name and tz.

return result
Expand Down