Skip to content
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

BUG: df[col] = arr should not overwrite data in df[col] #35417

Closed
wants to merge 150 commits into from
Closed
Show file tree
Hide file tree
Changes from 146 commits
Commits
Show all changes
150 commits
Select commit Hold shift + click to select a range
a1ce4fc
Make df[col] insert a new array, never overwrite
jbrockmendel Jul 26, 2020
b1913b7
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Jul 26, 2020
e600237
whatsnew
TomAugspurger Jul 27, 2020
140f5f2
update tests
jbrockmendel Jul 28, 2020
c096c5d
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Jul 28, 2020
cbd45e8
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Jul 30, 2020
989ba97
update whatsnew
jbrockmendel Jul 30, 2020
bf6e5f5
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Aug 5, 2020
a5ffd10
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Aug 8, 2020
e126ab5
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Aug 12, 2020
8c4f9f3
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Aug 13, 2020
f716904
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Aug 15, 2020
59e447a
Update merge test
jbrockmendel Aug 15, 2020
09e89ee
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Aug 18, 2020
11b8093
Fix cython groubpy.apply func for funcs that mutate inplace
jbrockmendel Aug 18, 2020
4271303
cleanup commented-out
jbrockmendel Aug 18, 2020
ed0af51
mypy fixup
jbrockmendel Aug 18, 2020
dffae7e
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Aug 26, 2020
a7f363b
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Aug 27, 2020
53992df
Test for #35731
jbrockmendel Aug 27, 2020
0a133d7
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Aug 31, 2020
db1b668
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Sep 2, 2020
858d6cb
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Sep 7, 2020
3deb0a7
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Sep 14, 2020
68e8715
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Sep 14, 2020
3a74222
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Sep 22, 2020
b0896be
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Sep 22, 2020
f9498a3
revert accidental
jbrockmendel Oct 11, 2020
d81a696
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 11, 2020
33ef890
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 12, 2020
ea49ae2
move whatsnew note
jbrockmendel Oct 12, 2020
1a850c8
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 12, 2020
fed4782
update version ref
jbrockmendel Oct 12, 2020
0633cc6
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 13, 2020
a00702a
restore
jbrockmendel Oct 13, 2020
1dd58ab
split_path compat
jbrockmendel Oct 14, 2020
4ffe7f4
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 16, 2020
247d0f8
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 16, 2020
9164c83
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 22, 2020
080c7e9
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 22, 2020
b47ed01
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 23, 2020
0290975
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 23, 2020
13b683d
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 23, 2020
7e9ea2d
lint fixup
jbrockmendel Oct 23, 2020
bd55d67
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 25, 2020
ae9c707
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 26, 2020
4dd24ca
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 26, 2020
253f625
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Oct 31, 2020
0b28fc0
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Nov 2, 2020
9c5e6fa
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Nov 17, 2020
2e0bf61
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Nov 23, 2020
04af8fa
doc fixup, split tests
jbrockmendel Nov 23, 2020
1d2b6d7
update test expected
jbrockmendel Nov 23, 2020
65d466b
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Nov 25, 2020
13077e9
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Nov 26, 2020
f013ec3
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 3, 2020
66d5ed0
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 4, 2020
f23245b
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 5, 2020
23e9462
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 6, 2020
6c8b15f
fix broken test
jbrockmendel Dec 7, 2020
03b3015
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 7, 2020
687d262
troubleshoot 32bit builds
jbrockmendel Dec 7, 2020
9164a1e
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 8, 2020
02585c5
move whatsnew
jbrockmendel Dec 8, 2020
3731fc8
restore 1.2.0 whatnsew
jbrockmendel Dec 8, 2020
184f013
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 9, 2020
8503671
troubleshoot
jbrockmendel Dec 9, 2020
34a96f7
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 10, 2020
a82f62b
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 11, 2020
fe58441
troubleshoot
jbrockmendel Dec 11, 2020
28e6296
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 13, 2020
1e802b8
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 14, 2020
c1ab90f
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 28, 2020
382def7
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 30, 2020
4023a16
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Dec 30, 2020
1e60537
flesh out tests
jbrockmendel Dec 30, 2020
072ef99
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Jan 8, 2021
51fe3b2
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Jan 12, 2021
7edd45a
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Jan 16, 2021
9783bce
arraymanager mypy fixup
jbrockmendel Jan 16, 2021
df6110e
Merge branch 'master' into 33457
jbrockmendel Jan 22, 2021
d7257f2
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Jan 25, 2021
1f9f9c3
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Jan 27, 2021
36fb2d4
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Jan 28, 2021
1292a92
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Jan 29, 2021
9b127bf
Merge branch 'master' into 33457
jbrockmendel Feb 2, 2021
831dc71
Merge branch 'master' into 33457
jbrockmendel Feb 3, 2021
1d50325
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Feb 3, 2021
d72f379
Merge branch 'master' of https://github.com/pandas-dev/pandas into 33457
jbrockmendel Feb 4, 2021
81e92d8
Merge branch 'master' into 33457
jbrockmendel Feb 11, 2021
67435f8
simplify test
jbrockmendel Feb 27, 2021
ab833aa
Merge branch 'master' into 33457
jbrockmendel Feb 27, 2021
017501e
Merge branch 'master' into 33457
jbrockmendel Mar 15, 2021
0219f7f
Merge branch 'master' into 33457
jbrockmendel Mar 15, 2021
032a55a
Merge branch 'master' into 33457
jbrockmendel Mar 17, 2021
51d102b
un-xfail
jbrockmendel Mar 17, 2021
bd6816b
Merge branch 'master' into 33457
jbrockmendel Mar 18, 2021
908b57b
Merge branch 'master' into 33457
jbrockmendel Mar 18, 2021
62f2437
32bit compat
jbrockmendel Mar 18, 2021
36b3302
troublehsoot
jbrockmendel Mar 18, 2021
37c9d22
troubleshoot 32bit builds
jbrockmendel Mar 18, 2021
0862ece
Merge branch 'master' into 33457
jbrockmendel Mar 19, 2021
38d8106
troubleshoot 32 bit
jbrockmendel Mar 19, 2021
647a393
DOC: suppress warnings from CategoricalBlock deprecation
jbrockmendel Mar 20, 2021
8b8d6a2
Merge branch 'master' into ci-doc
jbrockmendel Mar 20, 2021
9c6e008
Merge branch 'ci-doc' into 33457
jbrockmendel Mar 21, 2021
8285ece
troubleshoot 32 bit builds
jbrockmendel Mar 21, 2021
270be1e
troubleshoot 32bit builds
jbrockmendel Mar 21, 2021
9cf69c9
typo fixup
jbrockmendel Mar 21, 2021
065869a
Merge branch 'master' into 33457
jbrockmendel Mar 21, 2021
ca260c5
32bit troubleshoot
jbrockmendel Mar 21, 2021
5dcdc4a
Merge branch 'master' into 33457
jbrockmendel Mar 23, 2021
a03dcc3
Merge branch 'master' into 33457
jbrockmendel Mar 26, 2021
a1c5732
revert troubleshoot
jbrockmendel Mar 26, 2021
47b841a
Merge branch 'master' into 33457
jbrockmendel Mar 30, 2021
df060a5
Merge branch 'master' into 33457
jbrockmendel Mar 31, 2021
017b817
Merge branch 'master' into 33457
jbrockmendel Mar 31, 2021
0821a60
update test
jbrockmendel Apr 1, 2021
608fb9d
Merge branch 'master' into 33457
jbrockmendel Apr 1, 2021
a70fcc2
Merge branch 'master' into 33457
jbrockmendel Apr 5, 2021
effd630
Merge branch 'master' into 33457
jbrockmendel Apr 5, 2021
4e30881
Merge branch 'master' into 33457
jbrockmendel Apr 6, 2021
a254ed0
Merge branch 'master' into 33457
jbrockmendel Apr 8, 2021
df7b4d3
Merge branch 'master' into 33457
jbrockmendel Apr 9, 2021
8f49c7f
Merge branch 'master' into 33457
jbrockmendel Apr 11, 2021
a66afa0
revert duplicated whatsnew notes
jbrockmendel Apr 11, 2021
fb95732
revert unrelated test fix
jbrockmendel Apr 11, 2021
c0bace5
Merge branch 'master' into 33457
jbrockmendel Apr 16, 2021
9e43fe9
Merge branch 'master' into 33457
jbrockmendel Apr 16, 2021
f47fa2e
Merge branch 'master' into 33457
jbrockmendel Apr 27, 2021
29d3bba
Merge branch 'master' into 33457
jbrockmendel Apr 30, 2021
5b8de9a
Merge branch 'master' into 33457
jbrockmendel May 4, 2021
79c7ae2
Merge branch 'master' into 33457
jbrockmendel May 9, 2021
fa2dcea
Merge branch 'master' into 33457
jbrockmendel May 17, 2021
0e8f671
Merge branch 'master' into 33457
jbrockmendel Nov 8, 2021
3cdeeb4
Merge branch 'master' into 33457
jbrockmendel Nov 9, 2021
acd3514
Merge branch 'master' into 33457
jbrockmendel Nov 11, 2021
d97a1ac
Merge branch 'master' into 33457
jbrockmendel Nov 11, 2021
4342f5d
ArrayManager fixup
jbrockmendel Nov 11, 2021
f4dafc6
Merge branch 'master' into 33457
jbrockmendel Nov 12, 2021
bebb12f
avoid warning
jbrockmendel Nov 12, 2021
04475e3
Merge branch 'master' into 33457
jbrockmendel Nov 14, 2021
ed6f3ec
Merge branch 'master' into 33457
jbrockmendel Nov 26, 2021
fe9fe66
fixed on AM
jbrockmendel Nov 26, 2021
1ae50bf
Merge branch 'master' into 33457
jbrockmendel Nov 28, 2021
9d32c62
Merge branch 'master' into 33457
jbrockmendel Dec 16, 2021
8972875
Merge branch 'master' into 33457
jbrockmendel Dec 29, 2021
6524331
revert whitespace change
jbrockmendel Dec 29, 2021
123568d
revert no-longer-necessary
jbrockmendel Dec 29, 2021
8bef37a
Merge branch 'master' into 33457
jbrockmendel Dec 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ def _as_manager(self: NDFrameT, typ: str, copy: bool_t = True) -> NDFrameT:
# fastpath of passing a manager doesn't check the option/manager class
return self._constructor(new_mgr).__finalize__(self)

