Skip to content

API: Series/DataFrame from empty dict should have RangeIndex #52426

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

Merged

Conversation

topper-123
Copy link
Contributor

@MarcoGorelli MarcoGorelli added the Constructors Series/DataFrame/Index/pd.array Constructors label Apr 5, 2023
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Nice, thanks for the quick fix! Got a couple of little comments but looks good overall

Comment on lines 168 to 169
expected = Index(keys, dtype="object", tupleize_cols=False)
expected = Index(keys, tupleize_cols=False) if keys else RangeIndex(0)
Copy link
Member

Choose a reason for hiding this comment

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

any chance we could include this in the parametrisation, instead of introducing an if-else in the 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.

Yes, that would be better.

expected.index = expected.index.astype(float)
expected = empty_series.reset_index(drop=True)
if orient in ("split"):
expected.index = expected.index.astype(np.float64)
Copy link
Member

Choose a reason for hiding this comment

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

any reason to change float to np.float64? (not an issue, just trying to understand)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No reason, my fingers must have liked np.float64 better than float in this case :-)

Comment on lines +116 to +123
def test_empty_constructor(self, constructor, empty_index):
# GH 49573 (addition of empty_index parameter)
expected = Series(index=empty_index)
result = constructor(empty_index)

assert result.dtype == object
assert len(result.index) == 0
tm.assert_series_equal(result, expected, check_index_type=check_index_type)
tm.assert_series_equal(result, expected, check_index_type=True)
Copy link
Member

Choose a reason for hiding this comment

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

nice, lovely simplification here!

@MarcoGorelli MarcoGorelli added this to the 2.0.1 milestone Apr 5, 2023
[
({}, [], "index"),
([{("a",): 1}, {("a",): 2}], [("a",)], "columns"),
([OrderedDict([(("a",), 1), (("b",), 2)])], [("a",), ("b",)], "columns"),
Copy link
Member

Choose a reason for hiding this comment

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

what happened to the ordereddict test case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I've added it back in.

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

LGTM. Merge when ready @MarcoGorelli

@topper-123
Copy link
Contributor Author

Small ping.

@mroeschke mroeschke merged commit bff1261 into pandas-dev:main Apr 10, 2023
@mroeschke
Copy link
Member

Thanks @topper-123

meeseeksmachine pushed a commit to meeseeksmachine/pandas that referenced this pull request Apr 10, 2023
@topper-123 topper-123 deleted the Series_from_empty_dict_has_RangeIndex branch April 10, 2023 20:38
mroeschke pushed a commit that referenced this pull request Apr 10, 2023
…dict should have RangeIndex) (#52578)

Backport PR #52426: API: Series/DataFrame from empty dict should have RangeIndex

Co-authored-by: Terji Petersen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Constructors Series/DataFrame/Index/pd.array Constructors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Index on empty frame should be RangeIndex
3 participants