Skip to content

API / BUG: copy non-Index arrays in Index construction to avoid data corruption #51930

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
20 changes: 15 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def __new__(
cls,
data=None,
dtype=None,
copy: bool = False,
copy: bool | None = None,
name=None,
tupleize_cols: bool = True,
) -> Index:
Expand All @@ -477,6 +477,12 @@ def __new__(

data_dtype = getattr(data, "dtype", None)

if copy is None:
if isinstance(data, Index):
copy = False
else:
copy = True

# range
if isinstance(data, (range, RangeIndex)):
result = RangeIndex(start=data, copy=copy, name=name)
Expand Down Expand Up @@ -6714,7 +6720,7 @@ def infer_objects(self, copy: bool = True) -> Index:
)
if copy and res_values is values:
return self.copy()
return Index(res_values, name=self.name)
return Index(res_values, name=self.name, copy=False)

# --------------------------------------------------------------------
# Generated Arithmetic, Comparison, and Unary Methods
Expand Down Expand Up @@ -7051,15 +7057,16 @@ def ensure_index_from_sequences(sequences, names=None) -> Index:
return MultiIndex.from_arrays(sequences, names=names)


def ensure_index(index_like: Axes, copy: bool = False) -> Index:
def ensure_index(index_like: Axes, copy: bool | None = None) -> Index:
"""
Ensure that we have an index from some index-like object.

Parameters
----------
index_like : sequence
An Index or other sequence
copy : bool, default False
copy : bool, optional
By default copy if the passed object is not yet an Index object.

Returns
-------
Expand All @@ -7083,10 +7090,13 @@ def ensure_index(index_like: Axes, copy: bool = False) -> Index:
)
"""
if isinstance(index_like, Index):
if copy:
if copy is True:
index_like = index_like.copy()
return index_like

if copy is None:
copy = True

if isinstance(index_like, ABCSeries):
name = index_like.name
return Index(index_like, name=name, copy=copy)
Expand Down
44 changes: 44 additions & 0 deletions pandas/tests/copy_view/test_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from pandas import (
DataFrame,
Index,
Series,
)
import pandas._testing as tm


def test_index_constructor():
ser = Series([1, 2, 3])
idx = Index(ser)
# assert not np.shares_memory(idx.values, get_array(ser))
ser.iloc[0] = 0
tm.assert_index_equal(idx, Index([1, 2, 3]))


def test_series_constructor():
ser = Series([1, 2, 3])
ser2 = Series(ser, index=ser)
ser2.iloc[0] = 0
tm.assert_index_equal(ser2.index, Index([1, 2, 3]))


def test_dataframe_constructor():
ser = Series([1, 2, 3])
df = DataFrame({"a": ser}, index=ser)
ser.iloc[0] = 0
tm.assert_index_equal(df.index, Index([1, 2, 3]))
df.iloc[0, 0] = 10
tm.assert_index_equal(df.index, Index([1, 2, 3]))


def test_dataframe_set_index_inplace():
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df.set_index("a", drop=False, inplace=True)
df.iloc[0, 0] = 0
tm.assert_index_equal(df.index, Index([1, 2, 3], name="a"))


def test_index_attribute_assignment():
ser = Series([1, 2, 3])
ser.index = ser
ser.iloc[0] = 10
tm.assert_index_equal(ser.index, Index([1, 2, 3]))
1 change: 1 addition & 0 deletions pandas/tests/reductions/test_stat_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def test_td64_mean(self, box):
assert result == expected

tdarr[0] = pd.NaT
obj = box(tdarr)
assert obj.mean(skipna=False) is pd.NaT

result2 = obj.mean(skipna=True)
Expand Down