-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: loc casting to float for scalar with MultiIndex df #41374
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
Changes from 1 commit
5dc2757
858299e
6a4db6f
01dde93
90f5e1c
4484d91
832c236
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -888,26 +888,22 @@ def _getitem_nested_tuple(self, tup: tuple): | |
# handle the multi-axis by taking sections and reducing | ||
# this is iterative | ||
obj = self.obj | ||
axis = 0 | ||
for key in tup: | ||
# GH#41369 Loop in reverse order to avoid dtype conversion when converting df | ||
# row to a series | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC the reason this works is bc this ensures we index along columns before rows, so that in multi-block cases we select only necessary blocks, avoiding the fast_xs/interleave/whatever call that is doing the casting ATM? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes exactly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you flesh out the comment to that effect? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pending that, LGTM There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
axis = len(tup) - 1 | ||
for key in tup[::-1]: | ||
|
||
if com.is_null_slice(key): | ||
axis += 1 | ||
axis -= 1 | ||
continue | ||
|
||
current_ndim = obj.ndim | ||
obj = getattr(obj, self.name)._getitem_axis(key, axis=axis) | ||
axis += 1 | ||
axis -= 1 | ||
|
||
# if we have a scalar, we are done | ||
if is_scalar(obj) or not hasattr(obj, "ndim"): | ||
break | ||
|
||
# has the dim of the obj changed? | ||
# GH 7199 | ||
if obj.ndim < current_ndim: | ||
axis -= 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i take it this is unreachable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, in theory this is reachable, but does not make sense anymore. We are counting from the maximum number of dimensions backwards, so even if we reduce the dimension we have already reduced our axis to the new maximum number. DataFrame example: |
||
|
||
return obj | ||
|
||
def _convert_to_indexer(self, key, axis: int, is_setter: bool = False): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -776,3 +776,12 @@ def test_loc_getitem_drops_levels_for_one_row_dataframe(): | |
result = ser.loc["x", :, "z"] | ||
expected = Series([0], index=Index(["y"], name="b")) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_loc_get_scalar_casting_to_float(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add the .iloc example as well (to assert that its also an int as on master). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
# GH#41369 | ||
df = DataFrame( | ||
{"a": 1.0, "b": 2}, index=MultiIndex.from_arrays([[3], [4]], names=["c", "d"]) | ||
) | ||
result = df.loc[(3, 4), "b"] | ||
assert result == 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test passes on master. 2.0 == 2 is True. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an isinstance check |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"is from has" typo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes thanks, opened #41808