Skip to content

ENH: allow non-Tick offsets in index.round/ceil/floor #27090

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

Closed
wants to merge 5 commits into from
Closed
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
21 changes: 20 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,26 @@ class TimelikeOps:
def _round(self, freq, mode, ambiguous, nonexistent):
# round the local times
values = _ensure_datetimelike_to_i8(self)
result = round_nsint64(values, mode, freq)
try:
result = round_nsint64(values, mode, freq)
except ValueError as e:
if "non-fixed" in str(e):
offset = frequencies.to_offset(freq)
if mode == RoundTo.PLUS_INFTY:
result = self.to_period(offset) \
.to_timestamp(how='end').asi8
elif mode == RoundTo.MINUS_INFTY:
result = self.to_period(offset) \
.to_timestamp(how='start').asi8
elif mode == RoundTo.NEAREST_HALF_EVEN:
msg = ("round only supports fixed offsets "
"(i.e. 'Day' is ok, 'MonthEnd' is not). "
"You can use dti.snap or floor/ceil if "
"applicable.")
raise ValueError(msg)
else:
raise e

result = self._maybe_mask_results(result, fill_value=NaT)

dtype = self.dtype
Expand Down
98 changes: 97 additions & 1 deletion pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
import pandas as pd
from pandas.core.arrays import DatetimeArray
from pandas.core.arrays.datetimes import sequence_to_dt64ns
from pandas.tseries.offsets import Nano
from pandas import Timestamp
import pandas.util.testing as tm


class TestDatetimeArrayConstructor:

def test_only_1dim_accepted(self):
Expand Down Expand Up @@ -301,3 +302,98 @@ def test_min_max_empty(self, skipna, tz):

result = arr.max(skipna=skipna)
assert result is pd.NaT

@pytest.mark.parametrize(
'op, freq, dates, expected_dates',
[
(
'floor',
'M',
("2001-02-01",
Timestamp("2001-02-14 12:00") - Nano(),
"2001-02-14 12:00",
"2001-02-15",
Timestamp("2001-03-01") - Nano(),
"2001-03-01"
),
[
Timestamp("2001-02-01") - Nano(),
Timestamp("2001-02-01") - Nano(),
Timestamp("2001-02-01") - Nano(),
Timestamp("2001-02-01") - Nano(),
Timestamp("2001-03-01") - Nano(),
Timestamp("2001-03-01") - Nano(),
]
),
(
'ceil',
'M',
("2001-02-01",
Timestamp(
"2001-02-14 12:00") - Nano(),
"2001-02-14 12:00",
"2001-02-15",
Timestamp("2001-03-01") - Nano(),
"2001-03-01"
),
[
Timestamp("2001-03-01") - Nano(),
Timestamp("2001-03-01") - Nano(),
Timestamp("2001-03-01") - Nano(),
Timestamp("2001-03-01") - Nano(),
Timestamp("2001-03-01") - Nano(),
Timestamp("2001-04-01") - Nano(),
]
),
(
'floor',
'MS',
("2001-02-01",
Timestamp("2001-02-14 12:00") - Nano(),
"2001-02-14 12:00",
"2001-02-15",
Timestamp("2001-03-01") - Nano(),
"2001-03-01"
),
[
"2001-02-01",
"2001-02-01",
"2001-02-01",
"2001-02-01",
"2001-02-01",
"2001-03-01"
]
),
(
'ceil',
'MS',
("2001-02-01",
Timestamp("2001-02-14 12:00") - Nano(),
"2001-02-14 12:00",
"2001-02-15",
Timestamp("2001-03-01") - Nano(),
"2001-03-01"
),
[
"2001-02-01",
"2001-03-01",
"2001-03-01",
"2001-03-01",
"2001-03-01",
"2001-03-01",
]
)
]
)
def test_ceil_floor(self, op, freq, dates, expected_dates):
dta = DatetimeArray._from_sequence(dates)
dta[1] -= pd.offsets.Nano()
dta[-2] -= pd.offsets.Nano()
dti = pd.DatetimeIndex(dta)
result = getattr(dti, op)(freq)
expected = pd.DatetimeIndex(expected_dates)
tm.assert_index_equal(result, expected)

# Idempotent
result = getattr(result, op)(freq)
tm.assert_index_equal(result, expected)