Skip to content

Commit 92d8089

Browse files
committed
fix shift
1 parent 3e1ee5e commit 92d8089

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

pandas/core/arrays/datetimes.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
is_extension_type, is_float_dtype, is_int64_dtype, is_object_dtype,
2020
is_period_dtype, is_string_dtype, is_timedelta64_dtype, pandas_dtype)
2121
from pandas.core.dtypes.dtypes import DatetimeTZDtype
22-
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
22+
from pandas.core.dtypes.generic import ABCPandasArray, ABCIndexClass, ABCSeries
2323
from pandas.core.dtypes.missing import isna
2424

2525
from pandas.core import ops
@@ -1644,6 +1644,8 @@ def sequence_to_dt64ns(data, dtype=None, copy=False,
16441644
copy = False
16451645
elif isinstance(data, ABCSeries):
16461646
data = data._values
1647+
elif isinstance(data, ABCPandasArray):
1648+
data = data._ndarray
16471649

16481650
if hasattr(data, "freq"):
16491651
# i.e. DatetimeArray/Index

pandas/core/indexes/datetimelike.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,37 @@ def _time_shift(self, periods, freq=None):
666666

667667
@deprecate_kwarg(old_arg_name='n', new_arg_name='periods')
668668
def shift(self, periods, freq=None):
669-
new_values = self._data.shift(periods, freq=freq)
670-
return self._simple_new(new_values, name=self.name, freq=self.freq)
669+
"""
670+
Shift index by desired number of time frequency increments.
671+
672+
This method is for shifting the values of datetime-like indexes
673+
by a specified time increment a given number of times.
674+
675+
Parameters
676+
----------
677+
periods : int
678+
Number of periods (or increments) to shift by,
679+
can be positive or negative.
680+
681+
.. versionchanged:: 0.24.0
682+
683+
freq : pandas.DateOffset, pandas.Timedelta or string, optional
684+
Frequency increment to shift by.
685+
If None, the index is shifted by its own `freq` attribute.
686+
Offset aliases are valid strings, e.g., 'D', 'W', 'M' etc.
687+
688+
Returns
689+
-------
690+
pandas.DatetimeIndex
691+
Shifted index.
692+
693+
See Also
694+
--------
695+
Index.shift : Shift values of Index.
696+
PeriodIndex.shift : Shift values of PeriodIndex.
697+
"""
698+
result = self._eadata._time_shift(periods, freq=freq)
699+
return type(self)(result, name=self.name)
671700

672701

673702
def wrap_arithmetic_op(self, other, result):

pandas/core/indexes/datetimes.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -316,16 +316,18 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):
316316
if we are passed a non-dtype compat, then coerce using the constructor
317317
"""
318318
# DatetimeArray._simple_new will accept either i8 or M8[ns] dtypes
319-
values = DatetimeArray._simple_new(values, freq=freq, tz=tz)
320-
319+
if isinstance(values, DatetimeIndex):
320+
values = values._data
321321
dtarr = DatetimeArray._simple_new(values, freq=freq, tz=tz)
322+
assert isinstance(dtarr, DatetimeArray)
323+
322324
result = object.__new__(cls)
323-
result._data = dtarr._data
324-
result._freq = dtarr.freq
325+
result._data = dtarr
326+
result._freq = dtarr._freq
325327
result._tz = dtarr.tz
326328
result.name = name
327329
# For groupby perf. See note in indexes/base about _index_data
328-
result._index_data = values._data
330+
result._index_data = dtarr._data
329331
result._reset_identity()
330332
return result
331333

@@ -335,15 +337,6 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):
335337
def dtype(self):
336338
return self._eadata.dtype
337339

338-
@property
339-
def _values(self):
340-
# tz-naive -> ndarray
341-
# tz-aware -> DatetimeIndex
342-
if self.tz is not None:
343-
return self
344-
else:
345-
return self.values
346-
347340
@property
348341
def tz(self):
349342
# GH 18595

pandas/core/indexes/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def _simple_new(cls, values, name=None, freq=None, dtype=_TD_DTYPE):
245245
freq = to_offset(freq)
246246
tdarr = TimedeltaArray._simple_new(values, freq=freq)
247247
result = object.__new__(cls)
248-
result._data = tdarr._data
248+
result._data = tdarr
249249
result._freq = tdarr._freq
250250
result.name = name
251251
# For groupby perf. See note in indexes/base about _index_data

0 commit comments

Comments
 (0)