Skip to content

REF: tighten what we accept in TimedeltaIndex._simple_new #31315

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

Merged
merged 14 commits into from
Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from 11 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
5 changes: 4 additions & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,12 @@ def __init__(self, values, dtype=_TD_DTYPE, freq=None, copy=False):
def _simple_new(cls, values, freq=None, dtype=_TD_DTYPE):
assert dtype == _TD_DTYPE, dtype
assert isinstance(values, np.ndarray), type(values)
if values.dtype != _TD_DTYPE:
assert values.dtype == "i8"
values = values.view(_TD_DTYPE)

result = object.__new__(cls)
result._data = values.view(_TD_DTYPE)
result._data = values
result._freq = to_offset(freq)
result._dtype = _TD_DTYPE
return result
Expand Down
19 changes: 16 additions & 3 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,8 @@ def delete(self, loc):
if loc.start in (0, None) or loc.stop in (len(self), None):
freq = self.freq

return self._shallow_copy(new_i8s, freq=freq)
arr = type(self._data)._simple_new(new_i8s, dtype=self.dtype, freq=freq)
return type(self)._simple_new(arr, name=self.name)


class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, Int64Index):
Expand Down Expand Up @@ -611,6 +612,14 @@ def _shallow_copy(self, values=None, **kwargs):
if values is None:
values = self._data

if isinstance(values, type(self)):
values = values._data
if isinstance(values, np.ndarray):
# TODO: We would rather not get here
if kwargs.get("freq") is not None:
raise ValueError(kwargs)
values = type(self._data)(values, dtype=self.dtype)

attributes = self._get_attributes_dict()

if "freq" not in kwargs and self.freq is not None:
Expand Down Expand Up @@ -789,7 +798,10 @@ def _union(self, other, sort):
this, other = self._maybe_utc_convert(other)

if this._can_fast_union(other):
return this._fast_union(other, sort=sort)
result = this._fast_union(other, sort=sort)
if result.freq is None:
result._set_freq("infer")
return result
else:
result = Index._union(this, other, sort=sort)
if isinstance(result, type(self)):
Expand Down Expand Up @@ -923,7 +935,8 @@ def insert(self, loc, item):
new_i8s = np.concatenate(
(self[:loc].asi8, [item.view(np.int64)], self[loc:].asi8)
)
return self._shallow_copy(new_i8s, freq=freq)
arr = type(self._data)._simple_new(new_i8s, dtype=self.dtype, freq=freq)
return type(self)._simple_new(arr, name=self.name)
except (AttributeError, TypeError):

# fall back to object index
Expand Down
15 changes: 4 additions & 11 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,15 @@ def __new__(
def _simple_new(cls, values, name=None, freq=None, dtype=_TD_DTYPE):
# `dtype` is passed by _shallow_copy in corner cases, should always
# be timedelta64[ns] if present

if not isinstance(values, TimedeltaArray):
values = TimedeltaArray._simple_new(values, dtype=dtype, freq=freq)
else:
if freq is None:
freq = values.freq
assert isinstance(values, TimedeltaArray), type(values)
assert dtype == _TD_DTYPE, dtype
assert values.dtype == "m8[ns]", values.dtype
assert isinstance(values, TimedeltaArray)
assert freq is None or values.freq == freq

tdarr = TimedeltaArray._simple_new(values._data, freq=freq)
result = object.__new__(cls)
result._data = tdarr
result._data = values
result._name = name
# For groupby perf. See note in indexes/base about _index_data
result._index_data = tdarr._data
result._index_data = values._data

result._reset_identity()
return result
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,9 @@ def _wrap_result(self, result):
if isinstance(obj.index, PeriodIndex):
result.index = obj.index.asfreq(self.freq)
else:
result.index = obj.index._shallow_copy(freq=self.freq)
idx = obj.index
index = type(idx)([], dtype=idx.dtype, freq=self.freq, name=idx.name)
result.index = index
result.name = getattr(obj, "name", None)

return result
Expand Down Expand Up @@ -1787,8 +1789,10 @@ def asfreq(obj, freq, method=None, how=None, normalize=False, fill_value=None):

elif len(obj.index) == 0:
new_obj = obj.copy()
new_obj.index = obj.index._shallow_copy(freq=to_offset(freq))

idx = obj.index
new_index = type(idx)([], dtype=idx.dtype, name=idx.name, freq=freq)
new_obj.index = new_index
else:
dti = date_range(obj.index[0], obj.index[-1], freq=freq)
dti.name = obj.index.name
Expand Down
18 changes: 13 additions & 5 deletions pandas/tests/resample/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def test_resample_empty_series(freq, empty_series, resample_method):
if isinstance(s.index, PeriodIndex):
expected.index = s.index.asfreq(freq=freq)
else:
expected.index = s.index._shallow_copy(freq=freq)
idx = s.index
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above, why not just collapse this so: s.index.asfreq(freq=freq) or s.copy()

we don't need to have the shallow copy used like this in tests, nor work-arounds like this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented a helper _asfreq_compat for this, cleans up some code nicely

index = type(idx)([], dtype=idx.dtype, freq=freq, name=idx.name)
expected.index = index
tm.assert_index_equal(result.index, expected.index)
assert result.index.freq == expected.index.freq
tm.assert_series_equal(result, expected, check_dtype=False)
Expand All @@ -122,7 +124,9 @@ def test_resample_count_empty_series(freq, empty_series, resample_method):
if isinstance(empty_series.index, PeriodIndex):
index = empty_series.index.asfreq(freq=freq)
else:
index = empty_series.index._shallow_copy(freq=freq)
idx = empty_series.index
index = type(idx)([], dtype=idx.dtype, freq=freq, name=idx.name)

expected = pd.Series([], dtype="int64", index=index, name=empty_series.name)

tm.assert_series_equal(result, expected)
Expand All @@ -144,7 +148,9 @@ def test_resample_empty_dataframe(empty_frame, freq, resample_method):
if isinstance(df.index, PeriodIndex):
expected.index = df.index.asfreq(freq=freq)
else:
expected.index = df.index._shallow_copy(freq=freq)
idx = df.index
index = type(idx)([], dtype=idx.dtype, freq=freq, name=idx.name)
expected.index = index
tm.assert_index_equal(result.index, expected.index)
assert result.index.freq == expected.index.freq
tm.assert_almost_equal(result, expected, check_dtype=False)
Expand All @@ -165,7 +171,8 @@ def test_resample_count_empty_dataframe(freq, empty_frame):
if isinstance(empty_frame.index, PeriodIndex):
index = empty_frame.index.asfreq(freq=freq)
else:
index = empty_frame.index._shallow_copy(freq=freq)
idx = empty_frame.index
index = type(idx)([], dtype=idx.dtype, freq=freq, name=idx.name)
expected = pd.DataFrame({"a": []}, dtype="int64", index=index)

tm.assert_frame_equal(result, expected)
Expand All @@ -184,7 +191,8 @@ def test_resample_size_empty_dataframe(freq, empty_frame):
if isinstance(empty_frame.index, PeriodIndex):
index = empty_frame.index.asfreq(freq=freq)
else:
index = empty_frame.index._shallow_copy(freq=freq)
idx = empty_frame.index
index = type(idx)([], dtype=idx.dtype, freq=freq, name=idx.name)
expected = pd.Series([], dtype="int64", index=index)

tm.assert_series_equal(result, expected)
Expand Down