Skip to content

REF/PERF: Use concat(..., ignore_index=True) when index doesn't matter #57913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2622,7 +2622,7 @@ def describe(self) -> DataFrame:
from pandas import Index
from pandas.core.reshape.concat import concat

result = concat([counts, freqs], axis=1)
result = concat([counts, freqs], ignore_index=True, axis=1)
result.columns = Index(["counts", "freqs"])
result.index.name = "categories"

Expand Down
8 changes: 5 additions & 3 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def _transform_general(
if results:
from pandas.core.reshape.concat import concat

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

concat_index = obj.columns
concatenated = concat(applied, axis=0, verify_integrity=False)
concatenated = concat(
applied, axis=0, verify_integrity=False, ignore_index=True
)
concatenated = concatenated.reindex(concat_index, axis=1)
return self._set_result_index_ordered(concatenated)

Expand Down Expand Up @@ -2797,7 +2799,7 @@ def _wrap_transform_general_frame(
# other dimension; this will preserve dtypes
# GH14457
if res.index.is_(obj.index):
res_frame = concat([res] * len(group.columns), axis=1)
res_frame = concat([res] * len(group.columns), axis=1, ignore_index=True)
res_frame.columns = group.columns
res_frame.index = group.index
else:
Expand Down
1 change: 1 addition & 0 deletions pandas/core/methods/describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def describe(self, percentiles: Sequence[float] | np.ndarray) -> DataFrame:
d = concat(
[x.reindex(col_names) for x in ldesc],
axis=1,
ignore_index=True,
sort=False,
)
d.columns = data.columns.copy()
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 @@ -243,7 +243,7 @@ def melt(
not isinstance(dt, np.dtype) and dt._supports_2d for dt in frame.dtypes
):
mdata[value_name] = concat(
[frame.iloc[:, i] for i in range(frame.shape[1])]
[frame.iloc[:, i] for i in range(frame.shape[1])], ignore_index=True
).values
else:
mdata[value_name] = frame._values.ravel("F")
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ def _normalize(

elif normalize == "index":
index_margin = index_margin / index_margin.sum()
table = table._append(index_margin)
table = table._append(index_margin, ignore_index=True)
table = table.fillna(0)
table.index = table_index

Expand All @@ -844,7 +844,7 @@ def _normalize(
index_margin = index_margin / index_margin.sum()
index_margin.loc[margins_name] = 1
table = concat([table, column_margin], axis=1)
table = table._append(index_margin)
table = table._append(index_margin, ignore_index=True)

table = table.fillna(0)
table.index = table_index
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ def stack_v3(frame: DataFrame, level: list[int]) -> Series | DataFrame:

result: Series | DataFrame
if len(buf) > 0 and not frame.empty:
result = concat(buf)
result = concat(buf, ignore_index=True)
ratio = len(result) // len(frame)
else:
# input is empty
Expand Down