Skip to content

Commit 06f305d

Browse files
mroeschkepmhatre1
authored andcommitted
REF/PERF: Use concat(..., ignore_index=True) when index doesn't matter (pandas-dev#57913)
1 parent d436f31 commit 06f305d

File tree

6 files changed

+11
-8
lines changed

6 files changed

+11
-8
lines changed

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2622,7 +2622,7 @@ def describe(self) -> DataFrame:
26222622
from pandas import Index
26232623
from pandas.core.reshape.concat import concat
26242624

2625-
result = concat([counts, freqs], axis=1)
2625+
result = concat([counts, freqs], ignore_index=True, axis=1)
26262626
result.columns = Index(["counts", "freqs"])
26272627
result.index.name = "categories"
26282628

pandas/core/groupby/generic.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ def _transform_general(
574574
if results:
575575
from pandas.core.reshape.concat import concat
576576

577-
concatenated = concat(results)
577+
concatenated = concat(results, ignore_index=True)
578578
result = self._set_result_index_ordered(concatenated)
579579
else:
580580
result = self.obj._constructor(dtype=np.float64)
@@ -1803,7 +1803,9 @@ def _transform_general(self, func, engine, engine_kwargs, *args, **kwargs):
18031803
applied.append(res)
18041804

18051805
concat_index = obj.columns
1806-
concatenated = concat(applied, axis=0, verify_integrity=False)
1806+
concatenated = concat(
1807+
applied, axis=0, verify_integrity=False, ignore_index=True
1808+
)
18071809
concatenated = concatenated.reindex(concat_index, axis=1)
18081810
return self._set_result_index_ordered(concatenated)
18091811

@@ -2797,7 +2799,7 @@ def _wrap_transform_general_frame(
27972799
# other dimension; this will preserve dtypes
27982800
# GH14457
27992801
if res.index.is_(obj.index):
2800-
res_frame = concat([res] * len(group.columns), axis=1)
2802+
res_frame = concat([res] * len(group.columns), axis=1, ignore_index=True)
28012803
res_frame.columns = group.columns
28022804
res_frame.index = group.index
28032805
else:

pandas/core/methods/describe.py

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def describe(self, percentiles: Sequence[float] | np.ndarray) -> DataFrame:
175175
d = concat(
176176
[x.reindex(col_names) for x in ldesc],
177177
axis=1,
178+
ignore_index=True,
178179
sort=False,
179180
)
180181
d.columns = data.columns.copy()

pandas/core/reshape/melt.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def melt(
243243
not isinstance(dt, np.dtype) and dt._supports_2d for dt in frame.dtypes
244244
):
245245
mdata[value_name] = concat(
246-
[frame.iloc[:, i] for i in range(frame.shape[1])]
246+
[frame.iloc[:, i] for i in range(frame.shape[1])], ignore_index=True
247247
).values
248248
else:
249249
mdata[value_name] = frame._values.ravel("F")

pandas/core/reshape/pivot.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ def _normalize(
835835

836836
elif normalize == "index":
837837
index_margin = index_margin / index_margin.sum()
838-
table = table._append(index_margin)
838+
table = table._append(index_margin, ignore_index=True)
839839
table = table.fillna(0)
840840
table.index = table_index
841841

@@ -844,7 +844,7 @@ def _normalize(
844844
index_margin = index_margin / index_margin.sum()
845845
index_margin.loc[margins_name] = 1
846846
table = concat([table, column_margin], axis=1)
847-
table = table._append(index_margin)
847+
table = table._append(index_margin, ignore_index=True)
848848

849849
table = table.fillna(0)
850850
table.index = table_index

pandas/core/reshape/reshape.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ def stack_v3(frame: DataFrame, level: list[int]) -> Series | DataFrame:
953953

954954
result: Series | DataFrame
955955
if len(buf) > 0 and not frame.empty:
956-
result = concat(buf)
956+
result = concat(buf, ignore_index=True)
957957
ratio = len(result) // len(frame)
958958
else:
959959
# input is empty

0 commit comments

Comments
 (0)