-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: appending a Timedelta to Series incorrectly casts to integer #27303
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
Conversation
pandas/core/indexing.py
Outdated
[self.obj._values, new_values] | ||
) | ||
except TypeError: | ||
as_obj = self.obj.astype(object) |
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.
Was this hitting the except clause before? Not super familiar with this code but seems like some duplication we could ideally clean up / refactor
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.
It was not raising in the relevant case, and the np.concatenate would cast the timedelta64 to int64.
seems like some duplication we could ideally clean up / refactor
Yes, this method in particular is in dire need of a refactor. ATM I'm focused on Block._try_coerce_arg
and Block._can_hold_element
(see other open PRs) and I think/hope once some inconsistencies in those are ironed out, some of the special-casing here may be unnecessary.
# GH#22717 inserting a Timedelta should _not_ cast to int64 | ||
ser = pd.Series(["x"]) | ||
ser["td"] = pd.Timedelta("9 days") | ||
assert isinstance(ser["td"], pd.Timedelta) |
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.
Lost previous comment but can you use tm.assert_series_equal
here? Also move to a separate test (test_timedelta_assignment_to_object
?)
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.
will do
@@ -653,6 +653,15 @@ def test_timedelta_assignment(): | |||
expected.loc[[1, 2, 3]] = pd.Timedelta(np.timedelta64(20, "m")) | |||
tm.assert_series_equal(s, expected) | |||
|
|||
# GH#22717 inserting a Timedelta should _not_ cast to int64 |
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.
new test pls & parameterize over timedelta & np.timedelta64 as wel
@@ -653,6 +653,15 @@ def test_timedelta_assignment(): | |||
expected.loc[[1, 2, 3]] = pd.Timedelta(np.timedelta64(20, "m")) | |||
tm.assert_series_equal(s, expected) |
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.
should really put this in a test_timedelta.py (and take the existing tests out of test_indexing).
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.
after the current batch of PRs I'm planning on doing a review of the indexing tests. There are multiple dimensions along which we can sort/parametrize, any of which would be reasonable, but my guess is we are not being consistent about it.
# GH#22717 inserting a Timedelta should _not_ cast to int64 | ||
ser = pd.Series(["x"]) | ||
ser["td"] = pd.Timedelta("9 days") | ||
assert isinstance(ser["td"], pd.Timedelta) |
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.
can you compare vs the expected series instead
thanks, pls add the comments to list of followups |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff