[#16737] Index type for Series with empty data
State: closed

- [x] closes #16737
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

---------------------------------
I picked up all the notes from #16737 where it was suggested to use `Index` over `RangeIndex` for empty data.


Labels:
name: Dtype Conversions
description: Unexpected or buggy dtype conversions
name: Deprecate
description: Functionality to remove in pandas
name: Index
description: Related to the Index class or subclasses

Comments:
Can you please take another look @WillAyd. I think I addressed all your comments
@SaturnFromTitan maybe missed the comment but did we actually decide that this is the right approach? I think it's actually confusing for the index types to differ:

Here is current behaviour on *master*

```python
# series
>>> pd.Series().index
Index([], dtype='object')
>>> pd.Series([]).index
RangeIndex(start=0, stop=0, step=1)
>>> pd.Series([1]).index
RangeIndex(start=0, stop=1, step=1)

# frame
>>> pd.DataFrame().index
Index([], dtype='object')
>>> pd.DataFrame([[]]).index
RangeIndex(start=0, stop=1, step=1)
>>> pd.DataFrame([[1]]).index
RangeIndex(start=0, stop=1, step=1)
```

Why would we only want to change the second Series case shown above?
@WillAyd I went that direction as it was suggested in the comments of #16961:
- [Comment by @TomAugspurger](https://github.com/pandas-dev/pandas/pull/16961#pullrequestreview-58480118)
- [Comment by @jorisvandenbossche ](https://github.com/pandas-dev/pandas/pull/16961#issuecomment-344092275)
I see your point though @WillAyd and agree it could lead to more confusion merging the current changes. Do you think we should...
1. keep the current master behaviour
2. adjust the default index types for `pd.Series()` and `pd.DataFrame()` to use a `RangeIndex` as well

Not sure about all the details, but option 2 seems to be the most consistent.
@SaturnFromTitan also you should add the expansion test as indicated in the OP
Before I continue to work on this, can we please clarify if this should be changed in the first place or not?

And if it should change, then is my current implementation is the way to go or should I rather adjust the index of `pd.Series()`? See [WillAyds comment](https://github.com/pandas-dev/pandas/pull/32053#issuecomment-589380090) and [my reply](https://github.com/pandas-dev/pandas/pull/32053#issuecomment-590067299).

I'd also be fine with closing this issue+PR as a "Won't change". It seems like any solution could yield something that's surprising to some users and expected to others.
forgot the pings: @jreback @TomAugspurger @WillAyd 
IMO, the default index for `pd.Series([])` should match `pd.Index([])`, an empty object-dtype index.
I agree with @TomAugspurger that the default index for `pd.Series([])` should match `pd.Index([])`, an empty object-dtype index.

We already do a deprecation warning for `pd.Series([])` becoming object dtype in the future, I suppose we can add a second warning for the Index also becoming an object index in the future? Or would that be too annoying?

(@SaturnFromTitan I reopened the PR not to mean that you should continue working on this (although it is certainly welcome of course, once there is agreement on the behaviour), but that we should have something open with this discussion. But so if not, we should move the discussion to an issue)
I'd be OK with two warnings from `pd.Series([])` if it meant getting things
consistent.

On Wed, Apr 1, 2020 at 2:37 AM Joris Van den Bossche <
notifications@github.com> wrote:

> I agree with @TomAugspurger <https://github.com/TomAugspurger> that the
> default index for pd.Series([]) should match pd.Index([]), an empty
> object-dtype index.
>
> We already do a deprecation warning for pd.Series([]) becoming object
> dtype in the future, I suppose we can add a second warning for the Index
> also becoming an object index in the future? Or would that be too annoying?
>
> (@SaturnFromTitan <https://github.com/SaturnFromTitan> I reopened the PR
> not to mean that you should continue working on this (although it is
> certainly welcome of course, once there is agreement on the behaviour), but
> that we should have something open with this discussion. But so if not, we
> should move the discussion to an issue)
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/pandas-dev/pandas/pull/32053#issuecomment-607085903>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAKAOIRTEHNF6IVLY25LYLTRKLVKHANCNFSM4KWQLKGA>
> .
>

Getting back at this, thanks for keeping it alive @jorisvandenbossche. I'll change the code to add the warning.

I'll take the same approach as in the dtype change of `Series([])`: First `DeprecationWarning`, later `FutureWarning`, then code change.
I still have to fix a ton of DeprecationWarnings in the test suite. I'll take care of that in the coming days.
Hello @SaturnFromTitan! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:

































There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2020-04-22 08:02:50 UTC
Please have another look @jreback @TomAugspurger @jorisvandenbossche 
@SaturnFromTitan looks like a lot of merge conflicts have piled up - can you resolve?
Closing as I think stale but ping @SaturnFromTitan if you'd like to pick back up and can address merge conflicts