forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_indexing.py
1119 lines (940 loc) · 36.7 KB
/
test_indexing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import pytest
from pandas.errors import SettingWithCopyWarning
from pandas.core.dtypes.common import is_float_dtype
import pandas as pd
from pandas import (
DataFrame,
Series,
)
import pandas._testing as tm
from pandas.tests.copy_view.util import get_array
@pytest.fixture(params=["numpy", "nullable"])
def backend(request):
if request.param == "numpy":
def make_dataframe(*args, **kwargs):
return DataFrame(*args, **kwargs)
def make_series(*args, **kwargs):
return Series(*args, **kwargs)
elif request.param == "nullable":
def make_dataframe(*args, **kwargs):
df = DataFrame(*args, **kwargs)
df_nullable = df.convert_dtypes()
# convert_dtypes will try to cast float to int if there is no loss in
# precision -> undo that change
for col in df.columns:
if is_float_dtype(df[col].dtype) and not is_float_dtype(
df_nullable[col].dtype
):
df_nullable[col] = df_nullable[col].astype("Float64")
# copy final result to ensure we start with a fully self-owning DataFrame
return df_nullable.copy()
def make_series(*args, **kwargs):
ser = Series(*args, **kwargs)
return ser.convert_dtypes().copy()
return request.param, make_dataframe, make_series
# -----------------------------------------------------------------------------
# Indexing operations taking subset + modifying the subset/parent
def test_subset_column_selection(backend, using_copy_on_write):
# Case: taking a subset of the columns of a DataFrame
# + afterwards modifying the subset
_, DataFrame, _ = backend
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
subset = df[["a", "c"]]
if using_copy_on_write:
# the subset shares memory ...
assert np.shares_memory(get_array(subset, "a"), get_array(df, "a"))
# ... but uses CoW when being modified
subset.iloc[0, 0] = 0
else:
assert not np.shares_memory(get_array(subset, "a"), get_array(df, "a"))
# INFO this no longer raise warning since pandas 1.4
# with pd.option_context("chained_assignment", "warn"):
# with tm.assert_produces_warning(SettingWithCopyWarning):
subset.iloc[0, 0] = 0
assert not np.shares_memory(get_array(subset, "a"), get_array(df, "a"))
expected = DataFrame({"a": [0, 2, 3], "c": [0.1, 0.2, 0.3]})
tm.assert_frame_equal(subset, expected)
tm.assert_frame_equal(df, df_orig)
def test_subset_column_selection_modify_parent(backend, using_copy_on_write):
# Case: taking a subset of the columns of a DataFrame
# + afterwards modifying the parent
_, DataFrame, _ = backend
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
subset = df[["a", "c"]]
if using_copy_on_write:
# the subset shares memory ...
assert np.shares_memory(get_array(subset, "a"), get_array(df, "a"))
# ... but parent uses CoW parent when it is modified
df.iloc[0, 0] = 0
assert not np.shares_memory(get_array(subset, "a"), get_array(df, "a"))
if using_copy_on_write:
# different column/block still shares memory
assert np.shares_memory(get_array(subset, "c"), get_array(df, "c"))
expected = DataFrame({"a": [1, 2, 3], "c": [0.1, 0.2, 0.3]})
tm.assert_frame_equal(subset, expected)
def test_subset_row_slice(backend, using_copy_on_write):
# Case: taking a subset of the rows of a DataFrame using a slice
# + afterwards modifying the subset
_, DataFrame, _ = backend
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
subset = df[1:3]
subset._mgr._verify_integrity()
assert np.shares_memory(get_array(subset, "a"), get_array(df, "a"))
if using_copy_on_write:
subset.iloc[0, 0] = 0
assert not np.shares_memory(get_array(subset, "a"), get_array(df, "a"))
else:
# INFO this no longer raise warning since pandas 1.4
# with pd.option_context("chained_assignment", "warn"):
# with tm.assert_produces_warning(SettingWithCopyWarning):
subset.iloc[0, 0] = 0
subset._mgr._verify_integrity()
expected = DataFrame({"a": [0, 3], "b": [5, 6], "c": [0.2, 0.3]}, index=range(1, 3))
tm.assert_frame_equal(subset, expected)
if using_copy_on_write:
# original parent dataframe is not modified (CoW)
tm.assert_frame_equal(df, df_orig)
else:
# original parent dataframe is actually updated
df_orig.iloc[1, 0] = 0
tm.assert_frame_equal(df, df_orig)
@pytest.mark.parametrize(
"dtype", ["int64", "float64"], ids=["single-block", "mixed-block"]
)
def test_subset_column_slice(backend, using_copy_on_write, using_array_manager, dtype):
# Case: taking a subset of the columns of a DataFrame using a slice
# + afterwards modifying the subset
dtype_backend, DataFrame, _ = backend
single_block = (
dtype == "int64" and dtype_backend == "numpy"
) and not using_array_manager
df = DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": np.array([7, 8, 9], dtype=dtype)}
)
df_orig = df.copy()
subset = df.iloc[:, 1:]
subset._mgr._verify_integrity()
if using_copy_on_write:
assert np.shares_memory(get_array(subset, "b"), get_array(df, "b"))
subset.iloc[0, 0] = 0
assert not np.shares_memory(get_array(subset, "b"), get_array(df, "b"))
else:
# we only get a warning in case of a single block
warn = SettingWithCopyWarning if single_block else None
with pd.option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(warn):
subset.iloc[0, 0] = 0
expected = DataFrame({"b": [0, 5, 6], "c": np.array([7, 8, 9], dtype=dtype)})
tm.assert_frame_equal(subset, expected)
# original parent dataframe is not modified (also not for BlockManager case,
# except for single block)
if not using_copy_on_write and (using_array_manager or single_block):
df_orig.iloc[0, 1] = 0
tm.assert_frame_equal(df, df_orig)
else:
tm.assert_frame_equal(df, df_orig)
@pytest.mark.parametrize(
"dtype", ["int64", "float64"], ids=["single-block", "mixed-block"]
)
@pytest.mark.parametrize(
"row_indexer",
[slice(1, 2), np.array([False, True, True]), np.array([1, 2])],
ids=["slice", "mask", "array"],
)
@pytest.mark.parametrize(
"column_indexer",
[slice("b", "c"), np.array([False, True, True]), ["b", "c"]],
ids=["slice", "mask", "array"],
)
def test_subset_loc_rows_columns(
backend,
dtype,
row_indexer,
column_indexer,
using_array_manager,
using_copy_on_write,
):
# Case: taking a subset of the rows+columns of a DataFrame using .loc
# + afterwards modifying the subset
# Generic test for several combinations of row/column indexers, not all
# of those could actually return a view / need CoW (so this test is not
# checking memory sharing, only ensuring subsequent mutation doesn't
# affect the parent dataframe)
dtype_backend, DataFrame, _ = backend
df = DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": np.array([7, 8, 9], dtype=dtype)}
)
df_orig = df.copy()
subset = df.loc[row_indexer, column_indexer]
# modifying the subset never modifies the parent
subset.iloc[0, 0] = 0
expected = DataFrame(
{"b": [0, 6], "c": np.array([8, 9], dtype=dtype)}, index=range(1, 3)
)
tm.assert_frame_equal(subset, expected)
# a few corner cases _do_ actually modify the parent (with both row and column
# slice, and in case of ArrayManager or BlockManager with single block)
if (
isinstance(row_indexer, slice)
and isinstance(column_indexer, slice)
and (
using_array_manager
or (
dtype == "int64"
and dtype_backend == "numpy"
and not using_copy_on_write
)
)
):
df_orig.iloc[1, 1] = 0
tm.assert_frame_equal(df, df_orig)
@pytest.mark.parametrize(
"dtype", ["int64", "float64"], ids=["single-block", "mixed-block"]
)
@pytest.mark.parametrize(
"row_indexer",
[slice(1, 3), np.array([False, True, True]), np.array([1, 2])],
ids=["slice", "mask", "array"],
)
@pytest.mark.parametrize(
"column_indexer",
[slice(1, 3), np.array([False, True, True]), [1, 2]],
ids=["slice", "mask", "array"],
)
def test_subset_iloc_rows_columns(
backend,
dtype,
row_indexer,
column_indexer,
using_array_manager,
using_copy_on_write,
):
# Case: taking a subset of the rows+columns of a DataFrame using .iloc
# + afterwards modifying the subset
# Generic test for several combinations of row/column indexers, not all
# of those could actually return a view / need CoW (so this test is not
# checking memory sharing, only ensuring subsequent mutation doesn't
# affect the parent dataframe)
dtype_backend, DataFrame, _ = backend
df = DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": np.array([7, 8, 9], dtype=dtype)}
)
df_orig = df.copy()
subset = df.iloc[row_indexer, column_indexer]
# modifying the subset never modifies the parent
subset.iloc[0, 0] = 0
expected = DataFrame(
{"b": [0, 6], "c": np.array([8, 9], dtype=dtype)}, index=range(1, 3)
)
tm.assert_frame_equal(subset, expected)
# a few corner cases _do_ actually modify the parent (with both row and column
# slice, and in case of ArrayManager or BlockManager with single block)
if (
isinstance(row_indexer, slice)
and isinstance(column_indexer, slice)
and (
using_array_manager
or (
dtype == "int64"
and dtype_backend == "numpy"
and not using_copy_on_write
)
)
):
df_orig.iloc[1, 1] = 0
tm.assert_frame_equal(df, df_orig)
@pytest.mark.parametrize(
"indexer",
[slice(0, 2), np.array([True, True, False]), np.array([0, 1])],
ids=["slice", "mask", "array"],
)
def test_subset_set_with_row_indexer(backend, indexer_si, indexer, using_copy_on_write):
# Case: setting values with a row indexer on a viewing subset
# subset[indexer] = value and subset.iloc[indexer] = value
_, DataFrame, _ = backend
df = DataFrame({"a": [1, 2, 3, 4], "b": [4, 5, 6, 7], "c": [0.1, 0.2, 0.3, 0.4]})
df_orig = df.copy()
subset = df[1:4]
if (
indexer_si is tm.setitem
and isinstance(indexer, np.ndarray)
and indexer.dtype == "int"
):
pytest.skip("setitem with labels selects on columns")
if using_copy_on_write:
indexer_si(subset)[indexer] = 0
else:
# INFO iloc no longer raises warning since pandas 1.4
warn = SettingWithCopyWarning if indexer_si is tm.setitem else None
with pd.option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(warn):
indexer_si(subset)[indexer] = 0
expected = DataFrame(
{"a": [0, 0, 4], "b": [0, 0, 7], "c": [0.0, 0.0, 0.4]}, index=range(1, 4)
)
tm.assert_frame_equal(subset, expected)
if using_copy_on_write:
# original parent dataframe is not modified (CoW)
tm.assert_frame_equal(df, df_orig)
else:
# original parent dataframe is actually updated
df_orig[1:3] = 0
tm.assert_frame_equal(df, df_orig)
def test_subset_set_with_mask(backend, using_copy_on_write):
# Case: setting values with a mask on a viewing subset: subset[mask] = value
_, DataFrame, _ = backend
df = DataFrame({"a": [1, 2, 3, 4], "b": [4, 5, 6, 7], "c": [0.1, 0.2, 0.3, 0.4]})
df_orig = df.copy()
subset = df[1:4]
mask = subset > 3
if using_copy_on_write:
subset[mask] = 0
else:
with pd.option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(SettingWithCopyWarning):
subset[mask] = 0
expected = DataFrame(
{"a": [2, 3, 0], "b": [0, 0, 0], "c": [0.20, 0.3, 0.4]}, index=range(1, 4)
)
tm.assert_frame_equal(subset, expected)
if using_copy_on_write:
# original parent dataframe is not modified (CoW)
tm.assert_frame_equal(df, df_orig)
else:
# original parent dataframe is actually updated
df_orig.loc[3, "a"] = 0
df_orig.loc[1:3, "b"] = 0
tm.assert_frame_equal(df, df_orig)
def test_subset_set_column(backend, using_copy_on_write):
# Case: setting a single column on a viewing subset -> subset[col] = value
dtype_backend, DataFrame, _ = backend
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
subset = df[1:3]
if dtype_backend == "numpy":
arr = np.array([10, 11], dtype="int64")
else:
arr = pd.array([10, 11], dtype="Int64")
if using_copy_on_write:
subset["a"] = arr
else:
with pd.option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(SettingWithCopyWarning):
subset["a"] = arr
subset._mgr._verify_integrity()
expected = DataFrame(
{"a": [10, 11], "b": [5, 6], "c": [0.2, 0.3]}, index=range(1, 3)
)
tm.assert_frame_equal(subset, expected)
tm.assert_frame_equal(df, df_orig)
@pytest.mark.parametrize(
"dtype", ["int64", "float64"], ids=["single-block", "mixed-block"]
)
def test_subset_set_column_with_loc(
backend, using_copy_on_write, using_array_manager, dtype
):
# Case: setting a single column with loc on a viewing subset
# -> subset.loc[:, col] = value
_, DataFrame, _ = backend
df = DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": np.array([7, 8, 9], dtype=dtype)}
)
df_orig = df.copy()
subset = df[1:3]
if using_copy_on_write:
subset.loc[:, "a"] = np.array([10, 11], dtype="int64")
else:
with pd.option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(
None,
raise_on_extra_warnings=not using_array_manager,
):
subset.loc[:, "a"] = np.array([10, 11], dtype="int64")
subset._mgr._verify_integrity()
expected = DataFrame(
{"a": [10, 11], "b": [5, 6], "c": np.array([8, 9], dtype=dtype)},
index=range(1, 3),
)
tm.assert_frame_equal(subset, expected)
if using_copy_on_write:
# original parent dataframe is not modified (CoW)
tm.assert_frame_equal(df, df_orig)
else:
# original parent dataframe is actually updated
df_orig.loc[1:3, "a"] = np.array([10, 11], dtype="int64")
tm.assert_frame_equal(df, df_orig)
def test_subset_set_column_with_loc2(backend, using_copy_on_write, using_array_manager):
# Case: setting a single column with loc on a viewing subset
# -> subset.loc[:, col] = value
# separate test for case of DataFrame of a single column -> takes a separate
# code path
_, DataFrame, _ = backend
df = DataFrame({"a": [1, 2, 3]})
df_orig = df.copy()
subset = df[1:3]
if using_copy_on_write:
subset.loc[:, "a"] = 0
else:
with pd.option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(
None,
raise_on_extra_warnings=not using_array_manager,
):
subset.loc[:, "a"] = 0
subset._mgr._verify_integrity()
expected = DataFrame({"a": [0, 0]}, index=range(1, 3))
tm.assert_frame_equal(subset, expected)
if using_copy_on_write:
# original parent dataframe is not modified (CoW)
tm.assert_frame_equal(df, df_orig)
else:
# original parent dataframe is actually updated
df_orig.loc[1:3, "a"] = 0
tm.assert_frame_equal(df, df_orig)
@pytest.mark.parametrize(
"dtype", ["int64", "float64"], ids=["single-block", "mixed-block"]
)
def test_subset_set_columns(backend, using_copy_on_write, dtype):
# Case: setting multiple columns on a viewing subset
# -> subset[[col1, col2]] = value
dtype_backend, DataFrame, _ = backend
df = DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": np.array([7, 8, 9], dtype=dtype)}
)
df_orig = df.copy()
subset = df[1:3]
if using_copy_on_write:
subset[["a", "c"]] = 0
else:
with pd.option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(SettingWithCopyWarning):
subset[["a", "c"]] = 0
subset._mgr._verify_integrity()
if using_copy_on_write:
# first and third column should certainly have no references anymore
assert all(subset._mgr._has_no_reference(i) for i in [0, 2])
expected = DataFrame({"a": [0, 0], "b": [5, 6], "c": [0, 0]}, index=range(1, 3))
if dtype_backend == "nullable":
# there is not yet a global option, so overriding a column by setting a scalar
# defaults to numpy dtype even if original column was nullable
expected["a"] = expected["a"].astype("int64")
expected["c"] = expected["c"].astype("int64")
tm.assert_frame_equal(subset, expected)
tm.assert_frame_equal(df, df_orig)
@pytest.mark.parametrize(
"indexer",
[slice("a", "b"), np.array([True, True, False]), ["a", "b"]],
ids=["slice", "mask", "array"],
)
def test_subset_set_with_column_indexer(backend, indexer, using_copy_on_write):
# Case: setting multiple columns with a column indexer on a viewing subset
# -> subset.loc[:, [col1, col2]] = value
_, DataFrame, _ = backend
df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3], "c": [4, 5, 6]})
df_orig = df.copy()
subset = df[1:3]
if using_copy_on_write:
subset.loc[:, indexer] = 0
else:
with pd.option_context("chained_assignment", "warn"):
# As of 2.0, this setitem attempts (successfully) to set values
# inplace, so the assignment is not chained.
subset.loc[:, indexer] = 0
subset._mgr._verify_integrity()
expected = DataFrame({"a": [0, 0], "b": [0.0, 0.0], "c": [5, 6]}, index=range(1, 3))
tm.assert_frame_equal(subset, expected)
if using_copy_on_write:
tm.assert_frame_equal(df, df_orig)
else:
# pre-2.0, in the mixed case with BlockManager, only column "a"
# would be mutated in the parent frame. this changed with the
# enforcement of GH#45333
df_orig.loc[1:2, ["a", "b"]] = 0
tm.assert_frame_equal(df, df_orig)
@pytest.mark.parametrize(
"method",
[
lambda df: df[["a", "b"]][0:2],
lambda df: df[0:2][["a", "b"]],
lambda df: df[["a", "b"]].iloc[0:2],
lambda df: df[["a", "b"]].loc[0:1],
lambda df: df[0:2].iloc[:, 0:2],
lambda df: df[0:2].loc[:, "a":"b"], # type: ignore[misc]
],
ids=[
"row-getitem-slice",
"column-getitem",
"row-iloc-slice",
"row-loc-slice",
"column-iloc-slice",
"column-loc-slice",
],
)
@pytest.mark.parametrize(
"dtype", ["int64", "float64"], ids=["single-block", "mixed-block"]
)
def test_subset_chained_getitem(
request, backend, method, dtype, using_copy_on_write, using_array_manager
):
# Case: creating a subset using multiple, chained getitem calls using views
# still needs to guarantee proper CoW behaviour
_, DataFrame, _ = backend
df = DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": np.array([7, 8, 9], dtype=dtype)}
)
df_orig = df.copy()
# when not using CoW, it depends on whether we have a single block or not
# and whether we are slicing the columns -> in that case we have a view
test_callspec = request.node.callspec.id
if not using_array_manager:
subset_is_view = test_callspec in (
"numpy-single-block-column-iloc-slice",
"numpy-single-block-column-loc-slice",
)
else:
# with ArrayManager, it doesn't matter whether we have
# single vs mixed block or numpy vs nullable dtypes
subset_is_view = test_callspec.endswith(
("column-iloc-slice", "column-loc-slice")
)
# modify subset -> don't modify parent
subset = method(df)
subset.iloc[0, 0] = 0
if using_copy_on_write or (not subset_is_view):
tm.assert_frame_equal(df, df_orig)
else:
assert df.iloc[0, 0] == 0
# modify parent -> don't modify subset
subset = method(df)
df.iloc[0, 0] = 0
expected = DataFrame({"a": [1, 2], "b": [4, 5]})
if using_copy_on_write or not subset_is_view:
tm.assert_frame_equal(subset, expected)
else:
assert subset.iloc[0, 0] == 0
@pytest.mark.parametrize(
"dtype", ["int64", "float64"], ids=["single-block", "mixed-block"]
)
def test_subset_chained_getitem_column(backend, dtype, using_copy_on_write):
# Case: creating a subset using multiple, chained getitem calls using views
# still needs to guarantee proper CoW behaviour
_, DataFrame, Series = backend
df = DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": np.array([7, 8, 9], dtype=dtype)}
)
df_orig = df.copy()
# modify subset -> don't modify parent
subset = df[:]["a"][0:2]
df._clear_item_cache()
subset.iloc[0] = 0
if using_copy_on_write:
tm.assert_frame_equal(df, df_orig)
else:
assert df.iloc[0, 0] == 0
# modify parent -> don't modify subset
subset = df[:]["a"][0:2]
df._clear_item_cache()
df.iloc[0, 0] = 0
expected = Series([1, 2], name="a")
if using_copy_on_write:
tm.assert_series_equal(subset, expected)
else:
assert subset.iloc[0] == 0
@pytest.mark.parametrize(
"method",
[
lambda s: s["a":"c"]["a":"b"], # type: ignore[misc]
lambda s: s.iloc[0:3].iloc[0:2],
lambda s: s.loc["a":"c"].loc["a":"b"], # type: ignore[misc]
lambda s: s.loc["a":"c"] # type: ignore[misc]
.iloc[0:3]
.iloc[0:2]
.loc["a":"b"] # type: ignore[misc]
.iloc[0:1],
],
ids=["getitem", "iloc", "loc", "long-chain"],
)
def test_subset_chained_getitem_series(backend, method, using_copy_on_write):
# Case: creating a subset using multiple, chained getitem calls using views
# still needs to guarantee proper CoW behaviour
_, _, Series = backend
s = Series([1, 2, 3], index=["a", "b", "c"])
s_orig = s.copy()
# modify subset -> don't modify parent
subset = method(s)
subset.iloc[0] = 0
if using_copy_on_write:
tm.assert_series_equal(s, s_orig)
else:
assert s.iloc[0] == 0
# modify parent -> don't modify subset
subset = s.iloc[0:3].iloc[0:2]
s.iloc[0] = 0
expected = Series([1, 2], index=["a", "b"])
if using_copy_on_write:
tm.assert_series_equal(subset, expected)
else:
assert subset.iloc[0] == 0
def test_subset_chained_single_block_row(using_copy_on_write, using_array_manager):
# not parametrizing this for dtype backend, since this explicitly tests single block
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
df_orig = df.copy()
# modify subset -> don't modify parent
subset = df[:].iloc[0].iloc[0:2]
subset.iloc[0] = 0
if using_copy_on_write or using_array_manager:
tm.assert_frame_equal(df, df_orig)
else:
assert df.iloc[0, 0] == 0
# modify parent -> don't modify subset
subset = df[:].iloc[0].iloc[0:2]
df.iloc[0, 0] = 0
expected = Series([1, 4], index=["a", "b"], name=0)
if using_copy_on_write or using_array_manager:
tm.assert_series_equal(subset, expected)
else:
assert subset.iloc[0] == 0
@pytest.mark.parametrize(
"method",
[
lambda df: df[:],
lambda df: df.loc[:, :],
lambda df: df.loc[:],
lambda df: df.iloc[:, :],
lambda df: df.iloc[:],
],
ids=["getitem", "loc", "loc-rows", "iloc", "iloc-rows"],
)
def test_null_slice(backend, method, using_copy_on_write):
# Case: also all variants of indexing with a null slice (:) should return
# new objects to ensure we correctly use CoW for the results
_, DataFrame, _ = backend
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
df_orig = df.copy()
df2 = method(df)
# we always return new objects (shallow copy), regardless of CoW or not
assert df2 is not df
# and those trigger CoW when mutated
df2.iloc[0, 0] = 0
if using_copy_on_write:
tm.assert_frame_equal(df, df_orig)
else:
assert df.iloc[0, 0] == 0
@pytest.mark.parametrize(
"method",
[
lambda s: s[:],
lambda s: s.loc[:],
lambda s: s.iloc[:],
],
ids=["getitem", "loc", "iloc"],
)
def test_null_slice_series(backend, method, using_copy_on_write):
_, _, Series = backend
s = Series([1, 2, 3], index=["a", "b", "c"])
s_orig = s.copy()
s2 = method(s)
# we always return new objects, regardless of CoW or not
assert s2 is not s
# and those trigger CoW when mutated
s2.iloc[0] = 0
if using_copy_on_write:
tm.assert_series_equal(s, s_orig)
else:
assert s.iloc[0] == 0
# TODO add more tests modifying the parent
# -----------------------------------------------------------------------------
# Series -- Indexing operations taking subset + modifying the subset/parent
def test_series_getitem_slice(backend, using_copy_on_write):
# Case: taking a slice of a Series + afterwards modifying the subset
_, _, Series = backend
s = Series([1, 2, 3], index=["a", "b", "c"])
s_orig = s.copy()
subset = s[:]
assert np.shares_memory(get_array(subset), get_array(s))
subset.iloc[0] = 0
if using_copy_on_write:
assert not np.shares_memory(get_array(subset), get_array(s))
expected = Series([0, 2, 3], index=["a", "b", "c"])
tm.assert_series_equal(subset, expected)
if using_copy_on_write:
# original parent series is not modified (CoW)
tm.assert_series_equal(s, s_orig)
else:
# original parent series is actually updated
assert s.iloc[0] == 0
@pytest.mark.parametrize(
"indexer",
[slice(0, 2), np.array([True, True, False]), np.array([0, 1])],
ids=["slice", "mask", "array"],
)
def test_series_subset_set_with_indexer(
backend, indexer_si, indexer, using_copy_on_write
):
# Case: setting values in a viewing Series with an indexer
_, _, Series = backend
s = Series([1, 2, 3], index=["a", "b", "c"])
s_orig = s.copy()
subset = s[:]
warn = None
msg = "Series.__setitem__ treating keys as positions is deprecated"
if (
indexer_si is tm.setitem
and isinstance(indexer, np.ndarray)
and indexer.dtype.kind == "i"
):
warn = FutureWarning
with tm.assert_produces_warning(warn, match=msg):
indexer_si(subset)[indexer] = 0
expected = Series([0, 0, 3], index=["a", "b", "c"])
tm.assert_series_equal(subset, expected)
if using_copy_on_write:
tm.assert_series_equal(s, s_orig)
else:
tm.assert_series_equal(s, expected)
# -----------------------------------------------------------------------------
# del operator
def test_del_frame(backend, using_copy_on_write):
# Case: deleting a column with `del` on a viewing child dataframe should
# not modify parent + update the references
_, DataFrame, _ = backend
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
df2 = df[:]
assert np.shares_memory(get_array(df, "a"), get_array(df2, "a"))
del df2["b"]
assert np.shares_memory(get_array(df, "a"), get_array(df2, "a"))
tm.assert_frame_equal(df, df_orig)
tm.assert_frame_equal(df2, df_orig[["a", "c"]])
df2._mgr._verify_integrity()
# TODO in theory modifying column "b" of the parent wouldn't need a CoW
# but the weakref is still alive and so we still perform CoW
df2.loc[0, "a"] = 100
if using_copy_on_write:
# modifying child after deleting a column still doesn't update parent
tm.assert_frame_equal(df, df_orig)
else:
assert df.loc[0, "a"] == 100
def test_del_series(backend):
_, _, Series = backend
s = Series([1, 2, 3], index=["a", "b", "c"])
s_orig = s.copy()
s2 = s[:]
assert np.shares_memory(get_array(s), get_array(s2))
del s2["a"]
assert not np.shares_memory(get_array(s), get_array(s2))
tm.assert_series_equal(s, s_orig)
tm.assert_series_equal(s2, s_orig[["b", "c"]])
# modifying s2 doesn't need copy on write (due to `del`, s2 is backed by new array)
values = s2.values
s2.loc["b"] = 100
assert values[0] == 100
# -----------------------------------------------------------------------------
# Accessing column as Series
def test_column_as_series(backend, using_copy_on_write, using_array_manager):
# Case: selecting a single column now also uses Copy-on-Write
dtype_backend, DataFrame, Series = backend
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
s = df["a"]
assert np.shares_memory(get_array(s, "a"), get_array(df, "a"))
if using_copy_on_write or using_array_manager:
s[0] = 0
else:
warn = SettingWithCopyWarning if dtype_backend == "numpy" else None
with pd.option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(warn):
s[0] = 0
expected = Series([0, 2, 3], name="a")
tm.assert_series_equal(s, expected)
if using_copy_on_write:
# assert not np.shares_memory(s.values, get_array(df, "a"))
tm.assert_frame_equal(df, df_orig)
# ensure cached series on getitem is not the changed series
tm.assert_series_equal(df["a"], df_orig["a"])
else:
df_orig.iloc[0, 0] = 0
tm.assert_frame_equal(df, df_orig)
def test_column_as_series_set_with_upcast(
backend, using_copy_on_write, using_array_manager
):
# Case: selecting a single column now also uses Copy-on-Write -> when
# setting a value causes an upcast, we don't need to update the parent
# DataFrame through the cache mechanism
dtype_backend, DataFrame, Series = backend
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
s = df["a"]
if dtype_backend == "nullable":
with pytest.raises(TypeError, match="Invalid value"):
s[0] = "foo"
expected = Series([1, 2, 3], name="a")
elif using_copy_on_write or using_array_manager:
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
s[0] = "foo"
expected = Series(["foo", 2, 3], dtype=object, name="a")
else:
with pd.option_context("chained_assignment", "warn"):
msg = "|".join(
[
"A value is trying to be set on a copy of a slice from a DataFrame",
"Setting an item of incompatible dtype is deprecated",
]
)
with tm.assert_produces_warning(
(SettingWithCopyWarning, FutureWarning), match=msg
):
s[0] = "foo"
expected = Series(["foo", 2, 3], dtype=object, name="a")
tm.assert_series_equal(s, expected)
if using_copy_on_write:
tm.assert_frame_equal(df, df_orig)
# ensure cached series on getitem is not the changed series
tm.assert_series_equal(df["a"], df_orig["a"])
else:
df_orig["a"] = expected
tm.assert_frame_equal(df, df_orig)
@pytest.mark.parametrize(
"method",
[
lambda df: df["a"],
lambda df: df.loc[:, "a"],
lambda df: df.iloc[:, 0],
],
ids=["getitem", "loc", "iloc"],
)
def test_column_as_series_no_item_cache(
request, backend, method, using_copy_on_write, using_array_manager
):
# Case: selecting a single column (which now also uses Copy-on-Write to protect
# the view) should always give a new object (i.e. not make use of a cache)
dtype_backend, DataFrame, _ = backend
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
s1 = method(df)
s2 = method(df)
is_iloc = "iloc" in request.node.name
if using_copy_on_write or is_iloc:
assert s1 is not s2
else:
assert s1 is s2
if using_copy_on_write or using_array_manager:
s1.iloc[0] = 0
else:
warn = SettingWithCopyWarning if dtype_backend == "numpy" else None
with pd.option_context("chained_assignment", "warn"):
with tm.assert_produces_warning(warn):
s1.iloc[0] = 0
if using_copy_on_write:
tm.assert_series_equal(s2, df_orig["a"])
tm.assert_frame_equal(df, df_orig)
else:
assert s2.iloc[0] == 0
# TODO add tests for other indexing methods on the Series
def test_dataframe_add_column_from_series(backend, using_copy_on_write):
# Case: adding a new column to a DataFrame from an existing column/series