Skip to content

BUG: Series.__setitem__ with datetimetz data #25331

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

Closed
Closed
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: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Interval
Indexing
^^^^^^^^

-
- Bug when setting a value to a :class:`Series` with timezone aware values and an :class:`Index` of tuples or strings (:issue:`12862`, :issue:`20441`)
-
-

Expand Down
6 changes: 5 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,11 @@ def setitem(key, value):
self._maybe_update_cacher()

def _set_with_engine(self, key, value):
values = self._values
if is_extension_array_dtype(self):
# GH 20441: set_value expects and ndarray, not ExtensionArray
Copy link
Contributor

Choose a reason for hiding this comment

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

and -> an, but can't we just do: .to_numpy() ?

values = self.values
else:
values = self._values
try:
self.index._engine.set_value(values, key, value)
return
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,3 +839,24 @@ def test_head_tail(test_data):
assert_series_equal(test_data.series.head(0), test_data.series[0:0])
assert_series_equal(test_data.series.tail(), test_data.series[-5:])
assert_series_equal(test_data.series.tail(0), test_data.series[0:0])


def test_setitem_tuple_with_datetimetz():
# GH 20441
arr = pd.date_range('2017', periods=4, tz='US/Eastern')
index = [(0, 1), (0, 2), (0, 3), (0, 4)]
result = Series(arr, index=index)
expected = result.copy()
result[(0, 1)] = np.nan
expected.iloc[0] = np.nan
assert_series_equal(result, expected)


def test_setitem_str_with_datetimetz():
# GH 12862
result = pd.Series()
Copy link
Member

Choose a reason for hiding this comment

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

nit: use Series directly instead of pd.Series (you do so in your first test)

result['foo'] = pd.to_datetime(1000).tz_localize('UTC')
result['bar'] = pd.to_datetime(1001).tz_localize('UTC')
expected = pd.Series(pd.to_datetime([1000, 1001], utc=True),
index=['foo', 'bar'])
assert_series_equal(result, expected)