Skip to content

BUG: If Series is called with no arguments, let it have a RangeIndex (#16737) #16961

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
wants to merge 1 commit into from

Conversation

alanbato
Copy link
Contributor

Before:

In [4]: Series().index
Out[4]: Index([], dtype='object')

Now:

In [3]: Series().index
Out[3]: RangeIndex(start=0, stop=0, step=1)

alanbato added a commit to alanbato/pandas that referenced this pull request Jul 15, 2017
@gfyoung gfyoung added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Jul 15, 2017
@@ -201,3 +201,4 @@ Categorical
Other
^^^^^
- Bug in :func:`eval` where the ``inplace`` parameter was being incorrectly handled (:issue:`16732`)
- The ``Series`` constructor with no arguments would have an index like ``Index([], dtype='object')`` instead of ``RangeIndex(start=0, stop=0, step=1)``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make a less verbose comment, like:

Series constructor will default to construct a RangeIndex, rather than an Index when constructed as empty.

@@ -158,6 +158,8 @@ def __init__(self, data=None, index=None, dtype=None, name=None,

if data is None:
data = {}
if index is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add the test code in the following test? Or somewhere else?
Is setting check_index_type=True enough?

def test_constructor_empty(self):
        empty = Series()
        empty2 = Series([])

        # the are Index() and RangeIndex() which don't compare type equal
        # but are just .equals
        assert_series_equal(empty, empty2, check_index_type=False)

        empty = Series(index=lrange(10))
        empty2 = Series(np.nan, index=lrange(10))
        assert_series_equal(empty, empty2)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you prob just need to remove the check_index_type=False and then your change should work

@jreback
Copy link
Contributor

jreback commented Aug 18, 2017

can you rebase and respond to comments

Copy link
Contributor

@TomAugspurger TomAugspurger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to see this go the other way, so that pd.Series([]).index and pd.Series().index are both an empty Index with object dtype. Then we'll have a consistent rule of "empty = object dtype"

@alanbato
Copy link
Contributor Author

alanbato commented Oct 1, 2017

I also think we should aim for consistency, I'll change the code accordingly so pd.Series([]).index and pd.Series().index evaluate to an empty Index.

@jreback
Copy link
Contributor

jreback commented Nov 10, 2017

revisiting this, can you rebase and update and we can review

@alanbato
Copy link
Contributor Author

Hmmm, I think I messed up the rebase. Uhm. What should I do?

@TomAugspurger
Copy link
Contributor

@alanbato we should be able to recover it from your local git reflog, and then do the rebase again.

Ping one of us on gitter at https://gitter.im/pydata/pandas and we'll walk you through it.

@jorisvandenbossche
Copy link
Member

Your actual commit is still in this branch, so a git fetch upstream and git rebase -i upstream/master and then only keeping the commit you want should also work. But as @TomAugspurger said, feel free to ping on gitter as well for help.

@alanbato
Copy link
Contributor Author

I think I did it! Thanks for your help @TomAugspurger @jorisvandenbossche.
But the question still stands, what should be the expected behaviour?

Also pinging @jreback in case he has something to say

Cheers! 🐍

@jorisvandenbossche
Copy link
Member

I also think we should aim for consistency, I'll change the code accordingly so pd.Series([]).index and pd.Series().index evaluate to an empty Index.

I think we agree on this one (so what @TomAugspurger proposed)

@jreback
Copy link
Contributor

jreback commented Dec 28, 2017

closing as stale, if you want to work on this, pls ping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected results creating an empty Series
5 participants