Skip to content

BUG: Subtraction fails with matching index of different name and type (pandas-dev#57524) #57680

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
Show file tree
Hide file tree
Changes from all 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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ Bug fixes
- Fixed bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
- Fixed bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`)
- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
- Fixed bug in :meth:`take` allowing ``indexer`` argument to be None (:issue:`57524`)

Categorical
^^^^^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,10 @@ def take(
if not is_array_like(arr):
arr = np.asarray(arr)

indices = ensure_platform_int(indices)
if indices is None:
indices = np.arange(arr.shape[axis], dtype=np.intp)
else:
indices = ensure_platform_int(indices)

if allow_fill:
# Pandas style, -1 means NA
Expand Down
88 changes: 88 additions & 0 deletions pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,94 @@ def test_add_list_to_masked_array_boolean(self, request):
result = [True, None, True] + ser
tm.assert_series_equal(result, expected)

def test_arithmetic_same_index_same_dtype(self):
s1 = Series(
[23, 22, 21],
index=Index(["a", "b", "c"], name="index a", dtype=pd.StringDtype()),
dtype=pd.Int64Dtype(),
)

s2 = Series(
[21, 22, 23],
index=Index(["a", "b", "c"], name="index a", dtype=pd.StringDtype()),
dtype=pd.Int64Dtype(),
)
expected_result = Series(
[2, 0, -2],
index=Index(["a", "b", "c"], name="index a", dtype=pd.StringDtype()),
dtype=pd.Int64Dtype(),
)

result = s1 - s2

tm.assert_series_equal(result, expected_result)

def test_arithmetic_different_index_same_dtype(self):
s1 = Series(
[23, 22, 21],
index=Index(["a", "b", "c"], name="index a", dtype=pd.StringDtype()),
dtype=pd.Int64Dtype(),
)

s2 = Series(
[21, 22, 23],
index=Index(["a", "b", "c"], name="index b", dtype=pd.StringDtype()),
dtype=pd.Int64Dtype(),
)
expected_result = Series(
[2, 0, -2],
index=Index(["a", "b", "c"], name="index a", dtype=pd.StringDtype()),
dtype=pd.Int64Dtype(),
)

result = s1 - s2

tm.assert_series_equal(result, expected_result)

def test_arithmetic_same_index_different_dtype(self):
s1 = Series(
[23, 22, 21],
index=Index(["a", "b", "c"], name="index a", dtype=pd.StringDtype()),
dtype=pd.Int64Dtype(),
)

s2 = Series(
[21, 22, 23],
index=Index(["a", "b", "c"], name="index a"),
dtype=pd.Int64Dtype(),
)
expected_result = Series(
[2, 0, -2],
index=Index(["a", "b", "c"], name="index a", dtype=pd.StringDtype()),
dtype=pd.Int64Dtype(),
)

result = s1 - s2

tm.assert_series_equal(result, expected_result)

def test_arithmetic_different_index_different_dtype(self):
s1 = Series(
[23, 22, 21],
index=Index(["a", "b", "c"], name="index a", dtype=pd.StringDtype()),
dtype=pd.Int64Dtype(),
)

s2 = Series(
[21, 22, 23],
index=Index(["a", "b", "c"], name="index b"),
dtype=pd.Int64Dtype(),
)
expected_result = Series(
[2, 0, -2],
index=Index(["a", "b", "c"], name="index a", dtype=pd.StringDtype()),
dtype=pd.Int64Dtype(),
)

result = s1 - s2

tm.assert_series_equal(result, expected_result)


# ------------------------------------------------------------------
# Comparisons
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_take.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ def test_take_non_hashable_fill_value(self):
expected = np.array([2, [1]], dtype=object)
tm.assert_numpy_array_equal(result, expected)

def test_take_indices_is_None(self):
arr = np.array([1, 2, 3])
indexer = None
result = algos.take(arr, indexer)
tm.assert_numpy_array_equal(result, arr)


class TestExtensionTake:
# The take method found in pd.api.extensions
Expand Down