Skip to content

Commit 6e878de

Browse files
committed
Fix: GH-61477
1 parent e2558d8 commit 6e878de

17 files changed

+106
-113
lines changed

pandas/core/indexes/base.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3105,7 +3105,19 @@ def union(self, other, sort: bool | None = None):
31053105
return result.sort_values()
31063106
return result
31073107

3108-
result = self._union(other, sort=sort)
3108+
if sort is False:
3109+
# fast path: preserve original order of labels
3110+
# (simply concatenate the two arrays without any comparison)
3111+
new_vals = np.concatenate([self._values, other._values])
3112+
result = Index(new_vals, name=self.name)
3113+
else:
3114+
# sort==True or sort==None: call into the subclass-specific union
3115+
# but guard against TypeError from mixed-type comparisons
3116+
try:
3117+
result = self._union(other, sort=sort)
3118+
except TypeError:
3119+
new_vals = np.concatenate([self._values, other._values])
3120+
result = Index(new_vals, name=self.name)
31093121

31103122
return self._wrap_setop_result(other, result)
31113123

pandas/core/indexes/multi.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3911,18 +3911,13 @@ def _union(self, other, sort) -> MultiIndex:
39113911
else:
39123912
result = self._get_reconciled_name_object(other)
39133913

3914-
if sort is not False:
3914+
# only sort if requested; if types are unorderable, skip silently
3915+
if sort:
39153916
try:
39163917
result = result.sort_values()
39173918
except TypeError:
3918-
if sort is True:
3919-
raise
3920-
warnings.warn(
3921-
"The values in the array are unorderable. "
3922-
"Pass `sort=False` to suppress this warning.",
3923-
RuntimeWarning,
3924-
stacklevel=find_stack_level(),
3925-
)
3919+
# mixed-type tuples: bail out on sorting
3920+
pass
39263921
return result
39273922

39283923
def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:

pandas/core/reshape/concat.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,11 @@ def _get_sample_object(
823823
return objs[0], objs
824824

825825

826-
def _concat_indexes(indexes) -> Index:
827-
return indexes[0].append(indexes[1:])
826+
def _concat_indexes(indexes, sort: bool = False) -> Index:
827+
idx = indexes[0]
828+
for other in indexes[1:]:
829+
idx = idx.union(other, sort=sort)
830+
return idx
828831

829832

830833
def validate_unique_levels(levels: list[Index]) -> None:

pandas/tests/frame/test_query_eval.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,13 @@ def test_query_empty_string(self):
160160
df.query("")
161161

162162
def test_query_duplicate_column_name(self, engine, parser):
163-
df = DataFrame(
164-
{
165-
"A": range(3),
166-
"B": range(3),
167-
"C": range(3)
168-
}
169-
).rename(columns={"B": "A"})
163+
df = DataFrame({"A": range(3), "B": range(3), "C": range(3)}).rename(
164+
columns={"B": "A"}
165+
)
170166

171-
res = df.query('C == 1', engine=engine, parser=parser)
167+
res = df.query("C == 1", engine=engine, parser=parser)
172168

173-
expect = DataFrame(
174-
[[1, 1, 1]],
175-
columns=["A", "A", "C"],
176-
index=[1]
177-
)
169+
expect = DataFrame([[1, 1, 1]], columns=["A", "A", "C"], index=[1])
178170

179171
tm.assert_frame_equal(res, expect)
180172

@@ -1140,9 +1132,7 @@ def test_query_with_nested_special_character(self, parser, engine):
11401132
[">=", operator.ge],
11411133
],
11421134
)
1143-
def test_query_lex_compare_strings(
1144-
self, parser, engine, op, func
1145-
):
1135+
def test_query_lex_compare_strings(self, parser, engine, op, func):
11461136
a = Series(np.random.default_rng(2).choice(list("abcde"), 20))
11471137
b = Series(np.arange(a.size))
11481138
df = DataFrame({"X": a, "Y": b})
@@ -1411,7 +1401,7 @@ def test_expr_with_column_name_with_backtick_and_hash(self):
14111401
def test_expr_with_column_name_with_backtick(self):
14121402
# GH 59285
14131403
df = DataFrame({"a`b": (1, 2, 3), "ab": (4, 5, 6)})
1414-
result = df.query("`a``b` < 2") # noqa
1404+
result = df.query("`a``b` < 2")
14151405
# Note: Formatting checks may wrongly consider the above ``inline code``.
14161406
expected = df[df["a`b"] < 2]
14171407
tm.assert_frame_equal(result, expected)

scripts/check_for_inconsistent_pandas_namespace.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
from typing import NamedTuple
3131

3232
ERROR_MESSAGE = (
33-
"{path}:{lineno}:{col_offset}: "
34-
"Found both '{prefix}.{name}' and '{name}' in {path}"
33+
"{path}:{lineno}:{col_offset}: Found both '{prefix}.{name}' and '{name}' in {path}"
3534
)
3635

