8000 fix: scalar timestamp assignment (#19843) by DylanDmitri · Pull Request #19973 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

fix: scalar timestamp assignment (#19843) #19973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
clean
  • Loading branch information
jreback committed Aug 2, 2018
commit 1b9649c8957a9e0fdc25a8a1a2a4039eedfeba7e
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,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 object (:issue:`19843`)
- Bug in ``DataFrame`` assignment with a timezone-aware scalar (:issue:`19843`)

< 8000 /span>
Offsets
^^^^^^^
Expand Down
9 changes: 4 additions & 5 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 @@ -3508,14 +3509,12 @@ def reindexer(value):

else:
# 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)
infer_dtype, _ = infer_dtype_from_scalar(
value, pandas_dtype=True)

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

# then add dtype back in
value = maybe_cast_to_datetime(value, pd_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
14 changes: 3 additions & 11 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@

from pandas.tests.frame.common import TestData

from pandas.core.dtypes.dtypes import DatetimeTZDtype


class TestDataFrameIndexing(TestData):

Expand Down Expand Up @@ -3141,15 +3139,9 @@ def test_scalar_assignment(self):
# issue #19843
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):
# issue #19843
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)
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