File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed
Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -5866,7 +5866,9 @@ def shift(
58665866 freq : Frequency | None = None ,
58675867 axis : Axis = 0 ,
58685868 fill_value : Hashable = lib .no_default ,
5869+ prefix : str | None = None ,
58695870 suffix : str | None = None ,
5871+ sep : str = "_" ,
58705872 ) -> DataFrame :
58715873 if freq is not None and fill_value is not lib .no_default :
58725874 # GH#53832
@@ -5899,11 +5901,17 @@ def shift(
58995901 shifted_dataframes .append (
59005902 super ()
59015903 .shift (periods = period , freq = freq , axis = axis , fill_value = fill_value )
5904+ .add_prefix (f"{ prefix } { sep } " if prefix else "" )
59025905 .add_suffix (f"{ suffix } _{ period } " if suffix else f"_{ period } " )
5906+ .rename (
5907+ columns = lambda col : col .replace (f"{ sep } { sep } " , sep )
5908+ )
59035909 )
59045910 return concat (shifted_dataframes , axis = 1 )
59055911 elif suffix :
59065912 raise ValueError ("Cannot specify `suffix` if `periods` is an int." )
5913+ elif prefix :
5914+ raise ValueError ("Cannot specify `prefix` if `periods` is an int." )
59075915 periods = cast (int , periods )
59085916
59095917 ncols = len (self .columns )
Original file line number Diff line number Diff line change @@ -725,6 +725,16 @@ def test_shift_with_iterable_check_other_arguments(self):
725725 expected = DataFrame ({"a_suffix_0" : [1 , 2 ], "a_suffix_1" : [np .nan , 1.0 ]})
726726 tm .assert_frame_equal (shifted , expected )
727727
728+ # test prefix and sep
729+ shifted = df [["a" ]].shift (shifts , prefix = "pre" , suffix = "suf" , sep = "-" )
730+ expected = DataFrame (
731+ {
732+ "pre-a-suf-0" : [1 , 2 ],
733+ "pre-a-suf-1" : [np .nan , 1.0 ]
734+ }
735+ )
736+ tm .assert_frame_equal (shifted , expected )
737+
728738 # check bad inputs when doing multiple shifts
729739 msg = "If `periods` contains multiple shifts, `axis` cannot be 1."
730740 with pytest .raises (ValueError , match = msg ):
You can’t perform that action at this time.
0 commit comments