Skip to content

Commit cc67b72

Browse files
ajenkins-cargometricsjreback
authored andcommitted
BUG: Preserve timezone in unaligned assignments
closes #12981 Author: ajenkins-cargometrics <[email protected]> Closes #12982 from ajenkins-cargometrics/GH12981 and squashes the following commits: 6689f57 [ajenkins-cargometrics] TST: Add test with mask on LHS for test_setitem_with_unaligned_tz_aware_datetime_column 1347398 [ajenkins-cargometrics] BUG: Preserve timezone in unaligned assignments
1 parent 2fd0a06 commit cc67b72

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/whatsnew/v0.18.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ Bug Fixes
387387

388388

389389
- Bug in ``read_csv`` with the C engine when specifying ``skiprows`` with newlines in quoted items (:issue:`10911`, `12775`)
390+
- Bug in ``DataFrame`` timezone lost when assigning tz-aware datetime ``Series`` with alignment (:issue `12981`)
390391

391392

392393

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,20 @@ 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+
# GH 12981
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+
1953+
df = pd.DataFrame({'dates': column})
1954+
df.loc[[0, 1, 2], 'dates'] = column[[1, 0, 2]]
1955+
assert_series_equal(df['dates'], column)
1956+
19431957
def test_setitem_datetime_coercion(self):
19441958
# GH 1048
19451959
df = pd.DataFrame({'c': [pd.Timestamp('2010-10-01')] * 3})
@@ -1949,7 +1963,7 @@ def test_setitem_datetime_coercion(self):
19491963
df.loc[2, 'c'] = date(2005, 5, 5)
19501964
self.assertEqual(pd.Timestamp('2005-05-05'), df.loc[2, 'c'])
19511965

1952-
def test_datetimelike_setitem_with_inference(self):
1966+
def test_setitem_datetimelike_with_inference(self):
19531967
# GH 7592
19541968
# assignment of timedeltas with NaT
19551969

0 commit comments

Comments
 (0)