Skip to content

Commit 27f0000

Browse files
authored
REGR: preserve freq in DTI/TDI outer join (#32166)
1 parent 6e04264 commit 27f0000

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

doc/source/whatsnew/v1.0.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Fixed regressions
2323
- Fixed regression where :func:`read_pickle` raised a ``UnicodeDecodeError`` when reading a py27 pickle with :class:`MultiIndex` column (:issue:`31988`).
2424
- Fixed regression in :class:`DataFrame` arithmetic operations with mis-matched columns (:issue:`31623`)
2525
- Fixed regression in :meth:`GroupBy.agg` calling a user-provided function an extra time on an empty input (:issue:`31760`)
26+
- Joining on :class:`DatetimeIndex` or :class:`TimedeltaIndex` will preserve ``freq`` in simple cases (:issue:`32166`)
2627
-
2728

2829
.. ---------------------------------------------------------------------------

pandas/core/indexes/datetimelike.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -910,17 +910,14 @@ def _is_convertible_to_index_for_join(cls, other: Index) -> bool:
910910
return True
911911
return False
912912

913-
def _wrap_joined_index(self, joined, other):
913+
def _wrap_joined_index(self, joined: np.ndarray, other):
914+
assert other.dtype == self.dtype, (other.dtype, self.dtype)
914915
name = get_op_result_name(self, other)
915-
if self._can_fast_union(other):
916-
joined = self._shallow_copy(joined)
917-
joined.name = name
918-
return joined
919-
else:
920-
kwargs = {}
921-
if hasattr(self, "tz"):
922-
kwargs["tz"] = getattr(other, "tz", None)
923-
return type(self)._simple_new(joined, name, **kwargs)
916+
917+
freq = self.freq if self._can_fast_union(other) else None
918+
new_data = type(self._data)._simple_new(joined, dtype=self.dtype, freq=freq)
919+
920+
return type(self)._simple_new(new_data, name=name)
924921

925922
# --------------------------------------------------------------------
926923
# List-Like Methods

pandas/tests/indexes/datetimes/test_join.py

+13
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,16 @@ def test_naive_aware_conflicts(self):
129129

130130
with pytest.raises(TypeError, match=msg):
131131
aware.join(naive)
132+
133+
@pytest.mark.parametrize("tz", [None, "US/Pacific"])
134+
def test_join_preserves_freq(self, tz):
135+
# GH#32157
136+
dti = date_range("2016-01-01", periods=10, tz=tz)
137+
result = dti[:5].join(dti[5:], how="outer")
138+
assert result.freq == dti.freq
139+
tm.assert_index_equal(result, dti)
140+
141+
result = dti[:5].join(dti[6:], how="outer")
142+
assert result.freq is None
143+
expected = dti.delete(5)
144+
tm.assert_index_equal(result, expected)

pandas/tests/indexes/timedeltas/test_join.py

+12
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,15 @@ def test_does_not_convert_mixed_integer(self):
3535
assert cols.dtype == np.dtype("O")
3636
assert cols.dtype == joined.dtype
3737
tm.assert_index_equal(cols, joined)
38+
39+
def test_join_preserves_freq(self):
40+
# GH#32157
41+
tdi = timedelta_range("1 day", periods=10)
42+
result = tdi[:5].join(tdi[5:], how="outer")
43+
assert result.freq == tdi.freq
44+
tm.assert_index_equal(result, tdi)
45+
46+
result = tdi[:5].join(tdi[6:], how="outer")
47+
assert result.freq is None
48+
expected = tdi.delete(5)
49+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)