Skip to content

Commit 5a9b1b9

Browse files
committed
fix: scalar timestamp assignment (pandas-dev#19843)
1 parent a7a7f8c commit 5a9b1b9

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

pandas/core/frame.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2868,9 +2868,15 @@ def reindexer(value):
28682868
value = maybe_infer_to_datetimelike(value)
28692869

28702870
else:
2871-
# upcast the scalar
2871+
# cast ignores pandas dtypes. so save the dtype first
2872+
from pandas.core.dtypes.cast import infer_dtype_from_scalar
2873+
pd_dtype, _ = infer_dtype_from_scalar(value, pandas_dtype=True)
2874+
2875+
# upcast
28722876
value = cast_scalar_to_array(len(self.index), value)
2873-
value = maybe_cast_to_datetime(value, value.dtype)
2877+
2878+
# then add dtype back in
2879+
value = maybe_cast_to_datetime(value, pd_dtype)
28742880

28752881
# return internal types directly
28762882
if is_extension_type(value) or is_extension_array_dtype(value):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pandas as pd
2+
from pandas.core.dtypes.dtypes import DatetimeTZDtype
3+
4+
5+
# referencing #19843: scalar assignment of a tz-aware is object dtype
6+
7+
class TestTimestampProperties(object):
8+
9+
def test_scalar_assignment(self):
10+
df = pd.DataFrame(index=(0, 1, 2))
11+
12+
df['now'] = pd.Timestamp('20130101', tz='UTC')
13+
14+
assert isinstance(df.dtypes[0], DatetimeTZDtype)
15+
16+
def test_datetime_index_assignment(self):
17+
df = pd.DataFrame(index=(0, 1, 2))
18+
19+
di = pd.DatetimeIndex(
20+
[pd.Timestamp('20130101', tz='UTC')]).repeat(len(df))
21+
df['now'] = di
22+
23+
assert isinstance(df.dtypes[0], DatetimeTZDtype)

0 commit comments

Comments
 (0)