Skip to content
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

BUG: DatetimeIndex.union with non-nano #59037

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
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
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 @@ -489,6 +489,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)
Loading