-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix multiplying Timedelta Series with a pandas nullable dtype Series #58375
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,8 +41,10 @@ | |
|
||
from pandas.core.dtypes.common import ( | ||
TD64NS_DTYPE, | ||
is_bool_dtype, | ||
is_float_dtype, | ||
is_integer_dtype, | ||
is_numeric_dtype, | ||
is_object_dtype, | ||
is_scalar, | ||
is_string_dtype, | ||
|
@@ -492,6 +494,16 @@ def __mul__(self, other) -> Self: | |
result = np.array(result) | ||
return type(self)._simple_new(result, dtype=result.dtype) | ||
|
||
if is_bool_dtype(other.dtype) or is_numeric_dtype(other.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. instead of doing this here, check for BaseMaskedArray and return NotImplemented |
||
# this multiplication will succeed only if all elements of other | ||
# are any of the pandas nullable dtypes ('Int8', 'Int16', 'Int32', | ||
# 'Int64', 'UInt8', 'UInt16', 'UInt32', 'UInt64', 'Float32', | ||
# 'Float64', or 'boolean'), so we will end up with | ||
# timedelta64[ns]-dtyped result | ||
result = [self._ndarray[n] * other[n] for n in range(len(self))] | ||
result = np.array(result) | ||
return type(self)(result) | ||
|
||
# numpy will accept float or int dtype, raise TypeError for others | ||
result = self._ndarray * other | ||
return type(self)._simple_new(result, dtype=result.dtype) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -368,6 +368,45 @@ def test_add_list_to_masked_array_boolean(self, request): | |
result = [True, None, True] + ser | ||
tm.assert_series_equal(result, expected) | ||
|
||
def test_mul_nullable_dtype(self): | ||
# GH#58054. Multiplying a TimeDelta Series with another series containing | ||
# any of the Pandas nullable dtypes should work the same as with the | ||
# Numpy nullable dtypes | ||
td_series = Series([timedelta(hours=1)]) | ||
other = Series([True]) | ||
|
||
pandas_types = [ | ||
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. use a fixture or pytest.mark.parametrize |
||
"Int8", | ||
"Int16", | ||
"Int32", | ||
"Int64", | ||
"UInt8", | ||
"UInt16", | ||
"UInt32", | ||
"UInt64", | ||
"Float32", | ||
"Float64", | ||
"boolean", | ||
] | ||
numpy_types = [ | ||
"int8", | ||
"int16", | ||
"int32", | ||
"int64", | ||
"uint8", | ||
"uint16", | ||
"uint32", | ||
"uint64", | ||
"float32", | ||
"float64", | ||
"bool", | ||
] | ||
|
||
for dtype1, dtype2 in zip(pandas_types, numpy_types): | ||
result = td_series * other.astype(dtype1) | ||
expected = td_series * other.astype(dtype2) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
# ------------------------------------------------------------------ | ||
# Comparisons | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
don't reference _simple_new or even TimedeltaArray, just the relevant dtypes in Series/DataFrame