Skip to content

Commit 85663ea

Browse files
jbrockmendelPingviinituutti
authored andcommitted
Misc separable pieces of pandas-dev#24024 (pandas-dev#24488)
1 parent a631c75 commit 85663ea

File tree

8 files changed

+41
-108
lines changed

8 files changed

+41
-108
lines changed

pandas/core/arrays/datetimelike.py

+1-34
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pandas.compat.numpy import function as nv
1616
from pandas.errors import (
1717
AbstractMethodError, NullFrequencyError, PerformanceWarning)
18-
from pandas.util._decorators import Appender, Substitution, deprecate_kwarg
18+
from pandas.util._decorators import Appender, Substitution
1919

2020
from pandas.core.dtypes.common import (
2121
is_bool_dtype, is_categorical_dtype, is_datetime64_any_dtype,
@@ -1078,39 +1078,6 @@ def _addsub_offset_array(self, other, op):
10781078
return type(self)(res_values, freq='infer')
10791079
return self._from_sequence(res_values)
10801080

1081-
@deprecate_kwarg(old_arg_name='n', new_arg_name='periods')
1082-
def shift(self, periods, freq=None):
1083-
"""
1084-
Shift index by desired number of time frequency increments.
1085-
1086-
This method is for shifting the values of datetime-like indexes
1087-
by a specified time increment a given number of times.
1088-
1089-
Parameters
1090-
----------
1091-
periods : int
1092-
Number of periods (or increments) to shift by,
1093-
can be positive or negative.
1094-
1095-
.. versionchanged:: 0.24.0
1096-
1097-
freq : pandas.DateOffset, pandas.Timedelta or string, optional
1098-
Frequency increment to shift by.
1099-
If None, the index is shifted by its own `freq` attribute.
1100-
Offset aliases are valid strings, e.g., 'D', 'W', 'M' etc.
1101-
1102-
Returns
1103-
-------
1104-
pandas.DatetimeIndex
1105-
Shifted index.
1106-
1107-
See Also
1108-
--------
1109-
Index.shift : Shift values of Index.
1110-
PeriodIndex.shift : Shift values of PeriodIndex.
1111-
"""
1112-
return self._time_shift(periods=periods, freq=freq)
1113-
11141081
def _time_shift(self, periods, freq=None):
11151082
"""
11161083
Shift each value by `periods`.

pandas/core/arrays/period.py

-27
Original file line numberDiff line numberDiff line change
@@ -435,33 +435,6 @@ def value_counts(self, dropna=False):
435435

436436
# --------------------------------------------------------------------
437437

438-
def shift(self, periods=1, fill_value=None):
439-
"""
440-
Shift values by desired number.
441-
442-
Newly introduced missing values are filled with
443-
``self.dtype.na_value``.
444-
445-
.. versionadded:: 0.24.0
446-
447-
Parameters
448-
----------
449-
periods : int, default 1
450-
The number of periods to shift. Negative values are allowed
451-
for shifting backwards.
452-
fill_value : optional, default NaT
453-
454-
.. versionadded:: 0.24.0
455-
456-
Returns
457-
-------
458-
shifted : PeriodArray
459-
"""
460-
# TODO(DatetimeArray): remove
461-
# The semantics for Index.shift differ from EA.shift
462-
# then just call super.
463-
return ExtensionArray.shift(self, periods, fill_value=fill_value)
464-
465438
def _time_shift(self, n, freq=None):
466439
"""
467440
Shift each value by `periods`.

pandas/core/groupby/groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ def _try_cast(self, result, obj, numeric_only=False):
759759
760760
"""
761761
if obj.ndim > 1:
762-
dtype = obj.values.dtype
762+
dtype = obj._values.dtype
763763
else:
764764
dtype = obj.dtype
765765

@@ -768,7 +768,7 @@ def _try_cast(self, result, obj, numeric_only=False):
768768
# The function can return something of any type, so check
769769
# if the type is compatible with the calling EA.
770770
try:
771-
result = obj.values._from_sequence(result)
771+
result = obj._values._from_sequence(result, dtype=dtype)
772772
except Exception:
773773
# https://github.com/pandas-dev/pandas/issues/22850
774774
# pandas has no control over what 3rd-party ExtensionArrays

pandas/core/indexes/base.py

-30
Original file line numberDiff line numberDiff line change
@@ -602,36 +602,6 @@ def _shallow_copy_with_infer(self, values, **kwargs):
602602
pass
603603
return Index(values, **attributes)
604604

605-
def _deepcopy_if_needed(self, orig, copy=False):
606-
"""
607-
Make a copy of self if data coincides (in memory) with orig.
608-
Subclasses should override this if self._base is not an ndarray.
609-
610-
.. versionadded:: 0.19.0
611-
612-
Parameters
613-
----------
614-
orig : ndarray
615-
other ndarray to compare self._data against
616-
copy : boolean, default False
617-
when False, do not run any check, just return self
618-
619-
Returns
620-
-------
621-
A copy of self if needed, otherwise self : Index
622-
"""
623-
if copy:
624-
# Retrieve the "base objects", i.e. the original memory allocations
625-
if not isinstance(orig, np.ndarray):
626-
# orig is a DatetimeIndex
627-
orig = orig.values
628-
orig = orig if orig.base is None else orig.base
629-
new = self._data if self._data.base is None else self._data.base
630-
if orig is new:
631-
return self.copy(deep=True)
632-
633-
return self
634-
635605
def _update_inplace(self, result, **kwargs):
636606
# guard when called from IndexOpsMixin
637607
raise TypeError("Index can't be updated inplace")

pandas/core/indexes/datetimelike.py

+30-2
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,37 @@ def _time_shift(self, periods, freq=None):
603603
return type(self)(result, name=self.name)
604604

605605
@deprecate_kwarg(old_arg_name='n', new_arg_name='periods')
606-
@Appender(DatetimeLikeArrayMixin.shift.__doc__)
607606
def shift(self, periods, freq=None):
608-
result = self._eadata.shift(periods, freq=freq)
607+
"""
608+
Shift index by desired number of time frequency increments.
609+
610+
This method is for shifting the values of datetime-like indexes
611+
by a specified time increment a given number of times.
612+
613+
Parameters
614+
----------
615+
periods : int
616+
Number of periods (or increments) to shift by,
617+
can be positive or negative.
618+
619+
.. versionchanged:: 0.24.0
620+
621+
freq : pandas.DateOffset, pandas.Timedelta or string, optional
622+
Frequency increment to shift by.
623+
If None, the index is shifted by its own `freq` attribute.
624+
Offset aliases are valid strings, e.g., 'D', 'W', 'M' etc.
625+
626+
Returns
627+
-------
628+
pandas.DatetimeIndex
629+
Shifted index.
630+
631+
See Also
632+
--------
633+
Index.shift : Shift values of Index.
634+
PeriodIndex.shift : Shift values of PeriodIndex.
635+
"""
636+
result = self._eadata._time_shift(periods, freq=freq)
609637
return type(self)(result, name=self.name)
610638

611639

pandas/core/indexes/datetimes.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,6 @@ def __new__(cls, data=None,
299299

300300
# - Cases checked above all return/raise before reaching here - #
301301

302-
# This allows to later ensure that the 'copy' parameter is honored:
303-
if isinstance(data, Index):
304-
ref_to_data = data._data
305-
else:
306-
ref_to_data = data
307-
308302
if name is None and hasattr(data, 'name'):
309303
name = data.name
310304

@@ -314,8 +308,7 @@ def __new__(cls, data=None,
314308

315309
subarr = cls._simple_new(dtarr._data, name=name,
316310
freq=dtarr.freq, tz=dtarr.tz)
317-
318-
return subarr._deepcopy_if_needed(ref_to_data, copy)
311+
return subarr
319312

320313
@classmethod
321314
def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):

pandas/tests/arithmetic/test_datetime64.py

+2
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,8 @@ def check(get_ser, test_ser):
15961596
# with 'operate' (from core/ops.py) for the ops that are not
15971597
# defined
15981598
op = getattr(get_ser, op_str, None)
1599+
# Previously, _validate_for_numeric_binop in core/indexes/base.py
1600+
# did this for us.
15991601
with pytest.raises(TypeError,
16001602
match='operate|[cC]annot|unsupported operand'):
16011603
op(test_ser)

pandas/tests/indexes/timedeltas/test_astype.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ def test_astype_raises(self, dtype):
8888
with pytest.raises(TypeError, match=msg):
8989
idx.astype(dtype)
9090

91-
@pytest.mark.parametrize('tz', [None, 'US/Central'])
92-
def test_astype_category(self, tz):
93-
obj = pd.date_range("2000", periods=2, tz=tz)
91+
def test_astype_category(self):
92+
obj = pd.timedelta_range("1H", periods=2, freq='H')
93+
9494
result = obj.astype('category')
95-
expected = pd.CategoricalIndex([pd.Timestamp('2000-01-01', tz=tz),
96-
pd.Timestamp('2000-01-02', tz=tz)])
95+
expected = pd.CategoricalIndex([pd.Timedelta('1H'),
96+
pd.Timedelta('2H')])
9797
tm.assert_index_equal(result, expected)
9898

9999
# TODO: Use \._data following composition changeover

0 commit comments

Comments
 (0)