Skip to content

Commit 982c250

Browse files
code sample for pandas-dev#44866
1 parent d2f3872 commit 982c250

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Diff for: bisect/44866.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# BUG: DataFrame.copy mutates the source dataframe, discarding Series attributes listed in Series._metadata. #44866
2+
"""
3+
Metadata wipeout example.
4+
"""
5+
6+
import pandas as pd
7+
8+
print(pd.__version__)
9+
10+
11+
class S(pd.Series):
12+
_metadata = ["foo"]
13+
14+
@property
15+
def _constructor(self):
16+
return S
17+
18+
@property
19+
def _constructor_expanddim(self):
20+
return DF
21+
22+
23+
class DF(pd.DataFrame):
24+
def __repr__(self):
25+
foos = {k: getattr(v, "foo", None) for k, v in self.items()}
26+
return super().__repr__() + f"\n{foos=}"
27+
28+
@property
29+
def _constructor(self):
30+
return DF
31+
32+
@property
33+
def _constructor_sliced(self):
34+
return S
35+
36+
37+
df = DF({"a": [1]})
38+
df["a"].foo = "bar"
39+
df.copy() # ``DataFrame.copy`` mutates the data!
40+
assert hasattr(df["a"], "foo"), "Dataframe mutated by ``copy`` method"

0 commit comments

Comments
 (0)