-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 1 commit
5a9b1b9
601cf54
33f8651
59069b9
edcb4e2
c48fbe9
1b9649c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can find a home for this in another test file (and don't need a new one). In addition, the comment should be under the class declaration. @jreback : I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good, should they go under There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same class sounds good for now. |
||
|
||
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) |
There was a problem hiding this comment.
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