Skip to content

Commit 0cefff0

Browse files
jbrockmendeljreback
authored andcommitted
BUG: fix inserting tz-aware datetime to Series, closes #12862 (#27322)
1 parent 8887b1e commit 0cefff0

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ Indexing
10531053
- Bug in :meth:`DataFrame.loc` and :meth:`DataFrame.iloc` on a :class:`DataFrame` with a single timezone-aware datetime64[ns] column incorrectly returning a scalar instead of a :class:`Series` (:issue:`27110`)
10541054
- Bug in :class:`CategoricalIndex` and :class:`Categorical` incorrectly raising ``ValueError`` instead of ``TypeError`` when a list is passed using the ``in`` operator (``__contains__``) (:issue:`21729`)
10551055
- Bug in setting a new value in a :class:`Series` with a :class:`Timedelta` object incorrectly casting the value to an integer (:issue:`22717`)
1056+
- Bug in :class:`Series` setting a new key (``__setitem__``) with a timezone-aware datetime incorrectly raising ``ValueError`` (:issue:`12862`)
10561057
-
10571058
10581059
Missing

pandas/core/series.py

+7
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,13 @@ def _set_with(self, key, value):
12681268
except Exception:
12691269
pass
12701270

1271+
if is_scalar(key) and not is_integer(key) and key not in self.index:
1272+
# GH#12862 adding an new key to the Series
1273+
# Note: have to exclude integers because that is ambiguously
1274+
# position-based
1275+
self.loc[key] = value
1276+
return
1277+
12711278
if is_scalar(key):
12721279
key = [key]
12731280
elif not isinstance(key, (list, Series, np.ndarray)):

pandas/tests/indexing/test_loc.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ def test_loc_setitem_with_scalar_index(self, indexer, value):
802802

803803
assert is_scalar(result) and result == "Z"
804804

805-
def test_loc_coerceion(self):
805+
def test_loc_coercion(self):
806806

807807
# 12411
808808
df = DataFrame({"date": [Timestamp("20130101").tz_localize("UTC"), pd.NaT]})
@@ -838,6 +838,26 @@ def test_loc_coerceion(self):
838838
result = df.iloc[3:]
839839
tm.assert_series_equal(result.dtypes, expected)
840840

841+
def test_setitem_new_key_tz(self):
842+
# GH#12862 should not raise on assigning the second value
843+
vals = [
844+
pd.to_datetime(42).tz_localize("UTC"),
845+
pd.to_datetime(666).tz_localize("UTC"),
846+
]
847+
expected = pd.Series(vals, index=["foo", "bar"])
848+
849+
ser = pd.Series()
850+
ser["foo"] = vals[0]
851+
ser["bar"] = vals[1]
852+
853+
tm.assert_series_equal(ser, expected)
854+
855+
ser = pd.Series()
856+
ser.loc["foo"] = vals[0]
857+
ser.loc["bar"] = vals[1]
858+
859+
tm.assert_series_equal(ser, expected)
860+
841861
def test_loc_non_unique(self):
842862
# GH3659
843863
# non-unique indexer with loc slice

pandas/tests/series/indexing/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ def test_setitem_with_tz_dst():
523523
tm.assert_series_equal(s, exp)
524524

525525

526-
def test_categorial_assigning_ops():
526+
def test_categorical_assigning_ops():
527527
orig = Series(Categorical(["b", "b"], categories=["a", "b"]))
528528
s = orig.copy()
529529
s[:] = "a"

0 commit comments

Comments
 (0)