@@ -57,27 +57,32 @@ def test_basic_getitem_with_labels(datetime_series):
5757 expected = datetime_series .loc [indices [0 ] : indices [2 ]]
5858 tm .assert_series_equal (result , expected )
5959
60+
61+ def test_basic_getitem_with_integer_labels ():
6062 # integer indexes, be careful
61- s = Series (np .random .randn (10 ), index = list (range (0 , 20 , 2 )))
63+ ser = Series (np .random .randn (10 ), index = list (range (0 , 20 , 2 )))
6264 inds = [0 , 2 , 5 , 7 , 8 ]
6365 arr_inds = np .array ([0 , 2 , 5 , 7 , 8 ])
6466 with pytest .raises (KeyError , match = "with any missing labels" ):
65- s [inds ]
67+ ser [inds ]
6668
6769 with pytest .raises (KeyError , match = "with any missing labels" ):
68- s [arr_inds ]
70+ ser [arr_inds ]
71+
72+
73+ def test_basic_getitem_dt64tz_values ():
6974
7075 # GH12089
7176 # with tz for values
72- s = Series (
77+ ser = Series (
7378 pd .date_range ("2011-01-01" , periods = 3 , tz = "US/Eastern" ), index = ["a" , "b" , "c" ]
7479 )
7580 expected = Timestamp ("2011-01-01" , tz = "US/Eastern" )
76- result = s .loc ["a" ]
81+ result = ser .loc ["a" ]
7782 assert result == expected
78- result = s .iloc [0 ]
83+ result = ser .iloc [0 ]
7984 assert result == expected
80- result = s ["a" ]
85+ result = ser ["a" ]
8186 assert result == expected
8287
8388
@@ -93,33 +98,13 @@ def test_getitem_setitem_ellipsis():
9398 assert (result == 5 ).all ()
9499
95100
96- def test_getitem_get (datetime_series , string_series , object_series ):
97- idx1 = string_series .index [5 ]
98- idx2 = object_series .index [5 ]
99-
100- assert string_series [idx1 ] == string_series .get (idx1 )
101- assert object_series [idx2 ] == object_series .get (idx2 )
102-
103- assert string_series [idx1 ] == string_series [5 ]
104- assert object_series [idx2 ] == object_series [5 ]
105-
106- assert string_series .get (- 1 ) == string_series .get (string_series .index [- 1 ])
107- assert string_series [5 ] == string_series .get (string_series .index [5 ])
108-
101+ def test_getitem_missing (datetime_series ):
109102 # missing
110103 d = datetime_series .index [0 ] - BDay ()
111104 msg = r"Timestamp\('1999-12-31 00:00:00', freq='B'\)"
112105 with pytest .raises (KeyError , match = msg ):
113106 datetime_series [d ]
114107
115- # None
116- # GH 5652
117- s1 = Series (dtype = object )
118- s2 = Series (dtype = object , index = list ("abc" ))
119- for s in [s1 , s2 ]:
120- result = s .get (None )
121- assert result is None
122-
123108
124109def test_getitem_fancy (string_series , object_series ):
125110 slice1 = string_series [[1 , 2 , 3 ]]
@@ -180,14 +165,18 @@ def test_getitem_box_float64(datetime_series):
180165def test_series_box_timestamp ():
181166 rng = pd .date_range ("20090415" , "20090519" , freq = "B" )
182167 ser = Series (rng )
168+ assert isinstance (ser [0 ], Timestamp )
169+ assert isinstance (ser .at [1 ], Timestamp )
170+ assert isinstance (ser .iat [2 ], Timestamp )
171+ assert isinstance (ser .loc [3 ], Timestamp )
172+ assert isinstance (ser .iloc [4 ], Timestamp )
183173
184- assert isinstance (ser [5 ], Timestamp )
185-
186- rng = pd .date_range ("20090415" , "20090519" , freq = "B" )
187174 ser = Series (rng , index = rng )
188- assert isinstance (ser [5 ], Timestamp )
189-
190- assert isinstance (ser .iat [5 ], Timestamp )
175+ assert isinstance (ser [0 ], Timestamp )
176+ assert isinstance (ser .at [rng [1 ]], Timestamp )
177+ assert isinstance (ser .iat [2 ], Timestamp )
178+ assert isinstance (ser .loc [rng [3 ]], Timestamp )
179+ assert isinstance (ser .iloc [4 ], Timestamp )
191180
192181
193182def test_series_box_timedelta ():
@@ -256,19 +245,23 @@ def test_setitem(datetime_series, string_series):
256245 datetime_series [np .isnan (datetime_series )] = 5
257246 assert not np .isnan (datetime_series [2 ])
258247
248+
249+ def test_setitem_slicestep ():
259250 # caught this bug when writing tests
260251 series = Series (tm .makeIntIndex (20 ).astype (float ), index = tm .makeIntIndex (20 ))
261252
262253 series [::2 ] = 0
263254 assert (series [::2 ] == 0 ).all ()
264255
256+
257+ def test_setitem_not_contained (string_series ):
265258 # set item that's not contained
266- s = string_series .copy ()
267- s ["foobar" ] = 1
259+ ser = string_series .copy ()
260+ ser ["foobar" ] = 1
268261
269262 app = Series ([1 ], index = ["foobar" ], name = "series" )
270263 expected = string_series .append (app )
271- tm .assert_series_equal (s , expected )
264+ tm .assert_series_equal (ser , expected )
272265
273266
274267def test_setslice (datetime_series ):
@@ -451,11 +444,13 @@ def test_categorical_assigning_ops():
451444 exp = Series (Categorical (["b" , "a" ], categories = ["a" , "b" ]), index = ["x" , "y" ])
452445 tm .assert_series_equal (s , exp )
453446
447+
448+ def test_setitem_nan_into_categorical ():
454449 # ensure that one can set something to np.nan
455- s = Series (Categorical ([1 , 2 , 3 ]))
450+ ser = Series (Categorical ([1 , 2 , 3 ]))
456451 exp = Series (Categorical ([1 , np .nan , 3 ], categories = [1 , 2 , 3 ]))
457- s [1 ] = np .nan
458- tm .assert_series_equal (s , exp )
452+ ser [1 ] = np .nan
453+ tm .assert_series_equal (ser , exp )
459454
460455
461456def test_getitem_categorical_str ():
@@ -537,12 +532,14 @@ def test_timedelta_assignment():
537532 expected = Series (Timedelta ("1 days" ), index = ["A" , "B" ])
538533 tm .assert_series_equal (s , expected )
539534
535+
536+ def test_setitem_td64_non_nano ():
540537 # GH 14155
541- s = Series (10 * [np .timedelta64 (10 , "m" )])
542- s .loc [[1 , 2 , 3 ]] = np .timedelta64 (20 , "m" )
538+ ser = Series (10 * [np .timedelta64 (10 , "m" )])
539+ ser .loc [[1 , 2 , 3 ]] = np .timedelta64 (20 , "m" )
543540 expected = Series (10 * [np .timedelta64 (10 , "m" )])
544541 expected .loc [[1 , 2 , 3 ]] = Timedelta (np .timedelta64 (20 , "m" ))
545- tm .assert_series_equal (s , expected )
542+ tm .assert_series_equal (ser , expected )
546543
547544
548545@pytest .mark .parametrize (
@@ -612,23 +609,25 @@ def test_underlying_data_conversion():
612609 assert return_value is None
613610 tm .assert_frame_equal (df , expected )
614611
612+
613+ def test_chained_assignment ():
615614 # GH 3970
616- # these are chained assignments as well
617- pd .set_option ("chained_assignment" , None )
618- df = DataFrame ({"aa" : range (5 ), "bb" : [2.2 ] * 5 })
619- df ["cc" ] = 0.0
615+ with pd .option_context ("chained_assignment" , None ):
616+ df = DataFrame ({"aa" : range (5 ), "bb" : [2.2 ] * 5 })
617+ df ["cc" ] = 0.0
618+
619+ ck = [True ] * len (df )
620620
621- ck = [ True ] * len ( df )
621+ df [ "bb" ]. iloc [ 0 ] = 0.13
622622
623- df ["bb" ].iloc [0 ] = 0.13
623+ # TODO: unused
624+ df_tmp = df .iloc [ck ] # noqa
624625
625- # TODO: unused
626- df_tmp = df .iloc [ck ] # noqa
626+ df [ "bb" ]. iloc [ 0 ] = 0.15
627+ assert df [ "bb" ] .iloc [0 ] == 0.15
627628
628- df ["bb" ].iloc [0 ] = 0.15
629- assert df ["bb" ].iloc [0 ] == 0.15
630- pd .set_option ("chained_assignment" , "raise" )
631629
630+ def test_setitem_with_expansion_dtype ():
632631 # GH 3217
633632 df = DataFrame ({"a" : [1 , 3 ], "b" : [np .nan , 2 ]})
634633 df ["c" ] = np .nan
@@ -663,17 +662,27 @@ def test_type_promote_putmask():
663662 left [mask ] = right
664663 tm .assert_series_equal (left , ts .map (lambda t : str (t ) if t > 0 else t ))
665664
666- s = Series ([0 , 1 , 2 , 0 ])
667- mask = s > 0
668- s2 = s [mask ].map (str )
669- s [mask ] = s2
670- tm .assert_series_equal (s , Series ([0 , "1" , "2" , 0 ]))
671665
672- s = Series ([0 , "foo" , "bar" , 0 ])
666+ def test_setitem_mask_promote_strs ():
667+
668+ ser = Series ([0 , 1 , 2 , 0 ])
669+ mask = ser > 0
670+ ser2 = ser [mask ].map (str )
671+ ser [mask ] = ser2
672+
673+ expected = Series ([0 , "1" , "2" , 0 ])
674+ tm .assert_series_equal (ser , expected )
675+
676+
677+ def test_setitem_mask_promote ():
678+
679+ ser = Series ([0 , "foo" , "bar" , 0 ])
673680 mask = Series ([False , True , True , False ])
674- s2 = s [mask ]
675- s [mask ] = s2
676- tm .assert_series_equal (s , Series ([0 , "foo" , "bar" , 0 ]))
681+ ser2 = ser [mask ]
682+ ser [mask ] = ser2
683+
684+ expected = Series ([0 , "foo" , "bar" , 0 ])
685+ tm .assert_series_equal (ser , expected )
677686
678687
679688def test_multilevel_preserve_name ():
@@ -716,16 +725,8 @@ def test_getitem_unrecognized_scalar():
716725 assert result == 2
717726
718727
719- @pytest .mark .parametrize (
720- "index" ,
721- [
722- date_range ("2014-01-01" , periods = 20 , freq = "MS" ),
723- period_range ("2014-01" , periods = 20 , freq = "M" ),
724- timedelta_range ("0" , periods = 20 , freq = "H" ),
725- ],
726- )
727728def test_slice_with_zero_step_raises (index , frame_or_series , indexer_sli ):
728- ts = frame_or_series (np .arange (20 ), index = index )
729+ ts = frame_or_series (np .arange (len ( index ) ), index = index )
729730
730731 with pytest .raises (ValueError , match = "slice step cannot be zero" ):
731732 indexer_sli (ts )[::0 ]
0 commit comments