Skip to content

Commit

Permalink
BUG: DatetimeIndex.union with non-nano (#59037)
Browse files Browse the repository at this point in the history
* BUG: DatetimeIndex.union with non-nano

* Add as_unit
  • Loading branch information
mroeschke authored and pull[bot] committed Aug 23, 2024
1 parent bea3a18 commit 4b149df
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ Datetimelike
- Bug in :meth:`Dataframe.agg` with df with missing values resulting in IndexError (:issue:`58810`)
- Bug in :meth:`DatetimeIndex.is_year_start` and :meth:`DatetimeIndex.is_quarter_start` does not raise on Custom business days frequencies bigger then "1C" (:issue:`58664`)
- Bug in :meth:`DatetimeIndex.is_year_start` and :meth:`DatetimeIndex.is_quarter_start` returning ``False`` on double-digit frequencies (:issue:`58523`)
- Bug in :meth:`DatetimeIndex.union` when ``unit`` was non-nanosecond (:issue:`59036`)
- Bug in setting scalar values with mismatched resolution into arrays with non-nanosecond ``datetime64``, ``timedelta64`` or :class:`DatetimeTZDtype` incorrectly truncating those scalars (:issue:`56410`)

Timedelta
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ def _as_range_index(self) -> RangeIndex:
# Convert our i8 representations to RangeIndex
# Caller is responsible for checking isinstance(self.freq, Tick)
freq = cast(Tick, self.freq)
tick = Timedelta(freq).as_unit("ns")._value
tick = Timedelta(freq).as_unit(self.unit)._value
rng = range(self[0]._value, self[-1]._value + tick, tick)
return RangeIndex(rng)

Expand All @@ -536,7 +536,9 @@ def _wrap_range_setop(self, other, res_i8) -> Self:
# RangeIndex defaults to step=1, which we don't want.
new_freq = self.freq
elif isinstance(res_i8, RangeIndex):
new_freq = to_offset(Timedelta(res_i8.step))
new_freq = to_offset(
Timedelta(res_i8.step, unit=self.unit).as_unit(self.unit)
)

# TODO(GH#41493): we cannot just do
# type(self._data)(res_i8.values, dtype=self.dtype, freq=new_freq)
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,19 @@ def test_intersection_dst_transition(self, tz):
result = index1.union(index2)
expected = date_range("2021-10-28", periods=6, freq="D", tz="Europe/London")
tm.assert_index_equal(result, expected)


def test_union_non_nano_rangelike():
# GH 59036
l1 = DatetimeIndex(
["2024-05-11", "2024-05-12"], dtype="datetime64[us]", name="Date", freq="D"
)
l2 = DatetimeIndex(["2024-05-13"], dtype="datetime64[us]", name="Date", freq="D")
result = l1.union(l2)
expected = DatetimeIndex(
["2024-05-11", "2024-05-12", "2024-05-13"],
dtype="datetime64[us]",
name="Date",
freq="D",
)
tm.assert_index_equal(result, expected)

0 comments on commit 4b149df

Please sign in to comment.