Skip to content

API: Should Index.where/insert raise or cast on invalid other? #37594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jbrockmendel opened this issue Nov 2, 2020 · 4 comments
Closed

API: Should Index.where/insert raise or cast on invalid other? #37594

jbrockmendel opened this issue Nov 2, 2020 · 4 comments
Labels
API - Consistency Internal Consistency of API/Behavior Needs Discussion Requires discussion from core team before further action

Comments

@jbrockmendel
Copy link
Member

Index.where

ATM index.where(mask, something_incompatible) raises for EA-backed indexes and MultiIndex and casts (sometimes buggily xref #37591) for everything else.

These should all do the same thing. I lean towards raising rather than casting. Doing that breaks a handful of tests, but AFAICT they are all directly testing Index.where, so not used by other parts of the code.


Index.insert

CategoricalIndex and IntervalIndex raise on invalid, PeriodIndex casts to object, DatetimeIndex and TimedeltaIndex cast to object only for strings and otherwise raise. The base class casts but uses _coerce_scalar_to_index instead of any of our more standard patterns.

If changed to always raise, 25 tests fail. Most of these are directly testing Index.insert so could be changed if the API changed. The main troubling one is test_pivot_periods_with_margins, which I haven't tracked down all the way.


Index.putmask I'm still getting a handle on

@jbrockmendel jbrockmendel added Bug Needs Triage Issue that has not been reviewed by a pandas team member API - Consistency Internal Consistency of API/Behavior Needs Discussion Requires discussion from core team before further action and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 2, 2020
@jbrockmendel
Copy link
Member Author

On "insert" loc.setitem is liable to raise if we dont cast

@jorisvandenbossche
Copy link
Member

Is there a reason you closed this?

@jbrockmendel
Copy link
Member Author

I determined pretty definitively that insert needs to cast, and im not actually sure where/putmask are really needed. (IIRC there is one non-test place where Index.where is used)

@jorisvandenbossche
Copy link
Member

Index.where is a public method, so should ideally have a clear / consistent API, so I think the question your raised is still relevant (also, the same inconsistency is true for Series.where)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

No branches or pull requests

2 participants