wesm / pandas2

Design documents and code for the pandas 2.0 effort.
https://pandas-dev.github.io/pandas2/
306 stars 41 forks source link

A better vectorized ternary operator #6

Open wesm opened 8 years ago

wesm commented 8 years ago

I was poking around pandas to see if someone had implemented the equivalent of a vectorized if-then-else. For example, similar to np.where, but pandas friendly

df['category'].isin(values).ifelse(df.group_a, df.group_b)

I put this here as it will be easier to implement later (in theory)

shoyer commented 8 years ago

This is exactly what DataFrame.where already does, e.g., df.group_a.where(df['category'].isin(values), df.group_b)

The name and order of arguments true_case.where(condition, false_case) is obviously not very intuitive, though. So I would strongly support adding an DataFrame.ifelse method or maybe even adding a function (gasp!) pd.ifelse or pd.where with the arguments in the expected order.

wesm commented 8 years ago

@shoyer the problem with Series.where is that it presumes you have a Series on the LHS. For example, you might want:

cond.ifelse('foo', cond2.ifelse(df.other_col, 'bar'))

shoyer commented 8 years ago

Indeed, that is a good case for making it a function.