Skip to content
Open
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pandas/tests/indexing/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,15 @@ def test_reindex_behavior_with_interval_index(self, base):
expected_result = Series([np.nan, 0], index=[np.nan, 1.0], dtype=float)
result = ser.reindex(index=[np.nan, 1.0])
tm.assert_series_equal(result, expected_result)

def test_multiindex_with_interval_index(self):
# for GH#25298
intIndex = IntervalIndex.from_arrays([1, 5, 8, 13, 16], [4, 9, 12, 17, 20])
multiIndex = pd.MultiIndex.from_arrays([["a", "a", "b", "b", "c"], intIndex])
data = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
df = DataFrame(data, index=multiIndex)
result1 = df.loc[("b", 16)]
Copy link
Contributor

Choose a reason for hiding this comment

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

@harinik, one small comment: could you please rename result1 to result?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

expected = Series([7, 8])
tm.assert_series_equal(result1, expected, check_names=False)
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a name= to the Series so we do not need to call check_names=False?

Copy link
Contributor

Choose a reason for hiding this comment

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

@harinik, in case you need help with this comment:
When you define

expected = Series([7, 8], name=('b', Interval(13, 17, closed='right')))

your test should work without passing check_names=False.
(import Interval as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did that but it did not work for some reason. When I tried changing it to:

expected = Series([7, 8], name="('b', Interval(13, 17, closed='right'))")

and removed check_names=False it fails with:

E       Attribute "name" are different
E       [left]:  ('b', Interval(13, 17, closed='right'))
E       [right]: ('b', Interval(13, 17, closed='right'))

I am not quite sure what I am doing wrong.

Copy link
Contributor

Choose a reason for hiding this comment

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

the name has type tuple, not a string (just remove the quotes)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I hadn't realized that, thank you for catching. Fixed now.

result2 = df.loc["b"].loc[16]
tm.assert_series_equal(result2, expected, check_names=False)
Loading