Skip to content

Commit

Permalink
fix: scalar timestamp assignment (pandas-dev#19843)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanDmitri committed Mar 2, 2018
1 parent a7a7f8c commit 5a9b1b9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2868,9 +2868,15 @@ def reindexer(value):
value = maybe_infer_to_datetimelike(value)

else:
# upcast the scalar
# cast ignores pandas dtypes. so save the dtype first
from pandas.core.dtypes.cast import infer_dtype_from_scalar
pd_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)

# then add dtype back in
value = maybe_cast_to_datetime(value, pd_dtype)

# return internal types directly
if is_extension_type(value) or is_extension_array_dtype(value):
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/scalar/timestamp/test_assignment_dtype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pandas as pd
from pandas.core.dtypes.dtypes import DatetimeTZDtype


# referencing #19843: scalar assignment of a tz-aware is object dtype

class TestTimestampProperties(object):

def test_scalar_assignment(self):
df = pd.DataFrame(index=(0, 1, 2))

df['now'] = pd.Timestamp('20130101', tz='UTC')

assert isinstance(df.dtypes[0], DatetimeTZDtype)

def test_datetime_index_assignment(self):
df = pd.DataFrame(index=(0, 1, 2))

di = pd.DatetimeIndex(
[pd.Timestamp('20130101', tz='UTC')]).repeat(len(df))
df['now'] = di

assert isinstance(df.dtypes[0], DatetimeTZDtype)

0 comments on commit 5a9b1b9

Please sign in to comment.