@property
def _uses_array_manager(self) -> bool_t:
return isinstance(self._mgr, ArrayManager)

# ----------------------------------------------------------------------
# attrs and flags

Expand Down
27 changes: 25 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from pandas.util._decorators import doc
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.cast import maybe_infer_dtype_type
from pandas.core.dtypes.common import (
is_array_like,
is_bool_dtype,
Expand Down Expand Up @@ -693,8 +694,23 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None):
# GH#38148
keys = self.obj.columns.union(key, sort=False)

# Try to get the right dtype when we do this reindex.
jbrockmendel marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't infer_dtype_from do this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that infers a dtype, but we need to pass a scalar fill_value to reindex_axis

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this edit as no longer necessary. i think at this point all the controversial bits are gone and we're down to just the bugfix.

fv = None
if not is_list_like(value):
fv = value
elif len(value) and not is_list_like(value[0]):
fv = value[0]
else:
dtype = maybe_infer_dtype_type(value)
if dtype is not None:
fv = dtype.type(0)

self.obj._mgr = self.obj._mgr.reindex_axis(
keys, axis=0, consolidate=False, only_slice=True
keys,
axis=0,
consolidate=False,
only_slice=True,
fill_value=fv,
)

def __setitem__(self, key, value):
Expand Down Expand Up @@ -1588,7 +1604,6 @@ def _setitem_with_indexer(self, indexer, value, name="iloc"):
# essentially this separates out the block that is needed
# to possibly be modified
if self.ndim > 1 and i == info_axis:

