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

BUG: merging with a boolean/int categorical column #17841

Merged
merged 3 commits into from
Oct 14, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ Categorical
- Bug in :func:`Series.isin` when called with a categorical (:issue:`16639`)
- Bug in the categorical constructor with empty values and categories causing the ``.categories`` to be an empty ``Float64Index`` rather than an empty ``Index`` with object dtype (:issue:`17248`)
- Bug in categorical operations with :ref:`Series.cat <categorical.cat>` not preserving the original Series' name (:issue:`17509`)
- Bug in :func:`DataFrame.merge` failing for categorical columns with boolean/int data types (:issue:`17187`)

PyPy
^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5494,7 +5494,7 @@ def get_reindexed_values(self, empty_dtype, upcasted_na):
# preserve these for validation in _concat_compat
return self.block.values

if self.block.is_bool:
if self.block.is_bool and not self.block.is_categorical:
# External code requested filling/upcasting, bool values must
# be upcasted to object to avoid being upcasted to numeric.
values = self.block.astype(np.object_).values
Expand Down
43 changes: 43 additions & 0 deletions pandas/tests/reshape/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,49 @@ def test_dtype_on_categorical_dates(self):
result_inner = pd.merge(df, df2, how='inner', on=['date'])
assert_frame_equal(result_inner, expected_inner)

Copy link
Contributor

Choose a reason for hiding this comment

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

can you parametrize this and make it a single test (with 3 cases)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

def test_merging_with_boolean_cateorical_column(self):
df1 = pd.DataFrame({'id': [1, 2, 3, 4],
Copy link
Member

Choose a reason for hiding this comment

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

Can you add the github issue number here as a comment? And for the other tests you wrote too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

'cat': [False, True, True, False]})
df1['cat'] = df1['cat'].astype('category',
categories=[True, False], ordered=True)
df2 = pd.DataFrame({'id': [2, 4], 'num': [1, 9]})
result = df1.merge(df2)
expected = pd.DataFrame({'id': [2, 4], 'cat': [True, False],
'num': [1, 9]})
expected['cat'] = expected['cat'].astype('category',
categories=[True, False],
ordered=True)
assert_frame_equal(expected, result)

def test_merging_with_integer_cateorical_column(self):
df1 = pd.DataFrame({'id': [1, 2, 3, 4],
'cat': [2, 1, 1, 2]})
df1['cat'] = df1['cat'].astype('category',
categories=[1, 2], ordered=True)
df2 = pd.DataFrame({'id': [2, 4], 'num': [1, 9]})
result = df1.merge(df2)
expected = pd.DataFrame({'id': [2, 4], 'cat': [1, 2],
'num': [1, 9]})
expected['cat'] = expected['cat'].astype('category',
categories=[1, 2],
ordered=True)
assert_frame_equal(expected, result)

def test_merging_with_string_cateorical_column(self):
df1 = pd.DataFrame({'id': [1, 2, 3, 4],
'cat': ['False', 'True', 'True', 'False']})
df1['cat'] = df1['cat'].astype('category',
categories=['True', 'False'],
ordered=True)
df2 = pd.DataFrame({'id': [2, 4], 'num': [1, 9]})
result = df1.merge(df2)
expected = pd.DataFrame({'id': [2, 4], 'cat': ['True', 'False'],
'num': [1, 9]})
expected['cat'] = expected['cat'].astype('category',
categories=['True', 'False'],
ordered=True)
assert_frame_equal(expected, result)


@pytest.fixture
def left_df():
Expand Down