Skip to content

added test case for iloc function if it returns the same output for b… #41028

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
wants to merge 2 commits into from
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
8 changes: 5 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
isna,
)

from pandas.core.arrays.integer import IntegerArray
import pandas.core.common as com
from pandas.core.construction import array as pd_array
from pandas.core.indexers import (
Expand Down Expand Up @@ -1588,10 +1589,11 @@ def _setitem_with_indexer(self, indexer, value, name="iloc"):
BlockManager methods, see GH#12991, GH#22046, GH#15686.
"""
info_axis = self.obj._info_axis_number

# maybe partial set
take_split_path = not self.obj._mgr.is_single_block

value = np.array(value) if isinstance(value, IntegerArray) else value

# if there is only one block/type, still have to take split path
# unless the block is one-dimensional or it can hold the value
if (
Expand Down Expand Up @@ -1705,7 +1707,6 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
"""
# Above we only set take_split_path to True for 2D cases
assert self.ndim == 2

if not isinstance(indexer, tuple):
indexer = _tuplify(self.ndim, indexer)
if len(indexer) > self.ndim:
Expand All @@ -1716,8 +1717,8 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
if (isinstance(value, ABCSeries) and name != "iloc") or isinstance(value, dict):
from pandas import Series

print("value is ABCseries")
value = self._align_series(indexer, Series(value))

# Ensure we have something we can iterate over
info_axis = indexer[1]
ilocs = self._ensure_iterable_column_indexer(info_axis)
Expand All @@ -1731,6 +1732,7 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
if is_list_like_indexer(value) and getattr(value, "ndim", 1) > 0:

if isinstance(value, ABCDataFrame):
print("value is ABCDataFrame")
self._setitem_with_indexer_frame_value(indexer, value, name)

elif np.ndim(value) == 2:
Expand Down
48 changes: 48 additions & 0 deletions pandas/tests/indexing/multiindex/test_iloc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest

import pandas as pd
from pandas import (
DataFrame,
MultiIndex,
Expand Down Expand Up @@ -73,6 +74,53 @@ def test_iloc_getitem_multiple_items():
tm.assert_frame_equal(result, expected)


def test_iloc_pd_arr_value():
# test if iloc returns the same output for numpy array input and integer array input
df = DataFrame(
data={
"col1": [1, 2, 3, 4],
"col2": [3, 4, 5, 6],
"col3": [6, 7, 8, 9],
}
)
df_np = df.copy()
df_pd = df.copy()
df_int = df.copy()

pd_arr = pd.array([1, 2, 3])
np_arr = np.array([1, 2, 3])
int_arr = [1, 2, 3]
df_pd.iloc[[1, 2, 3]] = pd_arr
df_np.iloc[[1, 2, 3]] = np_arr
df_int.iloc[[1, 2, 3]] = int_arr

tm.assert_frame_equal(df_pd, df_np) and tm.assert_frame_equal(df_int, df_np)


def test_iloc_pd_arr_value2():
# test if iloc returns the same output for numpy array input and integer array input
df = DataFrame(
data={
"col1": [1, 2, 3, 4],
"col2": [3, 4, 5, 6],
"col3": [6, 7, 8, 9],
}
)
df_np = df.copy()
df_pd = df.copy()
df_int = df.copy()

pd_arr = pd.array([1, 2, 3])
np_arr = np.array([1, 2, 3])
int_arr = [1, 2, 3]

df_pd.iloc[[1, 2, 3], :] = pd_arr
df_np.iloc[[1, 2, 3], :] = np_arr
df_int.iloc[[1, 2, 3], :] = int_arr

tm.assert_frame_equal(df_pd, df_np) and tm.assert_frame_equal(df_int, df_np)


def test_iloc_getitem_labels():
# this is basically regular indexing
arr = np.random.randn(4, 3)
Expand Down
1 change: 1 addition & 0 deletions pip
Submodule pip added at e6a65f