Skip to content

regression fix for merging DF with datetime index with empty DF #36897

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 17 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ MultiIndex

- Bug in :meth:`DataFrame.xs` when used with :class:`IndexSlice` raises ``TypeError`` with message ``"Expected label or tuple of labels"`` (:issue:`35301`)
- Bug in :meth:`DataFrame.reset_index` with ``NaT`` values in index raises ``ValueError`` with message ``"cannot convert float NaN to integer"`` (:issue:`36541`)
-
- Fixed regression in :func:`merge` on merging DatetimeIndex with empty DataFrame (:issue:`36895`)
Copy link
Contributor

Choose a reason for hiding this comment

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

sorry can you move to the reshaping section


I/O
^^^
Expand Down
11 changes: 7 additions & 4 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,12 +830,15 @@ def _maybe_add_join_keys(self, result, left_indexer, right_indexer):
rvals = algos.take_1d(take_right, right_indexer, fill_value=rfill)

# if we have an all missing left_indexer
# make sure to just use the right values
mask = left_indexer == -1
if mask.all():
# make sure to just use the right values or vice-versa
mask_left = left_indexer == -1
mask_right = right_indexer == -1
if mask_left.all():
key_col = rvals
elif mask_right.all():
key_col = lvals
else:
key_col = Index(lvals).where(~mask, rvals)
key_col = Index(lvals).where(~mask_left, rvals)

if result._is_label_reference(name):
result[name] = key_col
Expand Down
50 changes: 50 additions & 0 deletions pandas/tests/reshape/merge/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,56 @@ def test_merge_datetime_index(self, klass):
result = df.merge(df, on=[df.index.year], how="inner")
tm.assert_frame_equal(result, expected)

def test_merge_datetime_multi_index_empty_df(self):

midx1 = pd.MultiIndex.from_tuples(
[[pd.Timestamp("1950-01-01"), "A"], [pd.Timestamp("1950-01-02"), "B"]],
names=["date", "panel"],
)
left = DataFrame(
data={
"data": [1.5, 1.5],
},
index=midx1,
)

midx2 = pd.MultiIndex.from_tuples([], names=["date", "panel"])

right = DataFrame(index=midx2, columns=["state"])

midx3 = pd.MultiIndex.from_tuples(
[[pd.Timestamp("1950-01-01"), "A"], [pd.Timestamp("1950-01-02"), "B"]],
names=["date", "panel"],
)

expected_left_merge = DataFrame(
data={
"data": [1.5, 1.5],
"state": [None, None],
},
index=midx3,
)

expected_right_merge = DataFrame(
data={
"state": [None, None],
"data": [1.5, 1.5],
},
index=midx3,
)

result_left_merge = left.merge(right, how="left", on=["date", "panel"])
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 paramaterize this; it is very hard to understand what is equal to what here. you can put the expected things in the parametr. I would like to hard code the result data frame (rather than trying to do operations to get it). its very hard to read otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I parametrized the merge type in my commit -- did you want me to explicitly construct an expected data frame in the parametrize call here? https://github.com/pandas-dev/pandas/pull/36897/files#diff-513c054eb6b1b4b5767980db8c8dc0bd93f7ea55df9facea3465bfac11785474R509
That feels like it might be difficult to read but I can do it.

tm.assert_frame_equal(result_left_merge, expected_left_merge)

result_right_merge = right.merge(left, how="right", on=["date", "panel"])
tm.assert_frame_equal(result_right_merge, expected_right_merge)

result_left_join = left.join(right, how="left")
tm.assert_frame_equal(result_left_join, expected_left_merge)

result_right_join = right.join(left, how="right")
tm.assert_frame_equal(result_right_join, expected_right_merge)

def test_join_multi_levels(self):

# GH 3662
Expand Down