Skip to content

ENH: consistently cast strings for DTA/TDA/PA.__setitem__ #36261

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 4 commits into from
Sep 12, 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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ Datetimelike
- Bug in :meth:`DatetimeIndex.get_slice_bound` where ``datetime.date`` objects were not accepted or naive :class:`Timestamp` with a tz-aware :class:`DatetimeIndex` (:issue:`35690`)
- Bug in :meth:`DatetimeIndex.slice_locs` where ``datetime.date`` objects were not accepted (:issue:`34077`)
- Bug in :meth:`DatetimeIndex.searchsorted`, :meth:`TimedeltaIndex.searchsorted`, :meth:`PeriodIndex.searchsorted`, and :meth:`Series.searchsorted` with ``datetime64``, ``timedelta64`` or ``Period`` dtype placement of ``NaT`` values being inconsistent with ``NumPy`` (:issue:`36176`,:issue:`36254`)
- Inconsistency in :class:`DatetimeArray`, :class:`TimedeltaArray`, and :class:`PeriodArray` setitem casting arrays of strings to datetimelike scalars but not scalar strings (:issue:`36261`)
-

Timedelta
^^^^^^^^^
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,7 @@ def _validate_setitem_value(self, value):
if is_list_like(value):
value = self._validate_listlike(value, "setitem", cast_str=True)
else:
# TODO: cast_str for consistency?
value = self._validate_scalar(value, msg, cast_str=False)
value = self._validate_scalar(value, msg, cast_str=True)

return self._unbox(value, setitem=True)

Expand Down
31 changes: 26 additions & 5 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pytest
import pytz

from pandas._libs import OutOfBoundsDatetime
from pandas.compat.numpy import np_version_under1p18
Expand Down Expand Up @@ -282,15 +283,35 @@ def test_setitem(self):
expected[:2] = expected[-2:]
tm.assert_numpy_array_equal(arr.asi8, expected)

def test_setitem_str_array(self, arr1d):
if isinstance(arr1d, DatetimeArray) and arr1d.tz is not None:
pytest.xfail(reason="timezone comparisons inconsistent")
def test_setitem_strs(self, arr1d):
# Check that we parse strs in both scalar and listlike
if isinstance(arr1d, DatetimeArray):
tz = arr1d.tz
if (
tz is not None
and tz is not pytz.UTC
and not isinstance(tz, pytz._FixedOffset)
):
# If we have e.g. tzutc(), when we cast to string and parse
# back we get pytz.UTC, and then consider them different timezones
# so incorrectly raise.
pytest.xfail(reason="timezone comparisons inconsistent")

# Setting list-like of strs
expected = arr1d.copy()
expected[[0, 1]] = arr1d[-2:]

arr1d[:2] = [str(x) for x in arr1d[-2:]]
result = arr1d.copy()
result[:2] = [str(x) for x in arr1d[-2:]]
tm.assert_equal(result, expected)

tm.assert_equal(arr1d, expected)
# Same thing but now for just a scalar str
expected = arr1d.copy()
expected[0] = arr1d[-1]

result = arr1d.copy()
result[0] = str(arr1d[-1])
tm.assert_equal(result, expected)

@pytest.mark.parametrize("as_index", [True, False])
def test_setitem_categorical(self, arr1d, as_index):
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,29 @@ def test_tz_setter_raises(self):
with pytest.raises(AttributeError, match="tz_localize"):
arr.tz = "UTC"

def test_setitem_str_impute_tz(self, tz_naive_fixture):
# Like for getitem, if we are passed a naive-like string, we impute
# our own timezone.
tz = tz_naive_fixture

data = np.array([1, 2, 3], dtype="M8[ns]")
dtype = data.dtype if tz is None else DatetimeTZDtype(tz=tz)
arr = DatetimeArray(data, dtype=dtype)
expected = arr.copy()

ts = pd.Timestamp("2020-09-08 16:50").tz_localize(tz)
setter = str(ts.tz_localize(None))

# Setting a scalar tznaive string
expected[0] = ts
arr[0] = setter
tm.assert_equal(arr, expected)

# Setting a listlike of tznaive strings
expected[1] = ts
arr[:2] = [setter, setter]
tm.assert_equal(arr, expected)

def test_setitem_different_tz_raises(self):
data = np.array([1, 2, 3], dtype="M8[ns]")
arr = DatetimeArray(data, copy=False, dtype=DatetimeTZDtype(tz="US/Central"))
Expand Down