Skip to content

Requested Follow-Ups #33856

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 6 commits into from
Apr 30, 2020
Merged
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ Datetimelike
- Bug in :meth:`DatetimeIndex.tz_localize` incorrectly retaining ``freq`` in some cases where the original freq is no longer valid (:issue:`30511`)
- Bug in :meth:`DatetimeIndex.intersection` losing ``freq`` and timezone in some cases (:issue:`33604`)
- Bug in :class:`DatetimeIndex` addition and subtraction with some types of :class:`DateOffset` objects incorrectly retaining an invalid ``freq`` attribute (:issue:`33779`)
- Bug in :class:`DatetimeIndex` where setting the ``freq`` attribute on an index could silently change the ``freq`` attribute on another index viewing the same data (:issue:`33552`)

Timedelta
^^^^^^^^^
Expand All @@ -570,6 +571,7 @@ Numeric
- Bug in :meth:`DataFrame.count` with ``level="foo"`` and index level ``"foo"`` containing NaNs causes segmentation fault (:issue:`21824`)
- Bug in :meth:`DataFrame.diff` with ``axis=1`` returning incorrect results with mixed dtypes (:issue:`32995`)
- Bug in :meth:`DataFrame.corr` and :meth:`DataFrame.cov` raising when handling nullable integer columns with ``pandas.NA`` (:issue:`33803`)
- Bug in :class:`DataFrame` and :class:`Series` addition and subtraction between object-dtype objects and ``datetime64`` dtype objects (:issue:`33824`)

Conversion
^^^^^^^^^^
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/array_algos/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
def shift(values: np.ndarray, periods: int, axis: int, fill_value) -> np.ndarray:
new_values = values

if periods == 0:
# TODO: should we copy here?
return new_values
if periods == 0 or values.size == 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

do we have a test on the copy itself? in the consumer of this code

Copy link
Member Author

Choose a reason for hiding this comment

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

disabling the copy here doesnt break any tests, so looks like "no". will add one

return new_values.copy()

# make sure array sent to np.roll is c_contiguous
f_ordered = values.flags.f_contiguous
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ def shift(self, periods, fill_value=None):

fill_value = self._validate_fill_value(fill_value)

codes = shift(codes.copy(), periods, axis=0, fill_value=fill_value)
codes = shift(codes, periods, axis=0, fill_value=fill_value)

return self._constructor(codes, dtype=self.dtype, fastpath=True)

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,6 @@ def _values_for_argsort(self):

@Appender(ExtensionArray.shift.__doc__)
def shift(self, periods=1, fill_value=None, axis=0):
if not self.size or periods == 0:
return self.copy()

fill_value = self._validate_shift_value(fill_value)
new_values = shift(self._data, periods, axis, fill_value)
Expand Down Expand Up @@ -745,7 +743,9 @@ def _validate_shift_value(self, fill_value):
# TODO(2.0): once this deprecation is enforced, used _validate_fill_value
if is_valid_nat_for_dtype(fill_value, self.dtype):
fill_value = NaT
elif not isinstance(fill_value, self._recognized_scalars):
elif isinstance(fill_value, self._recognized_scalars):
fill_value = self._scalar_type(fill_value)
else:
# only warn if we're not going to raise
if self._scalar_type is Period and lib.is_integer(fill_value):
# kludge for #31971 since Period(integer) tries to cast to str
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,12 @@ def diff(self, n: int, axis: int = 0) -> List["Block"]:
# Cannot currently calculate diff across multiple blocks since this
# function is invoked via apply
raise NotImplementedError
new_values = (self.values - self.shift(n, axis=axis)[0].values).asi8

if n == 0:
# Fastpath avoids making a copy in `shift`
new_values = np.zeros(self.values.shape, dtype=np.int64)
else:
new_values = (self.values - self.shift(n, axis=axis)[0].values).asi8

# Reshape the new_values like how algos.diff does for timedelta data
new_values = new_values.reshape(1, len(new_values))
Expand Down
34 changes: 34 additions & 0 deletions pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,40 @@ def test_searchsorted_invalid_types(self, other, index):
with pytest.raises(TypeError, match=msg):
arr.searchsorted(other)

def test_shift_fill_value(self):
dti = pd.date_range("2016-01-01", periods=3)

dta = dti._data
expected = DatetimeArray(np.roll(dta._data, 1))

fv = dta[-1]
for fill_value in [fv, fv.to_pydatetime(), fv.to_datetime64()]:
result = dta.shift(1, fill_value=fill_value)
tm.assert_datetime_array_equal(result, expected)

dta = dta.tz_localize("UTC")
expected = expected.tz_localize("UTC")
fv = dta[-1]
for fill_value in [fv, fv.to_pydatetime()]:
result = dta.shift(1, fill_value=fill_value)
tm.assert_datetime_array_equal(result, expected)

def test_shift_value_tzawareness_mismatch(self):
dti = pd.date_range("2016-01-01", periods=3)

dta = dti._data

fv = dta[-1].tz_localize("UTC")
for invalid in [fv, fv.to_pydatetime()]:
with pytest.raises(TypeError, match="Cannot compare"):
dta.shift(1, fill_value=invalid)

dta = dta.tz_localize("UTC")
fv = dta[-1].tz_localize(None)
for invalid in [fv, fv.to_pydatetime(), fv.to_datetime64()]:
with pytest.raises(TypeError, match="Cannot compare"):
dta.shift(1, fill_value=invalid)


class TestSequenceToDT64NS:
def test_tz_dtype_mismatch_raises(self):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/extension/base/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ def test_container_shift(self, data, frame, periods, indices):

compare(result, expected)

def test_shift_0_periods(self, data):
# GH#33856 shifting with periods=0 should return a copy, not same obj
result = data.shift(0)
assert data[0] != data[1] # otherwise below is invalid
data[0] = data[1]
assert result[0] != result[1] # i.e. not the same object/view

@pytest.mark.parametrize("periods", [1, -2])
def test_diff(self, data, periods):
data = data[:5]
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,13 @@ def test_searchsorted(self, data_for_sorting, as_series):
with tm.assert_produces_warning(PerformanceWarning):
super().test_searchsorted(data_for_sorting, as_series)

def test_shift_0_periods(self, data):
# GH#33856 shifting with periods=0 should return a copy, not same obj
result = data.shift(0)

data._sparse_values[0] = data._sparse_values[1]
assert result._sparse_values[0] != result._sparse_values[1]


class TestCasting(BaseSparseTests, base.BaseCastingTests):
def test_astype_object_series(self, all_data):
Expand Down