22
33from collections import (
44 OrderedDict ,
5- UserList ,
65 defaultdict ,
7- deque ,
86)
97from collections .abc import (
108 Callable ,
2927 TypeAlias ,
3028 TypedDict ,
3129 TypeVar ,
32- cast ,
3330)
3431
3532import numpy as np
36- import numpy .typing as npt
3733import pandas as pd
3834from pandas .api .typing import NAType
3935from pandas .core .resample import (
@@ -2798,19 +2794,6 @@ def test_loop_dataframe() -> None:
27982794 check (assert_type (df [c ], pd .Series ), pd .Series )
27992795
28002796
2801- def test_iloc_npint () -> None :
2802- # GH 69
2803- df = pd .DataFrame ({"a" : [10 , 20 , 30 ], "b" : [20 , 40 , 60 ], "c" : [30 , 60 , 90 ]})
2804- iloc = np .argmin (np .random .standard_normal (3 ))
2805- df .iloc [iloc ]
2806-
2807-
2808- # https://github.com/pandas-dev/pandas-stubs/issues/143
2809- def test_iloc_tuple () -> None :
2810- df = pd .DataFrame ({"Char" : ["A" , "B" , "C" ], "Number" : [1 , 2 , 3 ]})
2811- df = df .iloc [0 :2 ,]
2812-
2813-
28142797def test_take () -> None :
28152798 df = pd .DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ]})
28162799 check (assert_type (df .take ([0 , 1 ]), pd .DataFrame ), pd .DataFrame )
@@ -2936,15 +2919,6 @@ def test_frame_reindex_like() -> None:
29362919 )
29372920
29382921
2939- def test_frame_ndarray_assignmment () -> None :
2940- # GH 100
2941- df_a = pd .DataFrame ({"a" : [0.0 ] * 10 })
2942- df_a .iloc [:, :] = np .array ([[- 1.0 ]] * 10 )
2943-
2944- df_b = pd .DataFrame ({"a" : [0.0 ] * 10 , "b" : [1.0 ] * 10 })
2945- df_b .iloc [:, :] = np .array ([[- 1.0 , np .inf ]] * 10 )
2946-
2947-
29482922def test_not_hashable () -> None :
29492923 # GH 113
29502924 check (assert_type (pd .DataFrame .__hash__ , None ), type (None ))
@@ -3000,45 +2974,6 @@ def test_squeeze() -> None:
30002974 check (assert_type (df4 .squeeze (), pd .DataFrame | pd .Series | Scalar ), np .integer )
30012975
30022976
3003- def test_loc_set () -> None :
3004- df = pd .DataFrame ({"a" : [1 , 2 ], "b" : [3 , 4 ]})
3005- df .loc ["a" ] = [3 , 4 ]
3006-
3007-
3008- def test_loc_int_set () -> None :
3009- df = pd .DataFrame ({1 : [1 , 2 ], 2 : [3 , 4 ]})
3010- df .loc [1 ] = [3 , 4 ]
3011- df .loc [np .int_ (1 )] = pd .Series ([1 , 2 ])
3012- df .loc [np .uint (1 )] = pd .Series ([1 , 2 ])
3013- df .loc [np .int8 (1 )] = pd .Series ([1 , 2 ])
3014- df .loc [np .int32 (1 )] = [2 , 3 ]
3015- df .loc [np .uint64 (1 )] = [2 , 3 ]
3016-
3017-
3018- @pytest .mark .parametrize ("col" , [1 , None ])
3019- @pytest .mark .parametrize ("typ" , [list , tuple , deque , UserList , iter ])
3020- def test_loc_iterable (col : Hashable , typ : type ) -> None :
3021- # GH 189, GH 1410
3022- df = pd .DataFrame ({1 : [1 , 2 ], None : 5 }, columns = pd .Index ([1 , None ], dtype = object ))
3023- check (df .loc [:, typ ([col ])], pd .DataFrame )
3024-
3025- if TYPE_CHECKING :
3026- assert_type (df .loc [:, [None ]], pd .DataFrame )
3027- assert_type (df .loc [:, [1 ]], pd .DataFrame )
3028-
3029- assert_type (df .loc [:, (None ,)], pd .DataFrame )
3030- assert_type (df .loc [:, (1 ,)], pd .DataFrame )
3031-
3032- assert_type (df .loc [:, deque ([None ])], pd .DataFrame )
3033- assert_type (df .loc [:, deque ([1 ])], pd .DataFrame )
3034-
3035- assert_type (df .loc [:, UserList ([None ])], pd .DataFrame )
3036- assert_type (df .loc [:, UserList ([1 ])], pd .DataFrame )
3037-
3038- assert_type (df .loc [:, (None for _ in [0 ])], pd .DataFrame )
3039- assert_type (df .loc [:, (1 for _ in [0 ])], pd .DataFrame )
3040-
3041-
30422977def test_dict_items () -> None :
30432978 # GH 180
30442979 x = {"a" : [1 ]}
@@ -3202,28 +3137,6 @@ def test_xs_key() -> None:
32023137 check (assert_type (df .xs (0 , level = "foo" ), pd .DataFrame | pd .Series ), pd .DataFrame )
32033138
32043139
3205- def test_loc_slice () -> None :
3206- """Test DataFrame.loc with a slice, Index, Series."""
3207- # GH277
3208- df1 = pd .DataFrame (
3209- {"x" : [1 , 2 , 3 , 4 ]},
3210- index = pd .MultiIndex .from_product ([[1 , 2 ], ["a" , "b" ]], names = ["num" , "let" ]),
3211- )
3212- check (assert_type (df1 .loc [1 , :], pd .Series | pd .DataFrame ), pd .DataFrame )
3213- check (assert_type (df1 [::- 1 ], pd .DataFrame ), pd .DataFrame )
3214-
3215- # GH1299
3216- ind = pd .Index (["a" , "b" ])
3217- mask = pd .Series ([True , False ])
3218- mask_col = pd .Series ([True , False ], index = pd .Index (["a" , "b" ]))
3219- df = pd .DataFrame ({"a" : [1 , 2 ], "b" : [3 , 4 ]})
3220-
3221- # loc with index for columns
3222- check (assert_type (df .loc [mask , ind ], pd .DataFrame ), pd .DataFrame )
3223- # loc with index for columns
3224- check (assert_type (df .loc [mask , mask_col ], pd .DataFrame ), pd .DataFrame )
3225-
3226-
32273140def where_cond1 (x : int ) -> bool :
32283141 return x % 2 == 0
32293142
@@ -3386,44 +3299,6 @@ def test_frame_dropna_subset() -> None:
33863299 )
33873300
33883301
3389- def test_loc_callable () -> None :
3390- # GH 256
3391- df = pd .DataFrame ({"x" : [1 , 2 , 3 ], "y" : [4 , 5 , 6 ]})
3392-
3393- def select1 (df : pd .DataFrame ) -> pd .Series :
3394- return df ["x" ] > 2.0
3395-
3396- check (assert_type (df .loc [select1 ], pd .DataFrame ), pd .DataFrame )
3397- check (assert_type (df .loc [select1 , :], pd .DataFrame ), pd .DataFrame )
3398-
3399- def select2 (df : pd .DataFrame ) -> list [Hashable ]:
3400- return [i for i in df .index if cast (int , i ) % 2 == 1 ]
3401-
3402- check (assert_type (df .loc [select2 , "x" ], pd .Series ), pd .Series )
3403-
3404- def select3 (_ : pd .DataFrame ) -> int :
3405- return 1
3406-
3407- check (assert_type (df .loc [select3 , "x" ], Scalar ), np .integer )
3408-
3409- check (
3410- assert_type (df .loc [:, lambda df : df .columns .str .startswith ("x" )], pd .DataFrame ),
3411- pd .DataFrame ,
3412- )
3413-
3414-
3415- def test_npint_loc_indexer () -> None :
3416- # GH 508
3417-
3418- df = pd .DataFrame ({"x" : [1 , 2 , 3 ]}, index = np .array ([10 , 20 , 30 ], dtype = "uint64" ))
3419-
3420- def get_NDArray (df : pd .DataFrame , key : npt .NDArray [np .uint64 ]) -> pd .DataFrame :
3421- return df .loc [key ]
3422-
3423- a : npt .NDArray [np .uint64 ] = np .array ([10 , 30 ], dtype = "uint64" )
3424- check (assert_type (get_NDArray (df , a ), pd .DataFrame ), pd .DataFrame )
3425-
3426-
34273302def test_in_columns () -> None :
34283303 # GH 532 (PR)
34293304 df = pd .DataFrame (np .random .random ((3 , 4 )), columns = ["cat" , "dog" , "rat" , "pig" ])
@@ -3434,18 +3309,6 @@ def test_in_columns() -> None:
34343309 check (assert_type (df .groupby (by = cols ).sum (), pd .DataFrame ), pd .DataFrame )
34353310
34363311
3437- def test_loc_list_str () -> None :
3438- # GH 1162 (PR)
3439- df = pd .DataFrame (
3440- [[1 , 2 ], [4 , 5 ], [7 , 8 ]],
3441- index = ["cobra" , "viper" , "sidewinder" ],
3442- columns = ["max_speed" , "shield" ],
3443- )
3444-
3445- result = df .loc [["viper" , "sidewinder" ]]
3446- check (assert_type (result , pd .DataFrame ), pd .DataFrame )
3447-
3448-
34493312def test_insert_newvalues () -> None :
34503313 df = pd .DataFrame ({"a" : [1 , 2 ]})
34513314 ab = pd .DataFrame ({"col1" : [1 , 2 ], "col2" : [3 , 4 ]})
@@ -3563,12 +3426,6 @@ def test_align() -> None:
35633426 check (assert_type (aligned_df1 , pd .DataFrame ), pd .DataFrame )
35643427
35653428
3566- def test_loc_returns_series () -> None :
3567- df1 = pd .DataFrame ({"x" : [1 , 2 , 3 , 4 ]}, index = [10 , 20 , 30 , 40 ])
3568- df2 = df1 .loc [10 , :]
3569- check (assert_type (df2 , pd .Series | pd .DataFrame ), pd .Series )
3570-
3571-
35723429def test_to_dict_index () -> None :
35733430 df = pd .DataFrame ({"a" : [1 , 2 ], "b" : [9 , 10 ]})
35743431 check (
@@ -3785,28 +3642,6 @@ def test_info() -> None:
37853642 check (assert_type (df .info (show_counts = None ), None ), type (None ))
37863643
37873644
3788- def test_frame_single_slice () -> None :
3789- # GH 572
3790- df = pd .DataFrame ([1 , 2 , 3 ])
3791- check (assert_type (df .loc [:], pd .DataFrame ), pd .DataFrame )
3792-
3793- df .loc [:] = 1 + df
3794-
3795-
3796- def test_frame_index_timestamp () -> None :
3797- # GH 620
3798- dt1 = pd .to_datetime ("2023-05-01" )
3799- dt2 = pd .to_datetime ("2023-05-02" )
3800- s = pd .Series ([1 , 2 ], index = [dt1 , dt2 ])
3801- df = pd .DataFrame (s )
3802- # Next result is Series or DataFrame because the index could be a MultiIndex
3803- check (assert_type (df .loc [dt1 , :], pd .Series | pd .DataFrame ), pd .Series )
3804- check (assert_type (df .loc [[dt1 ], :], pd .DataFrame ), pd .DataFrame )
3805- df2 = pd .DataFrame ({"x" : s })
3806- check (assert_type (df2 .loc [dt1 , "x" ], Scalar ), np .integer )
3807- check (assert_type (df2 .loc [[dt1 ], "x" ], pd .Series ), pd .Series , np .integer )
3808-
3809-
38103645def test_frame_bool_fails () -> None :
38113646 # GH 663
38123647
@@ -3891,16 +3726,6 @@ def test_combine() -> None:
38913726 )
38923727
38933728
3894- def test_df_loc_dict () -> None :
3895- """Test that we can set a dict to a df.loc result GH1203."""
3896- df = pd .DataFrame (columns = ["X" ])
3897- df .loc [0 ] = {"X" : 0 }
3898- check (assert_type (df , pd .DataFrame ), pd .DataFrame )
3899-
3900- df .iloc [0 ] = {"X" : 0 }
3901- check (assert_type (df , pd .DataFrame ), pd .DataFrame )
3902-
3903-
39043729def test_unstack () -> None :
39053730 """Test different types of argument for `fill_value` in DataFrame.unstack."""
39063731 df = pd .DataFrame (
0 commit comments