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
Changes from all 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
@@ -467,7 +467,6 @@ 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`)
-

I/O
^^^
@@ -534,6 +533,7 @@ Reshaping
- Bug in :meth:`DataFrame.pivot` did not preserve :class:`MultiIndex` level names for columns when rows and columns both multiindexed (:issue:`36360`)
- Bug in :func:`join` returned a non deterministic level-order for the resulting :class:`MultiIndex` (:issue:`36910`)
- Bug in :meth:`DataFrame.combine_first()` caused wrong alignment with dtype ``string`` and one level of ``MultiIndex`` containing only ``NA`` (:issue:`37591`)
- Fixed regression in :func:`merge` on merging DatetimeIndex with empty DataFrame (:issue:`36895`)

Sparse
^^^^^^
11 changes: 7 additions & 4 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
@@ -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
49 changes: 48 additions & 1 deletion pandas/tests/reshape/merge/test_multi.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
import pytest

import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series
from pandas import DataFrame, Index, MultiIndex, Series, Timestamp
import pandas._testing as tm
from pandas.core.reshape.concat import concat
from pandas.core.reshape.merge import merge
@@ -481,6 +481,53 @@ def test_merge_datetime_index(self, klass):
result = df.merge(df, on=[df.index.year], how="inner")
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("merge_type", ["left", "right"])
def test_merge_datetime_multi_index_empty_df(self, merge_type):
# see gh-36895

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

right = DataFrame(
index=MultiIndex.from_tuples([], names=["date", "panel"]), columns=["state"]
)

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

if merge_type == "left":
expected = DataFrame(
data={
"data": [1.5, 1.5],
"state": [None, None],
},
index=expected_index,
)
results_merge = left.merge(right, how="left", on=["date", "panel"])
results_join = left.join(right, how="left")
else:
expected = DataFrame(
data={
"state": [None, None],
"data": [1.5, 1.5],
},
index=expected_index,
)
results_merge = right.merge(left, how="right", on=["date", "panel"])
results_join = right.join(left, how="right")

tm.assert_frame_equal(results_merge, expected)
tm.assert_frame_equal(results_join, expected)

def test_join_multi_levels(self):

# GH 3662