Skip to content

Commit

Permalink
fix: scalar timestamp assignment (pandas-dev#19843) (pandas-dev#19973)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanDmitri authored and dberenbaum committed Aug 3, 2018
1 parent ebc5a74 commit fd594cf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ Timezones
- Bug in :class:`DatetimeIndex` where constructing with an integer and tz would not localize correctly (:issue:`12619`)
- Fixed bug where :meth:`DataFrame.describe` and :meth:`Series.describe` on tz-aware datetimes did not show `first` and `last` result (:issue:`21328`)
- Bug in :class:`DatetimeIndex` comparisons failing to raise ``TypeError`` when comparing timezone-aware ``DatetimeIndex`` against ``np.datetime64`` (:issue:`22074`)
- Bug in ``DataFrame`` assignment with a timezone-aware scalar (:issue:`19843`)

Offsets
^^^^^^^
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
maybe_upcast,
cast_scalar_to_array,
construct_1d_arraylike_from_scalar,
infer_dtype_from_scalar,
maybe_cast_to_datetime,
maybe_infer_to_datetimelike,
maybe_convert_platform,
Expand Down Expand Up @@ -3507,9 +3508,13 @@ def reindexer(value):
value = maybe_infer_to_datetimelike(value)

else:
# upcast the scalar
# cast ignores pandas dtypes. so save the dtype first
infer_dtype, _ = infer_dtype_from_scalar(
value, pandas_dtype=True)

# upcast
value = cast_scalar_to_array(len(self.index), value)
value = maybe_cast_to_datetime(value, value.dtype)
value = maybe_cast_to_datetime(value, infer_dtype)

# return internal types directly
if is_extension_type(value) or is_extension_array_dtype(value):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3135,6 +3135,14 @@ def test_transpose(self):
expected.index = ['A', 'B']
assert_frame_equal(result, expected)

def test_scalar_assignment(self):
# issue #19843
df = pd.DataFrame(index=(0, 1, 2))
df['now'] = pd.Timestamp('20130101', tz='UTC')
expected = pd.DataFrame(
{'now': pd.Timestamp('20130101', tz='UTC')}, index=[0, 1, 2])
tm.assert_frame_equal(df, expected)


class TestDataFrameIndexingUInt64(TestData):

Expand Down

0 comments on commit fd594cf

Please sign in to comment.