Home > database >  Pandas slice using rows with column labels inside iteration gives future warning
Pandas slice using rows with column labels inside iteration gives future warning

Time:08-24

This code works inside a for loop where 'i' is iterating it through the dataframe rows and occasionally this code line is used to reset all the rows preceeding row 'i', in column 'atr_Lts', so my cummulation calcs work as I require.

cummax.loc[:i, 'atr_Lts'] = cummax.loc[cummax.index[i], 'atr_Lts']

It works fine, but it is giving me the following warning, and I cannot find a better approach.

FutureWarning: Slicing a positional slice with .loc is not supported, and will raise TypeError in a future version. Use .loc with labels or .iloc with positions instead.

I need to be able to set all rows in column 'atr_Lts' up to the current iteration i, to the current 'atr_Lts' value. To get at that I need to use row selection (:i) and column label ('atr_Lts') but according to the warning I can not combine the approach.

In many searches this was the recommended method, but is going to be obselete so wondered what the better approach is.

EDIT: copy of example cummax dataframe print

Date          Close       atr     atr_Lts     atr_Sts       atrts
2022-02-23  421.950012       NaN  371.614245  413.538855  413.538855
2022-02-24  428.299988       NaN  371.614245  413.538855  413.538855
2022-02-25  437.750000       NaN  371.614245  413.538855  413.538855
2022-02-28  436.630005       NaN  371.614245  413.538855  413.538855
2022-03-01  429.980011       NaN  371.614245  413.538855  413.538855

this is the print(cummax.info())

Data columns (total 5 columns):
 #   Column   Non-Null Count  Dtype
---  ------   --------------  -----
 0   Close    125 non-null    float64
 1   atr      112 non-null    float64
 2   atr_Lts  125 non-null    float64
 3   atr_Sts  112 non-null    float64
 4   atrts    112 non-null    float64
dtypes: float64(5)

CodePudding user response:

This warning is raised because .loc requires a label based index, you passed and integer based index: .loc[:i, <some columns>] (i is an int), therefore causing pandas to be confused.

To make this warning go away, you can either try:

  • Ignore the warnings using the warnings module.
  • Typing this instead: cummax.iloc[:i].loc[:, 'atr_Lts'] = ....
  • Related