def test_metadata_propagation_indiv(self, monkeypatch): # check that the metadata matches up on the resulting ops ser = Series(range(3), range(3)) ser.name = "foo" ser2 = Series(range(3), range(3)) ser2.name = "bar" result = ser.T tm.assert_metadata_equivalent(ser, result) def finalize(self, other, method=None, **kwargs): for name in self._metadata: if method == "concat" and name == "filename": value = "+".join([ getattr(obj, name) for obj in other.objs if getattr(obj, name, None) ]) object.__setattr__(self, name, value) else: object.__setattr__(self, name, getattr(other, name, None)) return self with monkeypatch.context() as m: m.setattr(Series, "_metadata", ["name", "filename"]) m.setattr(Series, "__finalize__", finalize) ser.filename = "foo" ser2.filename = "bar" result = pd.concat([ser, ser2]) assert result.filename == "foo+bar" assert result.name is None
def test_metadata_propagation_indiv_resample(self): # resample df = DataFrame( np.random.randn(1000, 2), index=date_range("20130101", periods=1000, freq="s"), ) result = df.resample("1T") tm.assert_metadata_equivalent(df, result)
def test_metadata_propagation_indiv_groupby(self): # groupby df = DataFrame({ "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], "B": ["one", "one", "two", "three", "two", "two", "one", "three"], "C": np.random.randn(8), "D": np.random.randn(8), }) result = df.groupby("A").sum() tm.assert_metadata_equivalent(df, result)
def test_metadata_propagation_indiv_groupby(self): # groupby df = DataFrame({ "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], "B": ["one", "one", "two", "three", "two", "two", "one", "three"], "C": np.random.randn(8), "D": np.random.randn(8), }) msg = "The default value of numeric_only" with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby("A").sum() tm.assert_metadata_equivalent(df, result)
def test_metadata_propagation_indiv_resample(self): # resample ts = Series( np.random.rand(1000), index=date_range("20130101", periods=1000, freq="s"), name="foo", ) result = ts.resample("1T").mean() tm.assert_metadata_equivalent(ts, result) result = ts.resample("1T").min() tm.assert_metadata_equivalent(ts, result) result = ts.resample("1T").apply(lambda x: x.sum()) tm.assert_metadata_equivalent(ts, result)
def test_metadata_propagation(self, frame_or_series): # check that the metadata matches up on the resulting ops o = construct(frame_or_series, shape=3) o.name = "foo" o2 = construct(frame_or_series, shape=3) o2.name = "bar" # ---------- # preserving # ---------- # simple ops with scalars for op in ["__add__", "__sub__", "__truediv__", "__mul__"]: result = getattr(o, op)(1) tm.assert_metadata_equivalent(o, result) # ops with like for op in ["__add__", "__sub__", "__truediv__", "__mul__"]: result = getattr(o, op)(o) tm.assert_metadata_equivalent(o, result) # simple boolean for op in ["__eq__", "__le__", "__ge__"]: v1 = getattr(o, op)(o) tm.assert_metadata_equivalent(o, v1) tm.assert_metadata_equivalent(o, v1 & v1) tm.assert_metadata_equivalent(o, v1 | v1) # combine_first result = o.combine_first(o2) tm.assert_metadata_equivalent(o, result) # --------------------------- # non-preserving (by default) # --------------------------- # add non-like result = o + o2 tm.assert_metadata_equivalent(result) # simple boolean for op in ["__eq__", "__le__", "__ge__"]: # this is a name matching op v1 = getattr(o, op)(o) v2 = getattr(o, op)(o2) tm.assert_metadata_equivalent(v2) tm.assert_metadata_equivalent(v1 & v2) tm.assert_metadata_equivalent(v1 | v2)