Skip to content

Commit f713c4f

Browse files
committed
Fix bugs
1 parent d49552d commit f713c4f

File tree

12 files changed

+81
-15
lines changed

12 files changed

+81
-15
lines changed

pandas/core/apply.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def wrap_results_list_like(
485485
obj = self.obj
486486

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

637637
axis: AxisInt = 0 if isinstance(obj, ABCSeries) else 1
638-
result = concat( # maybebug
638+
result = concat( # nobug
639639
results,
640640
axis=axis,
641641
keys=keys_to_use,
642+
sort=False,
642643
)
643644
elif any(is_ndframe):
644645
# There is a mix of NDFrames and scalars

pandas/core/frame.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6130,7 +6130,7 @@ def shift(
61306130
.shift(periods=period, freq=freq, axis=axis, fill_value=fill_value)
61316131
.add_suffix(f"{suffix}_{period}" if suffix else f"_{period}")
61326132
)
6133-
return concat(shifted_dataframes, axis=1) # bug
6133+
return concat(shifted_dataframes, axis=1, sort=False) # nobug
61346134
elif suffix:
61356135
raise ValueError("Cannot specify `suffix` if `periods` is an int.")
61366136
periods = cast(int, periods)
@@ -11168,7 +11168,7 @@ def _append_internal(
1116811168

1116911169
from pandas.core.reshape.concat import concat
1117011170

11171-
result = concat( # possible bug
11171+
result = concat( # nobug
1117211172
[self, row_df],
1117311173
ignore_index=ignore_index,
1117411174
)
@@ -11401,7 +11401,7 @@ def join(
1140111401
)
1140211402
return res.reindex(self.index)
1140311403
else:
11404-
return concat( # bug
11404+
return concat( # nobug
1140511405
frames, axis=1, join=how, verify_integrity=True, sort=sort
1140611406
)
1140711407

pandas/core/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9507,7 +9507,7 @@ def compare(
95079507

95089508
# error: List item 0 has incompatible type "NDFrame"; expected
95099509
# "Union[Series, DataFrame]"
9510-
diff = concat( # bug
9510+
diff = concat( # nobug - self and other must have same index/coluns
95119511
[self, other], # type: ignore[list-item]
95129512
axis=axis,
95139513
keys=result_names,

pandas/core/groupby/groupby.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ def _concat_objects(
11431143
group_levels = self._grouper.levels
11441144
group_names = self._grouper.names
11451145

1146-
result = concat( # maybebug
1146+
result = concat(
11471147
values,
11481148
axis=0,
11491149
keys=group_keys,
@@ -1152,10 +1152,10 @@ def _concat_objects(
11521152
sort=False,
11531153
)
11541154
else:
1155-
result = concat(values, axis=0) # maybebug
1155+
result = concat(values, axis=0)
11561156

11571157
elif not not_indexed_same:
1158-
result = concat(values, axis=0) # maybebug
1158+
result = concat(values, axis=0)
11591159

11601160
ax = self._selected_obj.index
11611161
if self.dropna:
@@ -1178,7 +1178,7 @@ def _concat_objects(
11781178
result = result.reindex(ax, axis=0)
11791179

11801180
else:
1181-
result = concat(values, axis=0) # maybebug
1181+
result = concat(values, axis=0)
11821182

11831183
if self.obj.ndim == 1:
11841184
name = self.obj.name

pandas/core/interchange/from_dataframe.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ def _from_dataframe(df: DataFrameXchg, allow_copy: bool = True) -> pd.DataFrame:
144144
elif len(pandas_dfs) == 1:
145145
pandas_df = pandas_dfs[0]
146146
else:
147-
pandas_df = pd.concat(pandas_dfs, axis=0, ignore_index=True, copy=False) # bug
147+
pandas_df = pd.concat(
148+
pandas_dfs, axis=0, ignore_index=True, copy=False
149+
) # nobug
148150

149151
index_obj = df.metadata.get("pandas.index", None)
150152
if index_obj is not None:

pandas/core/reshape/melt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ def melt_stub(df, stub: str, i, j, value_vars, sep: str):
668668
value_vars_flattened.extend(value_var)
669669
_melted.append(melt_stub(df, stub, i, j, value_var, sep))
670670

671-
melted = concat(_melted, axis=1) # maybebug
671+
melted = concat(_melted, axis=1) # nobug
672672
id_vars = df.columns.difference(value_vars_flattened)
673673
new = df[id_vars]
674674

pandas/core/series.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2994,7 +2994,7 @@ def searchsorted( # type: ignore[override]
29942994
def _append_internal(self, to_append: Series, ignore_index: bool = False) -> Series:
29952995
from pandas.core.reshape.concat import concat
29962996

2997-
return concat([self, to_append], ignore_index=ignore_index) # maybebug
2997+
return concat([self, to_append], ignore_index=ignore_index) # nobug
29982998

29992999
def compare(
30003000
self,
@@ -3271,7 +3271,7 @@ def combine_first(self, other) -> Series:
32713271
if this.dtype.kind == "M" and other.dtype.kind != "M":
32723272
# TODO: try to match resos?
32733273
other = to_datetime(other)
3274-
combined = concat([this, other]) # bug
3274+
combined = concat([this, other]) # nobug
32753275
combined = combined.reindex(new_index)
32763276
return combined.__finalize__(self, method="combine_first")
32773277

pandas/core/strings/accessor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ def cat(
659659
# align if required
660660
if any(not data.index.equals(x.index) for x in others):
661661
# Need to add keys for uniqueness in case of duplicate columns
662-
others = concat( # bug
662+
others = concat( # nobug
663663
others,
664664
axis=1,
665665
join=(join if join == "inner" else "outer"),

pandas/tests/apply/test_frame_apply.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,34 @@ def test_listlike_lambda(ops, by_row, expected):
915915
tm.assert_equal(result, expected)
916916

917917

918+
def test_listlike_datetime_index_unsorted():
919+
values = [datetime(2024, 1, 1), datetime(2024, 1, 2)]
920+
df = DataFrame({"a": [1, 2]}, index=[values[1], values[0]])
921+
result = df.apply([lambda x: x], by_row=False)
922+
expected = DataFrame(
923+
[[1], [2]],
924+
index=[values[1], values[0]],
925+
columns=MultiIndex([["a"], ["<lambda>"]], codes=[[0], [0]]),
926+
)
927+
tm.assert_frame_equal(result, expected)
928+
929+
930+
def test_dictlike_datetime_index_unsorted():
931+
values = [datetime(2024, 1, 1), datetime(2024, 1, 2), datetime(2024, 1, 3)]
932+
df = DataFrame({"a": [1, 2], "b": [3, 4]}, index=[values[1], values[0]])
933+
result = df.apply(
934+
{"a": lambda x: x, "b": lambda x: x.shift(freq="D")}, by_row=False
935+
)
936+
expected = DataFrame(
937+
{
938+
"a": [1.0, 2.0, np.nan],
939+
"b": [4.0, np.nan, 3.0],
940+
},
941+
index=[values[1], values[0], values[2]],
942+
)
943+
tm.assert_frame_equal(result, expected)
944+
945+
918946
@pytest.mark.parametrize(
919947
"ops",
920948
[

pandas/tests/frame/methods/test_shift.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,3 +794,16 @@ def test_shift_invalid_fill_value_deprecation(self):
794794
df["a"].shift(1, fill_value=NaT)
795795
with tm.assert_produces_warning(Pandas4Warning, match=msg):
796796
df["b"].shift(1, fill_value=NaT)
797+
798+
def test_shift_dt_index_multiple_periods_unsorted(self):
799+
values = date_range("1/1/2000", periods=4, freq="D")
800+
df = DataFrame({"a": [1, 2]}, index=[values[1], values[0]])
801+
result = df.shift(periods=[1, 2], freq="D")
802+
expected = DataFrame(
803+
{
804+
"a_1": [1.0, 2.0, np.nan],
805+
"a_2": [2.0, np.nan, 1.0],
806+
},
807+
index=[values[2], values[1], values[3]],
808+
)
809+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)