def test_subclassed_apply(self): # GH 19822 def check_row_subclass(row): assert isinstance(row, tm.SubclassedSeries) def strech(row): if row["variable"] == "height": row["value"] += 0.5 return row df = tm.SubclassedDataFrame( [ ["John", "Doe", "height", 5.5], ["Mary", "Bo", "height", 6.0], ["John", "Doe", "weight", 130], ["Mary", "Bo", "weight", 150], ], columns=["first", "last", "variable", "value"], ) df.apply(lambda x: check_row_subclass(x)) df.apply(lambda x: check_row_subclass(x), axis=1) expected = tm.SubclassedDataFrame( [ ["John", "Doe", "height", 6.0], ["Mary", "Bo", "height", 6.5], ["John", "Doe", "weight", 130], ["Mary", "Bo", "weight", 150], ], columns=["first", "last", "variable", "value"], ) result = df.apply(lambda x: strech(x), axis=1) assert isinstance(result, tm.SubclassedDataFrame) tm.assert_frame_equal(result, expected) expected = tm.SubclassedDataFrame([[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]) result = df.apply(lambda x: tm.SubclassedSeries([1, 2, 3]), axis=1) assert isinstance(result, tm.SubclassedDataFrame) tm.assert_frame_equal(result, expected) result = df.apply(lambda x: [1, 2, 3], axis=1, result_type="expand") assert isinstance(result, tm.SubclassedDataFrame) tm.assert_frame_equal(result, expected) expected = tm.SubclassedSeries([[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]) result = df.apply(lambda x: [1, 2, 3], axis=1) assert not isinstance(result, tm.SubclassedDataFrame) tm.assert_series_equal(result, expected)
def test_indexing_sliced(self): s = tm.SubclassedSeries([1, 2, 3, 4], index=list("abcd")) res = s.loc[["a", "b"]] exp = tm.SubclassedSeries([1, 2], index=list("ab")) tm.assert_series_equal(res, exp) res = s.iloc[[2, 3]] exp = tm.SubclassedSeries([3, 4], index=list("cd")) tm.assert_series_equal(res, exp) res = s.loc[["a", "b"]] exp = tm.SubclassedSeries([1, 2], index=list("ab")) tm.assert_series_equal(res, exp)
def test_asof(self): N = 3 rng = pd.date_range("1/1/1990", periods=N, freq="53s") s = tm.SubclassedSeries({"A": [np.nan, np.nan, np.nan]}, index=rng) result = s.asof(rng[-2:]) assert isinstance(result, tm.SubclassedSeries)
def test_subclass_align_combinations(self): # GH 12983 df = tm.SubclassedDataFrame({ "a": [1, 3, 5], "b": [1, 3, 5] }, index=list("ACE")) s = tm.SubclassedSeries([1, 2, 4], index=list("ABD"), name="x") # frame + series res1, res2 = df.align(s, axis=0) exp1 = pd.DataFrame( { "a": [1, np.nan, 3, np.nan, 5], "b": [1, np.nan, 3, np.nan, 5] }, index=list("ABCDE"), ) # name is lost when exp2 = pd.Series([1, 2, np.nan, 4, np.nan], index=list("ABCDE"), name="x") assert isinstance(res1, tm.SubclassedDataFrame) tm.assert_frame_equal(res1, exp1) assert isinstance(res2, tm.SubclassedSeries) tm.assert_series_equal(res2, exp2) # series + frame res1, res2 = s.align(df) assert isinstance(res1, tm.SubclassedSeries) tm.assert_series_equal(res1, exp2) assert isinstance(res2, tm.SubclassedDataFrame) tm.assert_frame_equal(res2, exp1)
def test_equals(self): # https://github.com/pandas-dev/pandas/pull/34402 # allow subclass in both directions s1 = pd.Series([1, 2, 3]) s2 = tm.SubclassedSeries([1, 2, 3]) assert s1.equals(s2) assert s2.equals(s1)
def test_subclass_unstack(self): # GH 15564 s = tm.SubclassedSeries([1, 2, 3, 4], index=[list("aabb"), list("xyxy")]) res = s.unstack() exp = tm.SubclassedDataFrame({"x": [1, 3], "y": [2, 4]}, index=["a", "b"]) tm.assert_frame_equal(res, exp)
def test_dot(self): df = tm.SubclassedDataFrame([[0, 1, -2, -1], [1, 1, 1, 1]]) s = tm.SubclassedSeries([1, 1, 2, 1]) result = df.dot(s) assert isinstance(result, tm.SubclassedSeries) df = tm.SubclassedDataFrame([[0, 1, -2, -1], [1, 1, 1, 1]]) s = tm.SubclassedDataFrame([1, 1, 2, 1]) result = df.dot(s) assert isinstance(result, tm.SubclassedDataFrame)
def test_indexing_sliced(self): # GH 11559 df = tm.SubclassedDataFrame( {"X": [1, 2, 3], "Y": [4, 5, 6], "Z": [7, 8, 9]}, index=["a", "b", "c"] ) res = df.loc[:, "X"] exp = tm.SubclassedSeries([1, 2, 3], index=list("abc"), name="X") tm.assert_series_equal(res, exp) assert isinstance(res, tm.SubclassedSeries) res = df.iloc[:, 1] exp = tm.SubclassedSeries([4, 5, 6], index=list("abc"), name="Y") tm.assert_series_equal(res, exp) assert isinstance(res, tm.SubclassedSeries) res = df.loc[:, "Z"] exp = tm.SubclassedSeries([7, 8, 9], index=list("abc"), name="Z") tm.assert_series_equal(res, exp) assert isinstance(res, tm.SubclassedSeries) res = df.loc["a", :] exp = tm.SubclassedSeries([1, 4, 7], index=list("XYZ"), name="a") tm.assert_series_equal(res, exp) assert isinstance(res, tm.SubclassedSeries) res = df.iloc[1, :] exp = tm.SubclassedSeries([2, 5, 8], index=list("XYZ"), name="b") tm.assert_series_equal(res, exp) assert isinstance(res, tm.SubclassedSeries) res = df.loc["c", :] exp = tm.SubclassedSeries([3, 6, 9], index=list("XYZ"), name="c") tm.assert_series_equal(res, exp) assert isinstance(res, tm.SubclassedSeries)
def test_subclass_stack(self): # GH 15564 df = tm.SubclassedDataFrame( [[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=["a", "b", "c"], columns=["X", "Y", "Z"], ) res = df.stack() exp = tm.SubclassedSeries( [1, 2, 3, 4, 5, 6, 7, 8, 9], index=[list("aaabbbccc"), list("XYZXYZXYZ")] ) tm.assert_series_equal(res, exp)
def test_subclass_unstack(self): # GH 15564 df = tm.SubclassedDataFrame( [[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=["a", "b", "c"], columns=["X", "Y", "Z"], ) res = df.unstack() exp = tm.SubclassedSeries( [1, 4, 7, 2, 5, 8, 3, 6, 9], index=[list("XXXYYYZZZ"), list("abcabcabc")] ) tm.assert_series_equal(res, exp)
def test_supported_for_subclass_series(self): data = [1, 2, 3] sser = tm.SubclassedSeries(data, dtype=np.intp) expected = Series(data, dtype=np.intp) with ensure_clean_path("temp.h5") as path: sser.to_hdf(path, "ser") result = read_hdf(path, "ser") tm.assert_series_equal(result, expected) with ensure_clean_path("temp.h5") as path: with HDFStore(path) as store: store.put("ser", sser) result = read_hdf(path, "ser") tm.assert_series_equal(result, expected)
def test_indexing_sliced(self, idx_method, indexer, exp_data, exp_idx): s = tm.SubclassedSeries([1, 2, 3, 4], index=list("abcd")) res = getattr(s, idx_method)[indexer] exp = tm.SubclassedSeries(exp_data, index=list(exp_idx)) tm.assert_series_equal(res, exp)
def test_to_frame(self): s = tm.SubclassedSeries([1, 2, 3, 4], index=list("abcd"), name="xxx") res = s.to_frame() exp = tm.SubclassedDataFrame({"xxx": [1, 2, 3, 4]}, index=list("abcd")) tm.assert_frame_equal(res, exp)
from datetime import datetime import numpy as np import pytest from pandas import DataFrame, Series import pandas._testing as tm @pytest.mark.parametrize( "obj", [ tm.SubclassedDataFrame({"A": np.arange(0, 10)}), tm.SubclassedSeries(np.arange(0, 10), name="A"), ], ) @pytest.mark.filterwarnings("ignore:tshift is deprecated:FutureWarning") def test_groupby_preserves_subclass(obj, groupby_func): # GH28330 -- preserve subclass through groupby operations if isinstance(obj, Series) and groupby_func in {"corrwith"}: pytest.skip("Not applicable") grouped = obj.groupby(np.arange(0, 10)) # Groups should preserve subclass type assert isinstance(grouped.get_group(0), type(obj)) args = [] if groupby_func in {"fillna", "nth"}: args.append(0)
def test_subclass_empty_repr(self): with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False): sub_series = tm.SubclassedSeries() assert "SubclassedSeries" in repr(sub_series)
def test_explode(self): s = tm.SubclassedSeries([[1, 2, 3], "foo", [], [3, 4]]) result = s.explode() assert isinstance(result, tm.SubclassedSeries)
def test_subclass_empty_repr(self): with tm.assert_produces_warning(FutureWarning): sub_series = tm.SubclassedSeries() assert "SubclassedSeries" in repr(sub_series)