# add the new item, and set the value
# must have all defined axes if we have a scalar
# or a list-like on the non-info axes if we have a
Expand Down Expand Up @@ -1848,6 +1863,14 @@ def _setitem_single_column(self, loc: int, value, plane_indexer):
# multi-dim object
# GH#6149 (null slice), GH#10408 (full bounds)
if com.is_null_slice(pi) or com.is_full_slice(pi, len(self.obj)):
if not self.obj._uses_array_manager:
blk = ser._mgr.blocks[0]
if blk._can_hold_element(value) and is_scalar(value):
# FIXME: ExtensionBlock._can_hold_element
# We can do an inplace-setting, do it directly on _values
# to get our underlying
ser._values[plane_indexer] = value
return
ser = value
elif (
is_array_like(value)
Expand Down
10 changes: 1 addition & 9 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1456,17 +1456,9 @@ def iget(self, col):
return self.values

def set_inplace(self, locs, values) -> None:
# NB: This is a misnomer, is supposed to be inplace but is not,
# see GH#33457
# When an ndarray, we should have locs.tolist() == [0]
# When a BlockPlacement we should have list(locs) == [0]
self.values = values
try:
# TODO(GH33457) this can be removed
self._cache.clear()
except AttributeError:
# _cache not yet initialized
pass
self.values[:] = values

def _maybe_squeeze_arg(self, arg):
"""
Expand Down
1 change: 0 additions & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,6 @@ def reindex_indexer(
use_na_proxy : bool, default False
Whether to use a np.void ndarray for newly introduced columns.

pandas-indexer with -1's only.
"""
if indexer is None:
if new_axis is self.axes[axis] and not copy:
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ def test_fancy_getitem_slice_mixed(self, float_frame, float_string_frame):
# setting it triggers setting with copy
sliced = float_frame.iloc[:, -3:]

# check that the setitem below is not a no-op
assert not (float_frame["C"] == 4).all()

assert np.shares_memory(sliced["C"]._values, float_frame["C"]._values)

msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame"
Expand All @@ -553,6 +556,12 @@ def test_fancy_getitem_slice_mixed(self, float_frame, float_string_frame):

assert (float_frame["C"] == 4).all()

# GH#35417 setting with setitem creates a new array, so we get the warning
# but do not modify the original
with pytest.raises(com.SettingWithCopyError, match=msg):
sliced["C"] = 5.0
assert (float_frame["C"] == 4).all()

def test_getitem_setitem_non_ix_labels(self):
df = tm.makeTimeDataFrame()

Expand Down
1 change: 1 addition & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ def test_setitem_same_dtype_not_inplace(self, value, using_array_manager, reques
df[["B"]] = value

expected = DataFrame([[0, 1], [0, 1]], columns=cols)

tm.assert_frame_equal(df, expected)
tm.assert_frame_equal(df_view, df_copy)

Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/frame/methods/test_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import numpy as np
import pytest

import pandas.util._test_decorators as td

from pandas import (
DataFrame,
Index,
Expand Down Expand Up @@ -170,9 +168,9 @@ def test_rename_multiindex(self):
renamed = df.rename(index={"foo1": "foo3", "bar2": "bar3"}, level=0)
tm.assert_index_equal(renamed.index, new_index)

@td.skip_array_manager_not_yet_implemented # TODO(ArrayManager) setitem copy/view
def test_rename_nocopy(self, float_frame):
jreback marked this conversation as resolved.
Show resolved Hide resolved
renamed = float_frame.rename(columns={"C": "foo"}, copy=False)
renamed["foo"][:] = 1.0
jreback marked this conversation as resolved.
Show resolved Hide resolved

assert np.shares_memory(renamed["foo"]._values, float_frame["C"]._values)

Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,22 @@ def test_identity_slice_returns_new_object(self, using_array_manager, request):

# should be a shallow copy
assert np.shares_memory(original_df["a"], sliced_df["a"])
original_df.loc[:, "a"] = [4, 4, 4]
if not using_array_manager:
assert (sliced_df["a"] == 4).all()
else:
# FIXME: what is the expected/desired behavior here? test it!
pass

# GH#35417 but setting with setitem creates a new array, so
# sliced_df is not changed
original_df["a"] = [5, 5, 5]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't raise a SettingWithCopy warning? Above a similar sliced case seems to assert a warning: https://github.com/pandas-dev/pandas/pull/35417/files#diff-f3753d3792b1cdb7c319380f7d1f58636ad0f9bbce037eb6920a6f9830412e9cR559

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

original_df["a"] = [5, 5, 5] here is setting a new array, not overwriting an existing one, whereas the .loc settings in the linked tests are writing into existing arrays

if using_array_manager:
# TODO(ArrayManager) verify it is expected that the original didn't change
# setitem is replacing full column, so doesn't update "viewing" dataframe
assert not (sliced_df["a"] == 4).all()
else:
assert (sliced_df["a"] == 4).all()

# Setting using .loc[:, "a"] sets inplace so alters both sliced and orig
original_df.loc[:, "a"] = [4, 4, 4]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated both tests

Expand Down
9 changes: 3 additions & 6 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,20 +1033,17 @@ def test_loc_empty_list_indexer_is_ok(self):
df.loc[[]], df.iloc[:0, :], check_index_type=True, check_column_type=True
)

def test_identity_slice_returns_new_object(self, using_array_manager, request):
def test_identity_slice_returns_new_object(self, request):
# GH13873
if using_array_manager:
mark = pytest.mark.xfail(
reason="setting with .loc[:, 'a'] does not alter inplace"
)
request.node.add_marker(mark)

original_df = DataFrame({"a": [1, 2, 3]})
sliced_df = original_df.loc[:]
assert sliced_df is not original_df
assert original_df[:] is not original_df

# should be a shallow copy
original_df["a"][:] = [4, 4, 4]

assert np.shares_memory(original_df["a"]._values, sliced_df["a"]._values)

# Setting using .loc[:, "a"] sets inplace so alters both sliced and orig
Expand Down
65 changes: 37 additions & 28 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,35 +741,51 @@ def test_get_numeric_data(self):
item_shape=(3,),
)
mgr.iset(5, np.array([1, 2, 3], dtype=np.object_))

numeric = mgr.get_numeric_data()
tm.assert_index_equal(numeric.items, Index(["int", "float", "complex", "bool"]))
mgr_idx = mgr.items.get_loc("float")
num_idx = numeric.items.get_loc("float")

tm.assert_almost_equal(
mgr.iget(mgr.items.get_loc("float")).internal_values(),
numeric.iget(numeric.items.get_loc("float")).internal_values(),
mgr.iget(mgr_idx).internal_values(),
numeric.iget(num_idx).internal_values(),
)

# Check sharing
numeric.iget(num_idx).internal_values()[:] = [100.0, 200.0, 300.0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would split the inplace to a separate test

numeric.iset(
numeric.items.get_loc("float"),
np.array([100.0, 200.0, 300.0]),
inplace=True,
)

tm.assert_almost_equal(
mgr.iget(mgr.items.get_loc("float")).internal_values(),
mgr.iget(mgr_idx).internal_values(),
np.array([100.0, 200.0, 300.0]),
)

def test_get_numeric_data_copy(self):
mgr = create_mgr(
"int: int; float: float; complex: complex;"
"str: object; bool: bool; obj: object; dt: datetime",
item_shape=(3,),
)
mgr.iset(5, np.array([1, 2, 3], dtype=np.object_))
numeric = mgr.get_numeric_data()
mgr_idx = mgr.items.get_loc("float")
num_idx = numeric.items.get_loc("float")

numeric2 = mgr.get_numeric_data(copy=True)
tm.assert_index_equal(numeric.items, Index(["int", "float", "complex", "bool"]))
numeric2.iget(num_idx).internal_values()[:] = [1000.0, 2000.0, 3000.0]
numeric2.iset(
numeric2.items.get_loc("float"),
np.array([1000.0, 2000.0, 3000.0]),
inplace=True,
)
tm.assert_almost_equal(
mgr.iget(mgr.items.get_loc("float")).internal_values(),
np.array([100.0, 200.0, 300.0]),
mgr.iget(mgr_idx).internal_values(),
np.array([1.0, 1.0, 1.0]),
)

def test_get_bool_data(self):
Expand All @@ -787,18 +803,28 @@ def test_get_bool_data(self):
bools.iget(bools.items.get_loc("bool")).internal_values(),
)

bools.iset(0, np.array([True, False, True]), inplace=True)
# GH#33457 setting a new array on bools does _not_ alter the original
# array in-place, so mgr is unchanged
bools.iset(0, np.array([True, False, True]))
tm.assert_numpy_array_equal(
mgr.iget(mgr.items.get_loc("bool")).internal_values(),
np.array([True, False, True]),
np.array([True, True, True]),
)

# Check sharing
def test_get_bool_data_copy(self):
# GH#35417
mgr = create_mgr(
"int: int; float: float; complex: complex;"
"str: object; bool: bool; obj: object; dt: datetime",
item_shape=(3,),
)
mgr.iset(6, np.array([True, False, True], dtype=np.object_))

bools2 = mgr.get_bool_data(copy=True)
bools2.iset(0, np.array([False, True, False]))
bools2.blocks[0].values[:] = [[False, True, False]]
jreback marked this conversation as resolved.
Show resolved Hide resolved
tm.assert_numpy_array_equal(
mgr.iget(mgr.items.get_loc("bool")).internal_values(),
np.array([True, False, True]),
np.array([True, True, True]),
)

def test_unicode_repr_doesnt_raise(self):
Expand Down Expand Up @@ -1343,23 +1369,6 @@ def check_frame_setitem(self, elem, index: Index, inplace: bool):
assert df.dtypes[0] == object


class TestShouldStore:
def test_should_store_categorical(self):
cat = Categorical(["A", "B", "C"])
df = DataFrame(cat)
blk = df._mgr.blocks[0]

# matching dtype
assert blk.should_store(cat)
assert blk.should_store(cat[:-1])

# different dtype
assert not blk.should_store(cat.as_ordered())

# ndarray instead of Categorical
assert not blk.should_store(np.asarray(cat))


def test_validate_ndim(block_maker):
values = np.array([1.0, 2.0])
placement = slice(2)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ def test_index_with_nan(self):

# all-nan in mi
df2 = df.copy()
df2.loc[:, "id2"] = np.nan
df2["id2"] = np.nan
y = df2.set_index("id2")
result = y.to_string()
expected = (
Expand Down