File tree 2 files changed +31
-2
lines changed
2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -2868,9 +2868,15 @@ def reindexer(value):
2868
2868
value = maybe_infer_to_datetimelike (value )
2869
2869
2870
2870
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
2872
2876
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 )
2874
2880
2875
2881
# return internal types directly
2876
2882
if is_extension_type (value ) or is_extension_array_dtype (value ):
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments