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

Support non unique period indexes on join and merge operations #16949

Merged
merged 3 commits into from
Jul 15, 2017
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3119,14 +3119,14 @@ def _join_multi(self, other, how, return_indexers=True):
def _join_non_unique(self, other, how='left', return_indexers=False):
from pandas.core.reshape.merge import _get_join_indexers

left_idx, right_idx = _get_join_indexers([self.values],
left_idx, right_idx = _get_join_indexers([self._values],
[other._values], how=how,
sort=True)

left_idx = _ensure_platform_int(left_idx)
right_idx = _ensure_platform_int(right_idx)

join_index = np.asarray(self.values.take(left_idx))
join_index = np.asarray(self._values.take(left_idx))
mask = left_idx == -1
np.putmask(join_index, mask, other._values.take(right_idx))

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/reshape/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,13 @@ def test_join_mixed_non_unique_index(self):
index=[1, 2, 2, 'a'])
tm.assert_frame_equal(result, expected)

def test_join_non_unique_period_index(self):
per_index = pd.period_range('2016-01-01', periods=16, freq='M')
Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number here as a comment

Copy link
Contributor

Choose a reason for hiding this comment

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

call this index

per_df = DataFrame([i for i in range(len(per_index))],
Copy link
Contributor

Choose a reason for hiding this comment

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

call this df

index=per_index, columns=['pnum'])
df2 = concat([per_df, per_df])
per_df.join(df2, how='outer', rsuffix='_df2')
Copy link
Contributor

Choose a reason for hiding this comment

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

result =

and provide an expected result; compare using tm.assert_frame_equal


def test_mixed_type_join_with_suffix(self):
# GH #916
df = DataFrame(np.random.randn(20, 6),
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/reshape/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,13 @@ def test_merge_on_datetime64tz(self):
assert result['value_x'].dtype == 'datetime64[ns, US/Eastern]'
assert result['value_y'].dtype == 'datetime64[ns, US/Eastern]'

def test_merge_non_unique_period_index(self):
per_index = pd.period_range('2016-01-01', periods=16, freq='M')
per_df = DataFrame([i for i in range(len(per_index))],
index=per_index, columns=['pnum'])
df2 = concat([per_df, per_df])
per_df.merge(df2, left_index=True, right_index=True, how='outer')

Copy link
Contributor

Choose a reason for hiding this comment

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

same here

def test_merge_on_periods(self):
left = pd.DataFrame({'key': pd.period_range('20151010', periods=2,
freq='D'),
Expand Down