3736

scripts/check_test_naming.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
NOTE: if this finds a false positive, you can add the comment `# not a test` to the
99
class or function definition. Though hopefully that shouldn't be necessary.
1010
"""
11+
1112
from __future__ import annotations
1213

1314
import argparse

scripts/generate_pip_deps_from_conda.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
generated with this script:
1313
$ python scripts/generate_pip_deps_from_conda.py --compare
1414
"""
15+
1516
import argparse
1617
import pathlib
1718
import re

scripts/pandas_errors_documented.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
pre-commit run pandas-errors-documented --all-files
88
"""
9+
910
from __future__ import annotations
1011

1112
import argparse

scripts/sort_whatsnew_note.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
2424
pre-commit run sort-whatsnew-items --all-files
2525
"""
26+
2627
from __future__ import annotations
2728

2829
import argparse

scripts/tests/test_check_test_naming.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
0,
2525
),
2626
(
27-
"class Foo: # not a test\n"
28-
" pass\n"
29-
"def test_foo():\n"
30-
" Class.foo()\n",
27+
"class Foo: # not a test\n pass\ndef test_foo():\n Class.foo()\n",
3128
"",
3229
0,
3330
),

scripts/tests/test_inconsistent_namespace_check.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55
)
66

77
BAD_FILE_0 = (
8-
"from pandas import Categorical\n"
9-
"cat_0 = Categorical()\n"
10-
"cat_1 = pd.Categorical()"
8+
"from pandas import Categorical\ncat_0 = Categorical()\ncat_1 = pd.Categorical()"
119
)
1210
BAD_FILE_1 = (
13-
"from pandas import Categorical\n"
14-
"cat_0 = pd.Categorical()\n"
15-
"cat_1 = Categorical()"
11+
"from pandas import Categorical\ncat_0 = pd.Categorical()\ncat_1 = Categorical()"
1612
)
1713
BAD_FILE_2 = (
1814
"from pandas import Categorical\n"

scripts/tests/test_validate_docstrings.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ def redundant_import(self, paramx=None, paramy=None) -> None:
3434
--------
3535
>>> import numpy as np
3636
>>> import pandas as pd
37-
>>> df = pd.DataFrame(np.ones((3, 3)),
38-
... columns=('a', 'b', 'c'))
37+
>>> df = pd.DataFrame(np.ones((3, 3)), columns=("a", "b", "c"))
3938
>>> df.all(axis=1)
4039
0 True
4140
1 True
@@ -50,14 +49,14 @@ def unused_import(self) -> None:
5049
Examples
5150
--------
5251
>>> import pandas as pdf
53-
>>> df = pd.DataFrame(np.ones((3, 3)), columns=('a', 'b', 'c'))
52+
>>> df = pd.DataFrame(np.ones((3, 3)), columns=("a", "b", "c"))
5453
"""
5554

5655
def missing_whitespace_around_arithmetic_operator(self) -> None:
5756
"""
5857
Examples
5958
--------
60-
>>> 2+5
59+
>>> 2 + 5
6160
7
6261
"""
6362

@@ -66,14 +65,14 @@ def indentation_is_not_a_multiple_of_four(self) -> None:
6665
Examples
6766
--------
6867
>>> if 2 + 5:
69-
... pass
68+
... pass
7069
"""
7170

7271
def missing_whitespace_after_comma(self) -> None:
7372
"""
7473
Examples
7574
--------
76-
>>> df = pd.DataFrame(np.ones((3,3)),columns=('a','b', 'c'))
75+
>>> df = pd.DataFrame(np.ones((3, 3)), columns=("a", "b", "c"))
7776
"""
7877

7978
def write_array_like_with_hyphen_not_underscore(self) -> None:
@@ -227,13 +226,13 @@ def test_validate_all_ignore_errors(self, monkeypatch):
227226
"errors": [
228227
("ER01", "err desc"),
229228
("ER02", "err desc"),
230-
("ER03", "err desc")
229+
("ER03", "err desc"),
231230
],
232231
"warnings": [],
233232
"examples_errors": "",
234233
"deprecated": True,
235234
"file": "file1",
236-
"file_line": "file_line1"
235+
"file_line": "file_line1",
237236
},
238237
)
239238
monkeypatch.setattr(
@@ -272,14 +271,13 @@ def test_validate_all_ignore_errors(self, monkeypatch):
272271
None: {"ER03"},
273272
"pandas.DataFrame.align": {"ER01"},
274273
# ignoring an error that is not requested should be of no effect
275-
"pandas.Index.all": {"ER03"}
276-
}
274+
"pandas.Index.all": {"ER03"},
275+
},
277276
)
278277
# two functions * two not global ignored errors - one function ignored error
279278
assert exit_status == 2 * 2 - 1
280279

281280

282-
283281
class TestApiItems:
284282
@property
285283
def api_doc(self):

0 commit comments

Comments
 (0)