Skip to content
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

fix: scalar timestamp assignment (#19843) #19973

Merged
merged 7 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ Datetimelike
- Bug in :func:`to_datetime` where passing an out-of-bounds datetime with ``errors='coerce'`` and ``utc=True`` would raise ``OutOfBoundsDatetime`` instead of parsing to ``NaT`` (:issue:`19612`)
- Bug in :class:`DatetimeIndex` and :class:`TimedeltaIndex` addition and subtraction where name of the returned object was not always set consistently. (:issue:`19744`)
- Bug in :class:`DatetimeIndex` and :class:`TimedeltaIndex` addition and subtraction where operations with numpy arrays raised ``TypeError`` (:issue:`19847`)
- Bug in `DataFrame` assignment with a timezone-aware object (:issue:`19843`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use double-backticks are DataFrame

say a timezone-aware scalar


Timedelta
^^^^^^^^^
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import at the top of the file with other imports

pd_dtype, _ = infer_dtype_from_scalar(value, pandas_dtype=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call the return dtype


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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need the blank line and the comment here

# 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
16 changes: 16 additions & 0 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

from pandas.tests.frame.common import TestData

from pandas.core.dtypes.dtypes import DatetimeTZDtype


class TestDataFrameIndexing(TestData):

Expand Down Expand Up @@ -3042,6 +3044,20 @@ def test_transpose(self):
expected.index = ['A', 'B']
assert_frame_equal(result, expected)

def test_scalar_assignment(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference issue number for both tests.

# issue #19843
df = pd.DataFrame(index=(0, 1, 2))
df['now'] = pd.Timestamp('20130101', tz='UTC')
assert isinstance(df.dtypes[0], DatetimeTZDtype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

construct the expected frame and use assert_frame_equal

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put this in class TestDataFrameIndexingDatetimeWithTZ(TestData)

name it test_setitem_scalar

parameterize it with both a tz and a tz-aware value.


def test_datetime_index_assignment(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a duplicate test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could put it as an additional test in the class as above if you'd like (parametrize as well)

# 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)


class TestDataFrameIndexingUInt64(TestData):

Expand Down