API: Index vs Series constructor alignment · Issue #49372 · pandas-dev/pandas (original) (raw)
Navigation Menu
- Explore
- Pricing
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Appearance settings
Description
There has been long-running interest in aligning the behaviors of various constructors: e.g. #29973, #27460, #17246. But I don't see an issue that is specifically "Let's make Index behave like Series". So here goes.
Series goes through sanitize_array
which calls a bunch of functions in core.dtypes.cast. Index uses casting/inference functions in indexes.base, as well as Index._ensure_array
.
The remaining behavior differences AFAICT are:
- Series(non_round_floats, dtype=inty) silently ignores dtype while Index raises (closed by API: Series(floaty, dtype=inty) #49609)
- Series silently returns object instead of raising with Series(np.array(["aaa"], dtype=object), dtype="datetime64[ns]") (would be closed by API: stop silently ignoring parsing failures with dtype=dt64 #49358)
- API: pd.Series([py_date, py_datetime]) vs pd.Index([py_date, py_datetime]) #49341 different dtype inference with pydate objects
- API: Series([pd.NaT, None]) vs Index([pd.NaT, None]) #49340 different dtype inference with NaT-and-other-NAs
- API: Series(mixed_datetime_and_ints, dtype="M8[ns]") vs DatetimeIndex(same) mismatch #49037 different inference with mixed datetime-and-ints (would be closed by API: allow mixed-datetimes-and-ints in to_datetime, DatetimeIndex #49348)
- RangeIndex and MultiIndex cases that I'm ignoring
- Probably subtle differences in overflow-handling in FooIndex._ensure_array
- Deprecated (TODO: GH ref) Index inference of numeric dtype with ndarray[object-of-all-numbers]
(recently enforced deprecations including #49319, #49242, #48379 cut down on this a ton)
The first entry here (handling of non-round floats with inty dtype) would be the most invasive to align. The current Series behavior was in place before I got involved, so I'm not aware of the reasoning behind it. I'd be on board with deprecating it and changing it in 3.0.
Thoughts? cc @jorisvandenbossche @mroeschke @jreback @TomAugspurger @phofl