Skip to content
Prev Previous commit
Next Next commit
Fix bugs
  • Loading branch information
rhshadrach committed Nov 1, 2025
commit f713c4fe28b6d17f6f32f6a1c3189a682f2117fd
5 changes: 3 additions & 2 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def wrap_results_list_like(
obj = self.obj

try:
return concat(results, keys=keys, axis=1, sort=False) # maybebug
return concat(results, keys=keys, axis=1, sort=False) # nobug
except TypeError as err:
# we are concatting non-NDFrame objects,
# e.g. a list of scalars
Expand Down Expand Up @@ -635,10 +635,11 @@ def wrap_results_dict_like(
keys_to_use = ktu

axis: AxisInt = 0 if isinstance(obj, ABCSeries) else 1
result = concat( # maybebug
result = concat( # nobug
results,
axis=axis,
keys=keys_to_use,
sort=False,
)
elif any(is_ndframe):
# There is a mix of NDFrames and scalars
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6130,7 +6130,7 @@ def shift(
.shift(periods=period, freq=freq, axis=axis, fill_value=fill_value)
.add_suffix(f"{suffix}_{period}" if suffix else f"_{period}")
)
return concat(shifted_dataframes, axis=1) # bug
return concat(shifted_dataframes, axis=1, sort=False) # nobug
elif suffix:
raise ValueError("Cannot specify `suffix` if `periods` is an int.")
periods = cast(int, periods)
Expand Down Expand Up @@ -11168,7 +11168,7 @@ def _append_internal(

from pandas.core.reshape.concat import concat

result = concat( # possible bug
result = concat( # nobug
[self, row_df],
ignore_index=ignore_index,
)
Expand Down Expand Up @@ -11401,7 +11401,7 @@ def join(
)
return res.reindex(self.index)
else:
return concat( # bug
return concat( # nobug
frames, axis=1, join=how, verify_integrity=True, sort=sort
)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9507,7 +9507,7 @@ def compare(

# error: List item 0 has incompatible type "NDFrame"; expected
# "Union[Series, DataFrame]"
diff = concat( # bug
diff = concat( # nobug - self and other must have same index/coluns
[self, other], # type: ignore[list-item]
axis=axis,
keys=result_names,
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ def _concat_objects(
group_levels = self._grouper.levels
group_names = self._grouper.names

result = concat( # maybebug
result = concat(
values,
axis=0,
keys=group_keys,
Expand All @@ -1152,10 +1152,10 @@ def _concat_objects(
sort=False,
)
else:
result = concat(values, axis=0) # maybebug
result = concat(values, axis=0)

elif not not_indexed_same:
result = concat(values, axis=0) # maybebug
result = concat(values, axis=0)

ax = self._selected_obj.index
if self.dropna:
Expand All @@ -1178,7 +1178,7 @@ def _concat_objects(
result = result.reindex(ax, axis=0)

else:
result = concat(values, axis=0) # maybebug
result = concat(values, axis=0)

if self.obj.ndim == 1:
name = self.obj.name
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/interchange/from_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ def _from_dataframe(df: DataFrameXchg, allow_copy: bool = True) -> pd.DataFrame:
elif len(pandas_dfs) == 1:
pandas_df = pandas_dfs[0]
else:
pandas_df = pd.concat(pandas_dfs, axis=0, ignore_index=True, copy=False) # bug
pandas_df = pd.concat(
pandas_dfs, axis=0, ignore_index=True, copy=False
) # nobug

index_obj = df.metadata.get("pandas.index", None)
if index_obj is not None:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ def melt_stub(df, stub: str, i, j, value_vars, sep: str):
value_vars_flattened.extend(value_var)
_melted.append(melt_stub(df, stub, i, j, value_var, sep))

melted = concat(_melted, axis=1) # maybebug
melted = concat(_melted, axis=1) # nobug
id_vars = df.columns.difference(value_vars_flattened)
new = df[id_vars]

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2994,7 +2994,7 @@ def searchsorted( # type: ignore[override]
def _append_internal(self, to_append: Series, ignore_index: bool = False) -> Series:
from pandas.core.reshape.concat import concat

return concat([self, to_append], ignore_index=ignore_index) # maybebug
return concat([self, to_append], ignore_index=ignore_index) # nobug

def compare(
self,
Expand Down Expand Up @@ -3271,7 +3271,7 @@ def combine_first(self, other) -> Series:
if this.dtype.kind == "M" and other.dtype.kind != "M":
# TODO: try to match resos?
other = to_datetime(other)
combined = concat([this, other]) # bug
combined = concat([this, other]) # nobug
combined = combined.reindex(new_index)
return combined.__finalize__(self, method="combine_first")

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ def cat(
# align if required
if any(not data.index.equals(x.index) for x in others):
# Need to add keys for uniqueness in case of duplicate columns
others = concat( # bug
others = concat( # nobug
others,
axis=1,
join=(join if join == "inner" else "outer"),
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,34 @@ def test_listlike_lambda(ops, by_row, expected):
tm.assert_equal(result, expected)


def test_listlike_datetime_index_unsorted():
values = [datetime(2024, 1, 1), datetime(2024, 1, 2)]
df = DataFrame({"a": [1, 2]}, index=[values[1], values[0]])
result = df.apply([lambda x: x], by_row=False)
expected = DataFrame(
[[1], [2]],
index=[values[1], values[0]],
columns=MultiIndex([["a"], ["<lambda>"]], codes=[[0], [0]]),
)
tm.assert_frame_equal(result, expected)


def test_dictlike_datetime_index_unsorted():
values = [datetime(2024, 1, 1), datetime(2024, 1, 2), datetime(2024, 1, 3)]
df = DataFrame({"a": [1, 2], "b": [3, 4]}, index=[values[1], values[0]])
result = df.apply(
{"a": lambda x: x, "b": lambda x: x.shift(freq="D")}, by_row=False
)
expected = DataFrame(
{
"a": [1.0, 2.0, np.nan],
"b": [4.0, np.nan, 3.0],
},
index=[values[1], values[0], values[2]],
)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"ops",
[
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/frame/methods/test_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,3 +794,16 @@ def test_shift_invalid_fill_value_deprecation(self):
df["a"].shift(1, fill_value=NaT)
with tm.assert_produces_warning(Pandas4Warning, match=msg):
df["b"].shift(1, fill_value=NaT)

def test_shift_dt_index_multiple_periods_unsorted(self):
values = date_range("1/1/2000", periods=4, freq="D")
df = DataFrame({"a": [1, 2]}, index=[values[1], values[0]])
result = df.shift(periods=[1, 2], freq="D")
expected = DataFrame(
{
"a_1": [1.0, 2.0, np.nan],
"a_2": [2.0, np.nan, 1.0],
},
index=[values[2], values[1], values[3]],
)
tm.assert_frame_equal(result, expected)
9 changes: 9 additions & 0 deletions pandas/tests/reshape/merge/test_join.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
import re

import numpy as np
Expand Down Expand Up @@ -671,6 +672,14 @@ def _check_diff_index(df_list, result, exp_index):
with pytest.raises(ValueError, match=msg):
df_list[0].join(df_list[1:], on="a")

def test_join_many_datetime_unsorted(self):
index = Index([datetime(2024, 1, 2), datetime(2024, 1, 1)])
df = DataFrame({"a": [1, 2]}, index=index)
df2 = DataFrame({"b": [1, 2]}, index=index)
result = df.join([df2], how="outer")
expected = DataFrame({"a": [1, 2], "b": [1, 2]}, index=index)
tm.assert_frame_equal(result, expected)

def test_join_many_mixed(self):
df = DataFrame(
np.random.default_rng(2).standard_normal((8, 4)),
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/strings/test_cat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
import re

import numpy as np
Expand Down Expand Up @@ -343,6 +344,18 @@ def test_str_cat_align_mixed_inputs(join_type):
s.str.cat([t, z], join=join_type)


def test_str_cat_datetime_index_unsorted(join_type):
values = [datetime(2024, 1, 1), datetime(2024, 1, 2)]
s = Series(["a", "b"], index=[values[0], values[1]])
others = Series(["c", "d"], index=[values[1], values[0]])
result = s.str.cat(others, join=join_type)
if join_type == "right":
expected = Series(["bc", "ad"], index=[values[1], values[0]])
else:
expected = Series(["ad", "bc"], index=[values[0], values[1]])
tm.assert_series_equal(result, expected)


def test_str_cat_all_na(index_or_series, index_or_series2):
# GH 24044
box = index_or_series
Expand Down