Skip to content

Commit e459c8e

Browse files
authored
Merge pull request #235 from pandas-dev/master
Sync Fork from Upstream Repo
2 parents 3892e1a + 1b60699 commit e459c8e

File tree

11 files changed

+406
-323
lines changed

11 files changed

+406
-323
lines changed

doc/source/user_guide/boolean.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
Nullable Boolean data type
1313
**************************
1414

15+
.. note::
16+
17+
BooleanArray is currently experimental. Its API or implementation may
18+
change without warning.
19+
1520
.. versionadded:: 1.0.0
1621

1722

doc/source/whatsnew/v1.3.1.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Fixed regressions
2525
- Fixed regression in :meth:`DataFrame.isin` and :meth:`Series.isin` raising ``TypeError`` with nullable data containing at least one missing value (:issue:`42405`)
2626
- Regression in :func:`concat` between objects with bool dtype and integer dtype casting to object instead of to integer (:issue:`42092`)
2727
- Bug in :class:`Series` constructor not accepting a ``dask.Array`` (:issue:`38645`)
28+
- Fixed regression for ``SettingWithCopyWarning`` displaying incorrect stacklevel (:issue:`42570`)
2829
- Fixed regression in :func:`to_datetime` returning pd.NaT for inputs that produce duplicated values, when ``cache=True`` (:issue:`42259`)
2930

3031

@@ -36,7 +37,7 @@ Bug fixes
3637
~~~~~~~~~
3738
- Fixed bug in :meth:`DataFrame.transpose` dropping values when the DataFrame had an Extension Array dtype and a duplicate index (:issue:`42380`)
3839
- Fixed bug in :meth:`DataFrame.to_xml` raising ``KeyError`` when called with ``index=False`` and an offset index (:issue:`42458`)
39-
-
40+
- Fixed bug in :meth:`DataFrame.copy` failing to consolidate blocks in the result (:issue:`42579`)
4041

4142
.. ---------------------------------------------------------------------------
4243

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3745,7 +3745,7 @@ def _set_item_mgr(self, key, value: ArrayLike) -> None:
37453745
# try to set first as we want an invalid
37463746
# value exception to occur first
37473747
if len(self):
3748-
self._check_setitem_copy()
3748+
self._check_setitem_copy(stacklevel=5)
37493749

37503750
def _iset_item(self, loc: int, value) -> None:
37513751
arraylike = self._sanitize_column(value)

pandas/core/internals/managers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,9 @@ def copy_func(ax):
598598

599599
res = self.apply("copy", deep=deep)
600600
res.axes = new_axes
601+
602+
if deep:
603+
res._consolidate_inplace()
601604
return res
602605

603606
def consolidate(self: T) -> T:

pandas/core/reshape/reshape.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ def _unstack_multiple(data, clocs, fill_value=None):
402402

403403
return result
404404

405-
dummy = data.copy()
405+
# GH#42579 deep=False to avoid consolidating
406+
dummy = data.copy(deep=False)
406407
dummy.index = dummy_index
407408

408409
unstacked = dummy.unstack("__placeholder__", fill_value=fill_value)

pandas/tests/frame/methods/test_copy.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import numpy as np
12
import pytest
23

4+
import pandas.util._test_decorators as td
5+
36
from pandas import DataFrame
47
import pandas._testing as tm
58

@@ -41,3 +44,20 @@ def test_copy(self, float_frame, float_string_frame):
4144
# copy objects
4245
copy = float_string_frame.copy()
4346
assert copy._mgr is not float_string_frame._mgr
47+
48+
@td.skip_array_manager_invalid_test
49+
def test_copy_consolidates(self):
50+
# GH#42477
51+
df = DataFrame(
52+
{
53+
"a": np.random.randint(0, 100, size=55),
54+
"b": np.random.randint(0, 100, size=55),
55+
}
56+
)
57+
58+
for i in range(0, 10):
59+
df.loc[:, f"n_{i}"] = np.random.randint(0, 100, size=55)
60+
61+
assert len(df._mgr.blocks) == 11
62+
result = df.copy()
63+
assert len(result._mgr.blocks) == 1

pandas/tests/indexing/test_chaining_and_caching.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,16 @@ def test_detect_chained_assignment_warnings_filter_and_dupe_cols(self):
435435
)
436436
tm.assert_frame_equal(df, expected)
437437

438+
@pytest.mark.parametrize("rhs", [3, DataFrame({0: [1, 2, 3, 4]})])
439+
def test_detect_chained_assignment_warning_stacklevel(self, rhs):
440+
# GH#42570
441+
df = DataFrame(np.arange(25).reshape(5, 5))
442+
chained = df.loc[:3]
443+
with option_context("chained_assignment", "warn"):
444+
with tm.assert_produces_warning(com.SettingWithCopyWarning) as t:
445+
chained[2] = rhs
446+
assert t[0].filename == __file__
447+
438448
# TODO(ArrayManager) fast_xs with array-like scalars is not yet working
439449
@td.skip_array_manager_not_yet_implemented
440450
def test_chained_getitem_with_lists(self):

pandas/tests/internals/test_internals.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@ def test_copy(self, mgr):
461461
# DatetimeTZBlock has DatetimeIndex values
462462
assert cp_blk.values._data.base is blk.values._data.base
463463

464+
# copy(deep=True) consolidates, so the block-wise assertions will
465+
# fail is mgr is not consolidated
466+
mgr._consolidate_inplace()
464467
cp = mgr.copy(deep=True)
465468
for blk, cp_blk in zip(mgr.blocks, cp.blocks):
466469

pandas/tests/reshape/test_pivot.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,23 @@ def test_pivot_table_sort_false(self):
21482148
)
21492149
tm.assert_frame_equal(result, expected)
21502150

2151+
def test_pivot_table_with_margins_and_numeric_columns(self):
2152+
# GH 26568
2153+
df = DataFrame([["a", "x", 1], ["a", "y", 2], ["b", "y", 3], ["b", "z", 4]])
2154+
df.columns = [10, 20, 30]
2155+
2156+
result = df.pivot_table(
2157+
index=10, columns=20, values=30, aggfunc="sum", fill_value=0, margins=True
2158+
)
2159+
2160+
expected = DataFrame([[1, 2, 0, 3], [0, 3, 4, 7], [1, 5, 4, 10]])
2161+
expected.columns = ["x", "y", "z", "All"]
2162+
expected.index = ["a", "b", "All"]
2163+
expected.columns.name = 20
2164+
expected.index.name = 10
2165+
2166+
tm.assert_frame_equal(result, expected)
2167+
21512168

21522169
class TestPivot:
21532170
def test_pivot(self):

0 commit comments

Comments
 (0)