Skip to content

Commit 1347398

Browse files
BUG: Preserve timezone in unaligned assignments
A fix for GH12981. When doing a slice assign to a DataFrame where the right-hand-side was timezone-aware datetime Series which required realignment to perform the assignment, the timezone would be stripped from the RHS on assignment.
1 parent 2fd0a06 commit 1347398

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v0.18.1.txt

+3
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,6 @@ Bug Fixes
433433
- Bug where ``loffset`` argument was not applied when calling ``resample().count()`` on a timeseries (:issue:`12725`)
434434
- ``pd.read_excel()`` now accepts path objects (e.g. ``pathlib.Path``, ``py.path.local``) for the file path, in line with other ``read_*`` functions (:issue:`12655`)
435435
- ``pd.read_excel()`` now accepts column names associated with keyword argument ``names``(:issue `12870`)
436+
437+
- Bug in ``DataFrame._santize_column`` now preserves Timezone when RHS is a Series (:issue `12981`)
438+

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2538,7 +2538,7 @@ def reindexer(value):
25382538

25392539
# GH 4107
25402540
try:
2541-
value = value.reindex(self.index).values
2541+
value = value.reindex(self.index)._values
25422542
except Exception as e:
25432543

25442544
# duplicate axis

pandas/tests/frame/test_indexing.py

+10
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,16 @@ def test_setitem_with_unaligned_sparse_value(self):
19401940
exp = pd.Series([1, 0, 0], name='new_column')
19411941
assert_series_equal(df['new_column'], exp)
19421942

1943+
def test_setitem_with_unaligned_tz_aware_datetime_column(self):
1944+
# GH12981
1945+
# Assignment of unaligned offset-aware datetime series.
1946+
# Make sure timezone isn't lost
1947+
column = pd.Series(pd.date_range('2015-01-01', periods=3, tz='utc'),
1948+
name='dates')
1949+
df = pd.DataFrame({'dates': column})
1950+
df['dates'] = column[[1, 0, 2]]
1951+
assert_series_equal(df['dates'], column)
1952+
19431953
def test_setitem_datetime_coercion(self):
19441954
# GH 1048
19451955
df = pd.DataFrame({'c': [pd.Timestamp('2010-10-01')] * 3})

0 commit comments

Comments
 (0)