Why reindex instead of using .loc?

Viewed 345

In previous versions of pandas, using .loc[list-of-values] would work, so as long as the list of values contained at least one existing key.

However, pandas has swapped out this functionality in favor for the reindex function. I personally liked the .loc feature since it worked well with index slicing and other .loc behaviors. Was wondering why they decided to do this?

2 Answers

This change appears to be discussed in some detail on the github.

There are (at least) three ways to behave with missing labels:

  1. you raise an error if requested at least one missing label
  2. you raise an error if requested only missing labels
    • a) ... while if at least one label is present, missing labels become NaN (current)
    • b) ... while if at least one label is present, missing labels are silently dropped
  3. you never raise an error for missing labels
    • a) ... and they become NaN
    • b). ... and they are silently dropped

It seems the general agreement was that the hybrid behaviour of option 2 was largely undocumented and somewhat counter-intuitive. Here is an example of a perhaps confusing behaviour, where reordering operations changes whether an error is raised:

For instance, with options 1. and 3.

pd.concat([chunk.loc[something] for chunk in chunks])

and

pd.concat(chunks).loc[something]

both return the same result (or raise). Instead with 2. it actually depends on how missing labels are distributed across chunks.

Ultimately, this is question of the design philosophy, and there are pros and cons to each approach.

From what my understanding

If you have Series below, and the index is 0-3 and we would like get some index beyond the index , we should do reindex

s=pd.Series([1,2,3,4])
s.reindex([100])
100   NaN
dtype: float64

If doing with loc , it will return error

s.loc[100]
Related