Skip to content

Commit 225675d

Browse files
committed
Addressed review comments
1 parent 7076841 commit 225675d

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

pandas/core/algorithms.py

+15-17
Original file line numberDiff line numberDiff line change
@@ -2061,32 +2061,32 @@ def sort_tuples(values):
20612061
def cmp_func(index_x, index_y):
20622062
x = values[index_x]
20632063
y = values[index_y]
2064+
# shortcut loop in case both tuples are the same
20642065
if x == y:
20652066
return 0
2066-
len_x = len(x)
2067-
len_y = len(y)
2068-
for i in range(max(len_x, len_y)):
2067+
# lexicographic sorting
2068+
for i in range(max(len(x), len(y))):
20692069
# check if the tuples have different lengths (shorter tuples
20702070
# first)
2071-
if i >= len_x:
2071+
if i >= len(x):
20722072
return -1
2073-
if i >= len_y:
2073+
if i >= len(y):
20742074
return +1
2075-
x_i_na = isna(x[i])
2076-
y_i_na = isna(y[i])
2075+
x_is_na = isna(x[i])
2076+
y_is_na = isna(y[i])
20772077
# values are the same -> resolve tie with next element
2078-
if (x_i_na and y_i_na) or (x[i] == y[i]):
2078+
if (x_is_na and y_is_na) or (x[i] == y[i]):
20792079
continue
2080-
# check for nan values (sort nan to the end which is consistent
2081-
# with numpy
2082-
if x_i_na and not y_i_na:
2080+
# check for nan values (sort nan to the end)
2081+
if x_is_na and not y_is_na:
20832082
return +1
2084-
if not x_i_na and y_i_na:
2083+
if not x_is_na and y_is_na:
20852084
return -1
20862085
# normal greater/less than comparison
20872086
if x[i] < y[i]:
20882087
return -1
20892088
return +1
2089+
# both values are the same (should already have been caught)
20902090
return 0
20912091

20922092
ixs = np.arange(len(values))
@@ -2095,12 +2095,10 @@ def cmp_func(index_x, index_y):
20952095

20962096
sorter = None
20972097

2098-
ext_arr = is_extension_array_dtype(values)
2099-
if not ext_arr and lib.infer_dtype(values, skipna=False) == "mixed-integer":
2100-
# unorderable in py3 if mixed str/int
2098+
is_ea = is_extension_array_dtype(values)
2099+
if not is_ea and lib.infer_dtype(values, skipna=False) == "mixed-integer":
21012100
ordered = sort_mixed(values)
2102-
elif not ext_arr and values.size and isinstance(values[0], tuple):
2103-
# 1-D arrays with tuples of potentially mixed type (solves GH36562)
2101+
elif not is_ea and values.size and isinstance(values[0], tuple):
21042102
ordered = sort_tuples(values)
21052103
else:
21062104
try:

pandas/tests/indexing/multiindex/test_multiindex.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_multiindex_get_loc_list_raises(self):
9393
idx.get_loc([])
9494

9595

96-
def test_combine_first_with_nan_index():
96+
def test_combine_first_with_nan_multiindex():
9797
mi1 = pd.MultiIndex.from_arrays(
9898
[["b", "b", "c", "a", "b", np.nan], [1, 2, 3, 4, 5, 6]], names=["a", "b"]
9999
)
@@ -102,17 +102,19 @@ def test_combine_first_with_nan_index():
102102
[["a", "b", "c", "a", "b", "d"], [1, 1, 1, 1, 1, 1]], names=["a", "b"]
103103
)
104104
s = pd.Series([1, 2, 3, 4, 5, 6], index=mi2)
105-
df_combined = df.combine_first(pd.DataFrame({"col": s}))
105+
res = df.combine_first(pd.DataFrame({"d": s}))
106106
mi_expected = pd.MultiIndex.from_arrays(
107107
[
108108
["a", "a", "a", "b", "b", "b", "b", "c", "c", "d", np.nan],
109109
[1, 1, 4, 1, 1, 2, 5, 1, 3, 1, 6],
110110
],
111111
names=["a", "b"],
112112
)
113-
assert (df_combined.index == mi_expected).all()
114-
exp_col = np.asarray(
115-
[1.0, 4.0, np.nan, 2.0, 5.0, np.nan, np.nan, 3.0, np.nan, 6.0, np.nan]
113+
expected = pd.DataFrame(
114+
{
115+
"c": [np.nan, np.nan, 1, 1, 1, 1, 1, np.nan, 1, np.nan, 1],
116+
"d": [1.0, 4.0, np.nan, 2.0, 5.0, np.nan, np.nan, 3.0, np.nan, 6.0, np.nan],
117+
},
118+
index=mi_expected,
116119
)
117-
act_col = df_combined["col"].values
118-
assert np.allclose(act_col, exp_col, rtol=0, atol=0, equal_nan=True)
120+
tm.assert_frame_equal(res, expected)

0 commit comments

Comments
 (0)