Skip to content

Commit

Permalink
BUG: fix pandas-dev#30353 invalid end
Browse files Browse the repository at this point in the history
  • Loading branch information
hasB4K committed Apr 14, 2020
1 parent 4562a25 commit 5e269d4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
10 changes: 6 additions & 4 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from pandas.core import nanops
from pandas.core.algorithms import checked_add_with_arr
from pandas.core.arrays import datetimelike as dtl
from pandas.core.arrays._ranges import _generate_range_overflow_safe
import pandas.core.common as com
from pandas.core.construction import extract_array

Expand Down Expand Up @@ -1061,14 +1062,15 @@ def _generate_regular_range(start, end, periods, offset):
stride = offset.nanos
if periods is None:
b = Timedelta(start).value
e = Timedelta(end).value
e += stride - e % stride
# cannot just use e = Timestamp(end) + 1 because arange breaks when
# stride is too large, see GH 10887 & GH 30353
e = b + (Timedelta(end).value - b) // stride * stride + stride // 2 + 1
elif start is not None:
b = Timedelta(start).value
e = b + periods * stride
e = _generate_range_overflow_safe(b, periods, stride, side="start")
elif end is not None:
e = Timedelta(end).value + stride
b = e - periods * stride
b = _generate_range_overflow_safe(e, periods, stride, side="end")
else:
raise ValueError(
"at least 'start' or 'end' should be specified if a 'period' is given."
Expand Down
11 changes: 2 additions & 9 deletions pandas/tests/resample/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pandas.core.groupby.grouper import Grouper
from pandas.core.indexes.datetimes import date_range
from pandas.core.indexes.period import PeriodIndex, period_range
from pandas.core.indexes.timedeltas import TimedeltaIndex, timedelta_range
from pandas.core.indexes.timedeltas import timedelta_range
from pandas.core.resample import _asfreq_compat

# a fixture value can be overridden by the test parameter value. Note that the
Expand Down Expand Up @@ -182,7 +182,6 @@ def test_resample_size_empty_dataframe(freq, empty_frame_dti):
@pytest.mark.parametrize("index", tm.all_timeseries_index_generator(0))
@pytest.mark.parametrize("dtype", [np.float, np.int, np.object, "datetime64[ns]"])
def test_resample_empty_dtypes(index, dtype, resample_method):

# Empty series were sometimes causing a segfault (for the functions
# with Cython bounds-checking disabled) or an IndexError. We just run
# them to ensure they no longer do. (GH #10228)
Expand Down Expand Up @@ -215,13 +214,7 @@ def test_resample_loffset_arg_type(frame, create_index, arg):
if isinstance(arg, list):
expected.columns = pd.MultiIndex.from_tuples([("value", "mean")])

# GH 13022, 7687 - TODO: fix resample w/ TimedeltaIndex
if isinstance(expected.index, TimedeltaIndex):
msg = "DataFrame are different"
with pytest.raises(AssertionError, match=msg):
tm.assert_frame_equal(result_agg, expected)
else:
tm.assert_frame_equal(result_agg, expected)
tm.assert_frame_equal(result_agg, expected)


@all_ts
Expand Down
24 changes: 22 additions & 2 deletions pandas/tests/resample/test_timedelta.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import timedelta

import numpy as np
import pytest

import pandas as pd
from pandas import DataFrame, Series
Expand Down Expand Up @@ -114,14 +115,33 @@ def test_resample_timedelta_values():
# check that timedelta dtype is preserved when NaT values are
# introduced by the resampling

times = timedelta_range("1 day", "4 day", freq="4D")
times = timedelta_range("1 day", "6 day", freq="4D")
df = DataFrame({"time": times}, index=times)

times2 = timedelta_range("1 day", "4 day", freq="2D")
times2 = timedelta_range("1 day", "6 day", freq="2D")
exp = Series(times2, index=times2, name="time")
exp.iloc[1] = pd.NaT

res = df.resample("2D").first()["time"]
tm.assert_series_equal(res, exp)
res = df["time"].resample("2D").first()
tm.assert_series_equal(res, exp)


@pytest.mark.parametrize(
"freq, resample_freq, start, periods, expected_resample_end",
[("10S", "3H", "8H", 5040, "20H")],
)
def test_resample_timedelta_end_already_included_in_bins(
freq, resample_freq, start, periods, expected_resample_end,
):
# GH 30353
# check that the timedelta bins does not contains an extra bin
idx = pd.timedelta_range(start=start, freq=freq, periods=periods)
s = pd.Series(np.arange(periods), index=idx)
result = s.resample(resample_freq).min()
expected_index = pd.timedelta_range(
freq=resample_freq, start=start, end=expected_resample_end
)
tm.assert_index_equal(result.index, expected_index)
assert not np.isnan(result[-1])

0 comments on commit 5e269d4

Please sign in to comment.