8000 BUG: Avoid duplicating entire exploded column when joining back with origi… by MarcoGorelli · Pull Request #28010 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG: Avoid duplicating entire exploded column when joining back with origi… #28010

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 23 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Rewrite
Syntax: python explode.py infile template [range]

The template argument is used to construct the names of the
individual frame files.  The frames are numbered file001.ext,
file002.ext, etc.  You can insert %d to control the placement
and syntax of the frame number.

The optional range argument specifies which frames to extract.
You can give one or more ranges like 1-10, 5, -15 etc.  If
omitted, all frames are extracted. method of frame so it doesn't require exploding a series twice
  • Loading branch information
Marco Gorelli authored and MarcoGorelli committed Sep 3, 2019
commit 97baf462335ea832a3a50dd8e916b8745c4999bc
40 changes: 28 additions & 12 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2110,7 +2110,7 @@ def to_stata(
data_label=data_label,
write_index=write_index,
variable_labels=variable_labels,
**kwargs
**kwargs,
)
writer.write_file()

Expand All @@ -2136,7 +2136,7 @@ def to_parquet(
compression="snappy",
index=None,
partition_cols=None,
**kwargs
**kwargs,
):
"""
Write a DataFrame to the binary parquet format.
Expand Down Expand Up @@ -2212,7 +2212,7 @@ def to_parquet(
compression=compression,
index=index,
partition_cols=partition_cols,
**kwargs
**kwargs,
)

@Substitution(
Expand Down Expand Up @@ -4186,7 +4186,7 @@ def fillna(
inplace=False,
limit=None,
downcast=None,
**kwargs
**kwargs,
):
return super().fillna(
value=value,
Expand All @@ -4195,7 +4195,7 @@ def fillna(
inplace=inplace,
limit=limit,
downcast=downcast,
**kwargs
**kwargs,
)

@Appender(_shared_docs["replace"] % _shared_doc_kwargs)
Expand Down Expand Up @@ -6259,15 +6259,31 @@ def explode(self, column: Union[str, Tuple]) -> "DataFrame":
if not self.columns.is_unique:
raise ValueError("columns must be unique")

result = self[column].explode()
return (
if isinstance(self.index, MultiIndex):
index_names = [
f"level_{num}" if val is None else val
for num, val in enumerate(self.index.names)
]
else:
index_names = [i if i else "index" for i in [self.index.name]]

column_with_index = self[column].reset_index()

result = (
self.drop([column], axis=1)
.reset_index(drop=True)
.join(self[column].reset_index(drop=True).explode())
.reset_index()
.join(column_with_index[column].explode())
.set_index(index_names)
.reindex(columns=self.columns, copy=False)
.set_index(result.index)
)

if isinstance(self.index, MultiIndex):
result.index.names = self.index.names
else:
result.index.name = self.index.name

return result

def unstack(self, level=-1, fill_value=None):
"""
Pivot a level of the (necessarily hierarchical) index labels, returning
Expand Down Expand Up @@ -6639,7 +6655,7 @@ def _gotitem(
see_also=_agg_summary_and_see_also_doc,
examples=_agg_examples_doc,
versionadded="\n.. versionadded:: 0.20.0\n",
**_shared_doc_kwargs
**_shared_doc_kwargs,
)
@Appender(_shared_docs["aggregate"])
def aggregate(self, func, axis=0, *args, **kwargs):
Expand Down Expand Up @@ -6681,7 +6697,7 @@ def apply(
reduce=None,
result_type=None,
args=(),
**kwds
**kwds,
):
"""
Apply a function along an axis of the DataFrame.
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/frame/test_explode.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ def test_usecase():
dtype=object,
),
),
(
pd.DataFrame(
{"col": [[1, 2], [3, 4]], "other_col": ["a", "b"], "my_index": [0, 0]}
).set_index("my_index"),
pd.DataFrame(
{
"col": [1, 2, 3, 4],
"other_col": ["a", "a", "b", "b"],
"my_index": [0, 0, 0, 0],
},
dtype=object,
).set_index("my_index"),
),
],
)
def test_duplicate_index(df, expected):
Expand Down
0