Home > Mobile >  How do I prevent Pandas from counting from the end when I locate a row with a negative index?
How do I prevent Pandas from counting from the end when I locate a row with a negative index?

Time:12-25

I have a DataFrame msftHist as shown below

                             Open        High  ...  Dividends  Stock Splits
              Date                                ...                         
              1986-03-13    0.055898    0.064119  ...        0.0           0.0
              1986-03-14    0.061378    0.064667  ...        0.0           0.0
              1986-03-17    0.063570    0.065215  ...        0.0           0.0
              ...                ...         ...  ...        ...           ...
              2021-12-17  320.880005  324.920013  ...        0.0           0.0
              2021-12-20  320.049988  322.799988  ...        0.0           0.0
              2021-12-21  323.290009  327.730011  ...        0.0           0.0
              2021-12-22  328.299988  333.609985  ...        0.0           0.0
              2021-12-23  332.750000  336.390015  ...        0.0           0.0

and when I call msftHist.iloc[-4] it returns the series corresponding to the date 2021-12-20.

How do I make it so that calling a negative index will return an error instead of just counting back from the end?

CodePudding user response:

I'm pretty sure you can't disallow negative indexing. Can you just check if the index is <0, with

if(i<0):
    raise .... #What error you want

Similar question: Is it possible to disable negative indexing?

  • Related