Skip to content

Commit 55f0666

Browse files
mroeschkejreback
authored andcommitted
BUG: merge_asof with multiple by columns with tz (pandas-dev#27243)
1 parent c95027f commit 55f0666

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,7 @@ Reshaping
11511151
- Bug in :func:`DataFrame.pivot_table` with a :class:`IntervalIndex` as pivot index would raise ``TypeError`` (:issue:`25814`)
11521152
- Bug in :meth:`DataFrame.transpose` where transposing a DataFrame with a timezone-aware datetime column would incorrectly raise ``ValueError`` (:issue:`26825`)
11531153
- Bug in :func:`pivot_table` when pivoting a timezone aware column as the ``values`` would remove timezone information (:issue:`14948`)
1154+
- Bug in :func:`merge_asof` when specifying multiple ``by`` columns where one is ``datetime64[ns, tz]`` dtype (:issue:`26649`)
11541155
11551156
Sparse
11561157
^^^^^^

pandas/core/reshape/merge.py

+3
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,9 @@ def _get_join_indexers(self):
16861686

16871687
def flip(xs):
16881688
""" unlike np.transpose, this returns an array of tuples """
1689+
xs = [
1690+
x if not is_extension_array_dtype(x) else x._ndarray_values for x in xs
1691+
]
16891692
labels = list(string.ascii_lowercase[: len(xs)])
16901693
dtypes = [x.dtype for x in xs]
16911694
labeled_dtypes = list(zip(labels, dtypes))

pandas/tests/reshape/merge/test_merge_asof.py

+29-3
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ def test_basic_left_index(self):
190190
result = merge_asof(
191191
trades, quotes, left_index=True, right_on="time", by="ticker"
192192
)
193-
# left-only index uses right's index, oddly
193+
# left-only index uses right"s index, oddly
194194
expected.index = result.index
195-
# time column appears after left's columns
195+
# time column appears after left"s columns
196196
expected = expected[result.columns]
197197
assert_frame_equal(result, expected)
198198

@@ -233,7 +233,7 @@ def test_multi_index(self):
233233

234234
def test_on_and_index(self):
235235

236-
# 'on' parameter and index together is prohibited
236+
# "on" parameter and index together is prohibited
237237
trades = self.trades.set_index("time")
238238
quotes = self.quotes.set_index("time")
239239
with pytest.raises(MergeError):
@@ -1220,3 +1220,29 @@ def test_merge_by_col_tz_aware(self):
12201220
columns=["by_col", "on_col", "values_x", "values_y"],
12211221
)
12221222
assert_frame_equal(result, expected)
1223+
1224+
def test_by_mixed_tz_aware(self):
1225+
# GH 26649
1226+
left = pd.DataFrame(
1227+
{
1228+
"by_col1": pd.DatetimeIndex(["2018-01-01"]).tz_localize("UTC"),
1229+
"by_col2": ["HELLO"],
1230+
"on_col": [2],
1231+
"value": ["a"],
1232+
}
1233+
)
1234+
right = pd.DataFrame(
1235+
{
1236+
"by_col1": pd.DatetimeIndex(["2018-01-01"]).tz_localize("UTC"),
1237+
"by_col2": ["WORLD"],
1238+
"on_col": [1],
1239+
"value": ["b"],
1240+
}
1241+
)
1242+
result = pd.merge_asof(left, right, by=["by_col1", "by_col2"], on="on_col")
1243+
expected = pd.DataFrame(
1244+
[[pd.Timestamp("2018-01-01", tz="UTC"), "HELLO", 2, "a"]],
1245+
columns=["by_col1", "by_col2", "on_col", "value_x"],
1246+
)
1247+
expected["value_y"] = np.array([np.nan], dtype=object)
1248+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)