test_subclass.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. import numpy as np
  4. import pytest
  5. import pandas as pd
  6. from pandas import DataFrame, Index, MultiIndex, Panel, Series
  7. from pandas.tests.frame.common import TestData
  8. import pandas.util.testing as tm
  9. class TestDataFrameSubclassing(TestData):
  10. def test_frame_subclassing_and_slicing(self):
  11. # Subclass frame and ensure it returns the right class on slicing it
  12. # In reference to PR 9632
  13. class CustomSeries(Series):
  14. @property
  15. def _constructor(self):
  16. return CustomSeries
  17. def custom_series_function(self):
  18. return 'OK'
  19. class CustomDataFrame(DataFrame):
  20. """
  21. Subclasses pandas DF, fills DF with simulation results, adds some
  22. custom plotting functions.
  23. """
  24. def __init__(self, *args, **kw):
  25. super(CustomDataFrame, self).__init__(*args, **kw)
  26. @property
  27. def _constructor(self):
  28. return CustomDataFrame
  29. _constructor_sliced = CustomSeries
  30. def custom_frame_function(self):
  31. return 'OK'
  32. data = {'col1': range(10),
  33. 'col2': range(10)}
  34. cdf = CustomDataFrame(data)
  35. # Did we get back our own DF class?
  36. assert isinstance(cdf, CustomDataFrame)
  37. # Do we get back our own Series class after selecting a column?
  38. cdf_series = cdf.col1
  39. assert isinstance(cdf_series, CustomSeries)
  40. assert cdf_series.custom_series_function() == 'OK'
  41. # Do we get back our own DF class after slicing row-wise?
  42. cdf_rows = cdf[1:5]
  43. assert isinstance(cdf_rows, CustomDataFrame)
  44. assert cdf_rows.custom_frame_function() == 'OK'
  45. # Make sure sliced part of multi-index frame is custom class
  46. mcol = pd.MultiIndex.from_tuples([('A', 'A'), ('A', 'B')])
  47. cdf_multi = CustomDataFrame([[0, 1], [2, 3]], columns=mcol)
  48. assert isinstance(cdf_multi['A'], CustomDataFrame)
  49. mcol = pd.MultiIndex.from_tuples([('A', ''), ('B', '')])
  50. cdf_multi2 = CustomDataFrame([[0, 1], [2, 3]], columns=mcol)
  51. assert isinstance(cdf_multi2['A'], CustomSeries)
  52. def test_dataframe_metadata(self):
  53. df = tm.SubclassedDataFrame({'X': [1, 2, 3], 'Y': [1, 2, 3]},
  54. index=['a', 'b', 'c'])
  55. df.testattr = 'XXX'
  56. assert df.testattr == 'XXX'
  57. assert df[['X']].testattr == 'XXX'
  58. assert df.loc[['a', 'b'], :].testattr == 'XXX'
  59. assert df.iloc[[0, 1], :].testattr == 'XXX'
  60. # see gh-9776
  61. assert df.iloc[0:1, :].testattr == 'XXX'
  62. # see gh-10553
  63. unpickled = tm.round_trip_pickle(df)
  64. tm.assert_frame_equal(df, unpickled)
  65. assert df._metadata == unpickled._metadata
  66. assert df.testattr == unpickled.testattr
  67. def test_indexing_sliced(self):
  68. # GH 11559
  69. df = tm.SubclassedDataFrame({'X': [1, 2, 3],
  70. 'Y': [4, 5, 6],
  71. 'Z': [7, 8, 9]},
  72. index=['a', 'b', 'c'])
  73. res = df.loc[:, 'X']
  74. exp = tm.SubclassedSeries([1, 2, 3], index=list('abc'), name='X')
  75. tm.assert_series_equal(res, exp)
  76. assert isinstance(res, tm.SubclassedSeries)
  77. res = df.iloc[:, 1]
  78. exp = tm.SubclassedSeries([4, 5, 6], index=list('abc'), name='Y')
  79. tm.assert_series_equal(res, exp)
  80. assert isinstance(res, tm.SubclassedSeries)
  81. res = df.loc[:, 'Z']
  82. exp = tm.SubclassedSeries([7, 8, 9], index=list('abc'), name='Z')
  83. tm.assert_series_equal(res, exp)
  84. assert isinstance(res, tm.SubclassedSeries)
  85. res = df.loc['a', :]
  86. exp = tm.SubclassedSeries([1, 4, 7], index=list('XYZ'), name='a')
  87. tm.assert_series_equal(res, exp)
  88. assert isinstance(res, tm.SubclassedSeries)
  89. res = df.iloc[1, :]
  90. exp = tm.SubclassedSeries([2, 5, 8], index=list('XYZ'), name='b')
  91. tm.assert_series_equal(res, exp)
  92. assert isinstance(res, tm.SubclassedSeries)
  93. res = df.loc['c', :]
  94. exp = tm.SubclassedSeries([3, 6, 9], index=list('XYZ'), name='c')
  95. tm.assert_series_equal(res, exp)
  96. assert isinstance(res, tm.SubclassedSeries)
  97. @pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning")
  98. def test_to_panel_expanddim(self):
  99. # GH 9762
  100. class SubclassedFrame(DataFrame):
  101. @property
  102. def _constructor_expanddim(self):
  103. return SubclassedPanel
  104. class SubclassedPanel(Panel):
  105. pass
  106. index = MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2)])
  107. df = SubclassedFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}, index=index)
  108. result = df.to_panel()
  109. assert isinstance(result, SubclassedPanel)
  110. expected = SubclassedPanel([[[1, 2, 3]], [[4, 5, 6]]],
  111. items=['X', 'Y'], major_axis=[0],
  112. minor_axis=[0, 1, 2],
  113. dtype='int64')
  114. tm.assert_panel_equal(result, expected)
  115. def test_subclass_attr_err_propagation(self):
  116. # GH 11808
  117. class A(DataFrame):
  118. @property
  119. def bar(self):
  120. return self.i_dont_exist
  121. with pytest.raises(AttributeError, match='.*i_dont_exist.*'):
  122. A().bar
  123. def test_subclass_align(self):
  124. # GH 12983
  125. df1 = tm.SubclassedDataFrame({'a': [1, 3, 5],
  126. 'b': [1, 3, 5]}, index=list('ACE'))
  127. df2 = tm.SubclassedDataFrame({'c': [1, 2, 4],
  128. 'd': [1, 2, 4]}, index=list('ABD'))
  129. res1, res2 = df1.align(df2, axis=0)
  130. exp1 = tm.SubclassedDataFrame({'a': [1, np.nan, 3, np.nan, 5],
  131. 'b': [1, np.nan, 3, np.nan, 5]},
  132. index=list('ABCDE'))
  133. exp2 = tm.SubclassedDataFrame({'c': [1, 2, np.nan, 4, np.nan],
  134. 'd': [1, 2, np.nan, 4, np.nan]},
  135. index=list('ABCDE'))
  136. assert isinstance(res1, tm.SubclassedDataFrame)
  137. tm.assert_frame_equal(res1, exp1)
  138. assert isinstance(res2, tm.SubclassedDataFrame)
  139. tm.assert_frame_equal(res2, exp2)
  140. res1, res2 = df1.a.align(df2.c)
  141. assert isinstance(res1, tm.SubclassedSeries)
  142. tm.assert_series_equal(res1, exp1.a)
  143. assert isinstance(res2, tm.SubclassedSeries)
  144. tm.assert_series_equal(res2, exp2.c)
  145. def test_subclass_align_combinations(self):
  146. # GH 12983
  147. df = tm.SubclassedDataFrame({'a': [1, 3, 5],
  148. 'b': [1, 3, 5]}, index=list('ACE'))
  149. s = tm.SubclassedSeries([1, 2, 4], index=list('ABD'), name='x')
  150. # frame + series
  151. res1, res2 = df.align(s, axis=0)
  152. exp1 = pd.DataFrame({'a': [1, np.nan, 3, np.nan, 5],
  153. 'b': [1, np.nan, 3, np.nan, 5]},
  154. index=list('ABCDE'))
  155. # name is lost when
  156. exp2 = pd.Series([1, 2, np.nan, 4, np.nan],
  157. index=list('ABCDE'), name='x')
  158. assert isinstance(res1, tm.SubclassedDataFrame)
  159. tm.assert_frame_equal(res1, exp1)
  160. assert isinstance(res2, tm.SubclassedSeries)
  161. tm.assert_series_equal(res2, exp2)
  162. # series + frame
  163. res1, res2 = s.align(df)
  164. assert isinstance(res1, tm.SubclassedSeries)
  165. tm.assert_series_equal(res1, exp2)
  166. assert isinstance(res2, tm.SubclassedDataFrame)
  167. tm.assert_frame_equal(res2, exp1)
  168. def test_subclass_iterrows(self):
  169. # GH 13977
  170. df = tm.SubclassedDataFrame({'a': [1]})
  171. for i, row in df.iterrows():
  172. assert isinstance(row, tm.SubclassedSeries)
  173. tm.assert_series_equal(row, df.loc[i])
  174. def test_subclass_sparse_slice(self):
  175. rows = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]
  176. ssdf = tm.SubclassedSparseDataFrame(rows)
  177. ssdf.testattr = "testattr"
  178. tm.assert_sp_frame_equal(ssdf.loc[:2],
  179. tm.SubclassedSparseDataFrame(rows[:3]))
  180. tm.assert_sp_frame_equal(ssdf.iloc[:2],
  181. tm.SubclassedSparseDataFrame(rows[:2]))
  182. tm.assert_sp_frame_equal(ssdf[:2],
  183. tm.SubclassedSparseDataFrame(rows[:2]))
  184. assert ssdf.loc[:2].testattr == "testattr"
  185. assert ssdf.iloc[:2].testattr == "testattr"
  186. assert ssdf[:2].testattr == "testattr"
  187. tm.assert_sp_series_equal(ssdf.loc[1],
  188. tm.SubclassedSparseSeries(rows[1]),
  189. check_names=False,
  190. check_kind=False)
  191. tm.assert_sp_series_equal(ssdf.iloc[1],
  192. tm.SubclassedSparseSeries(rows[1]),
  193. check_names=False,
  194. check_kind=False)
  195. def test_subclass_sparse_transpose(self):
  196. ossdf = tm.SubclassedSparseDataFrame([[1, 2, 3],
  197. [4, 5, 6]])
  198. essdf = tm.SubclassedSparseDataFrame([[1, 4],
  199. [2, 5],
  200. [3, 6]])
  201. tm.assert_sp_frame_equal(ossdf.T, essdf)
  202. def test_subclass_stack(self):
  203. # GH 15564
  204. df = tm.SubclassedDataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
  205. index=['a', 'b', 'c'],
  206. columns=['X', 'Y', 'Z'])
  207. res = df.stack()
  208. exp = tm.SubclassedSeries(
  209. [1, 2, 3, 4, 5, 6, 7, 8, 9],
  210. index=[list('aaabbbccc'), list('XYZXYZXYZ')])
  211. tm.assert_series_equal(res, exp)
  212. def test_subclass_stack_multi(self):
  213. # GH 15564
  214. df = tm.SubclassedDataFrame([
  215. [10, 11, 12, 13],
  216. [20, 21, 22, 23],
  217. [30, 31, 32, 33],
  218. [40, 41, 42, 43]],
  219. index=MultiIndex.from_tuples(
  220. list(zip(list('AABB'), list('cdcd'))),
  221. names=['aaa', 'ccc']),
  222. columns=MultiIndex.from_tuples(
  223. list(zip(list('WWXX'), list('yzyz'))),
  224. names=['www', 'yyy']))
  225. exp = tm.SubclassedDataFrame([
  226. [10, 12],
  227. [11, 13],
  228. [20, 22],
  229. [21, 23],
  230. [30, 32],
  231. [31, 33],
  232. [40, 42],
  233. [41, 43]],
  234. index=MultiIndex.from_tuples(list(zip(
  235. list('AAAABBBB'), list('ccddccdd'), list('yzyzyzyz'))),
  236. names=['aaa', 'ccc', 'yyy']),
  237. columns=Index(['W', 'X'], name='www'))
  238. res = df.stack()
  239. tm.assert_frame_equal(res, exp)
  240. res = df.stack('yyy')
  241. tm.assert_frame_equal(res, exp)
  242. exp = tm.SubclassedDataFrame([
  243. [10, 11],
  244. [12, 13],
  245. [20, 21],
  246. [22, 23],
  247. [30, 31],
  248. [32, 33],
  249. [40, 41],
  250. [42, 43]],
  251. index=MultiIndex.from_tuples(list(zip(
  252. list('AAAABBBB'), list('ccddccdd'), list('WXWXWXWX'))),
  253. names=['aaa', 'ccc', 'www']),
  254. columns=Index(['y', 'z'], name='yyy'))
  255. res = df.stack('www')
  256. tm.assert_frame_equal(res, exp)
  257. def test_subclass_stack_multi_mixed(self):
  258. # GH 15564
  259. df = tm.SubclassedDataFrame([
  260. [10, 11, 12.0, 13.0],
  261. [20, 21, 22.0, 23.0],
  262. [30, 31, 32.0, 33.0],
  263. [40, 41, 42.0, 43.0]],
  264. index=MultiIndex.from_tuples(
  265. list(zip(list('AABB'), list('cdcd'))),
  266. names=['aaa', 'ccc']),
  267. columns=MultiIndex.from_tuples(
  268. list(zip(list('WWXX'), list('yzyz'))),
  269. names=['www', 'yyy']))
  270. exp = tm.SubclassedDataFrame([
  271. [10, 12.0],
  272. [11, 13.0],
  273. [20, 22.0],
  274. [21, 23.0],
  275. [30, 32.0],
  276. [31, 33.0],
  277. [40, 42.0],
  278. [41, 43.0]],
  279. index=MultiIndex.from_tuples(list(zip(
  280. list('AAAABBBB'), list('ccddccdd'), list('yzyzyzyz'))),
  281. names=['aaa', 'ccc', 'yyy']),
  282. columns=Index(['W', 'X'], name='www'))
  283. res = df.stack()
  284. tm.assert_frame_equal(res, exp)
  285. res = df.stack('yyy')
  286. tm.assert_frame_equal(res, exp)
  287. exp = tm.SubclassedDataFrame([
  288. [10.0, 11.0],
  289. [12.0, 13.0],
  290. [20.0, 21.0],
  291. [22.0, 23.0],
  292. [30.0, 31.0],
  293. [32.0, 33.0],
  294. [40.0, 41.0],
  295. [42.0, 43.0]],
  296. index=MultiIndex.from_tuples(list(zip(
  297. list('AAAABBBB'), list('ccddccdd'), list('WXWXWXWX'))),
  298. names=['aaa', 'ccc', 'www']),
  299. columns=Index(['y', 'z'], name='yyy'))
  300. res = df.stack('www')
  301. tm.assert_frame_equal(res, exp)
  302. def test_subclass_unstack(self):
  303. # GH 15564
  304. df = tm.SubclassedDataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
  305. index=['a', 'b', 'c'],
  306. columns=['X', 'Y', 'Z'])
  307. res = df.unstack()
  308. exp = tm.SubclassedSeries(
  309. [1, 4, 7, 2, 5, 8, 3, 6, 9],
  310. index=[list('XXXYYYZZZ'), list('abcabcabc')])
  311. tm.assert_series_equal(res, exp)
  312. def test_subclass_unstack_multi(self):
  313. # GH 15564
  314. df = tm.SubclassedDataFrame([
  315. [10, 11, 12, 13],
  316. [20, 21, 22, 23],
  317. [30, 31, 32, 33],
  318. [40, 41, 42, 43]],
  319. index=MultiIndex.from_tuples(
  320. list(zip(list('AABB'), list('cdcd'))),
  321. names=['aaa', 'ccc']),
  322. columns=MultiIndex.from_tuples(
  323. list(zip(list('WWXX'), list('yzyz'))),
  324. names=['www', 'yyy']))
  325. exp = tm.SubclassedDataFrame([
  326. [10, 20, 11, 21, 12, 22, 13, 23],
  327. [30, 40, 31, 41, 32, 42, 33, 43]],
  328. index=Index(['A', 'B'], name='aaa'),
  329. columns=MultiIndex.from_tuples(list(zip(
  330. list('WWWWXXXX'), list('yyzzyyzz'), list('cdcdcdcd'))),
  331. names=['www', 'yyy', 'ccc']))
  332. res = df.unstack()
  333. tm.assert_frame_equal(res, exp)
  334. res = df.unstack('ccc')
  335. tm.assert_frame_equal(res, exp)
  336. exp = tm.SubclassedDataFrame([
  337. [10, 30, 11, 31, 12, 32, 13, 33],
  338. [20, 40, 21, 41, 22, 42, 23, 43]],
  339. index=Index(['c', 'd'], name='ccc'),
  340. columns=MultiIndex.from_tuples(list(zip(
  341. list('WWWWXXXX'), list('yyzzyyzz'), list('ABABABAB'))),
  342. names=['www', 'yyy', 'aaa']))
  343. res = df.unstack('aaa')
  344. tm.assert_frame_equal(res, exp)
  345. def test_subclass_unstack_multi_mixed(self):
  346. # GH 15564
  347. df = tm.SubclassedDataFrame([
  348. [10, 11, 12.0, 13.0],
  349. [20, 21, 22.0, 23.0],
  350. [30, 31, 32.0, 33.0],
  351. [40, 41, 42.0, 43.0]],
  352. index=MultiIndex.from_tuples(
  353. list(zip(list('AABB'), list('cdcd'))),
  354. names=['aaa', 'ccc']),
  355. columns=MultiIndex.from_tuples(
  356. list(zip(list('WWXX'), list('yzyz'))),
  357. names=['www', 'yyy']))
  358. exp = tm.SubclassedDataFrame([
  359. [10, 20, 11, 21, 12.0, 22.0, 13.0, 23.0],
  360. [30, 40, 31, 41, 32.0, 42.0, 33.0, 43.0]],
  361. index=Index(['A', 'B'], name='aaa'),
  362. columns=MultiIndex.from_tuples(list(zip(
  363. list('WWWWXXXX'), list('yyzzyyzz'), list('cdcdcdcd'))),
  364. names=['www', 'yyy', 'ccc']))
  365. res = df.unstack()
  366. tm.assert_frame_equal(res, exp)
  367. res = df.unstack('ccc')
  368. tm.assert_frame_equal(res, exp)
  369. exp = tm.SubclassedDataFrame([
  370. [10, 30, 11, 31, 12.0, 32.0, 13.0, 33.0],
  371. [20, 40, 21, 41, 22.0, 42.0, 23.0, 43.0]],
  372. index=Index(['c', 'd'], name='ccc'),
  373. columns=MultiIndex.from_tuples(list(zip(
  374. list('WWWWXXXX'), list('yyzzyyzz'), list('ABABABAB'))),
  375. names=['www', 'yyy', 'aaa']))
  376. res = df.unstack('aaa')
  377. tm.assert_frame_equal(res, exp)
  378. def test_subclass_pivot(self):
  379. # GH 15564
  380. df = tm.SubclassedDataFrame({
  381. 'index': ['A', 'B', 'C', 'C', 'B', 'A'],
  382. 'columns': ['One', 'One', 'One', 'Two', 'Two', 'Two'],
  383. 'values': [1., 2., 3., 3., 2., 1.]})
  384. pivoted = df.pivot(
  385. index='index', columns='columns', values='values')
  386. expected = tm.SubclassedDataFrame({
  387. 'One': {'A': 1., 'B': 2., 'C': 3.},
  388. 'Two': {'A': 1., 'B': 2., 'C': 3.}})
  389. expected.index.name, expected.columns.name = 'index', 'columns'
  390. tm.assert_frame_equal(pivoted, expected)
  391. def test_subclassed_melt(self):
  392. # GH 15564
  393. cheese = tm.SubclassedDataFrame({
  394. 'first': ['John', 'Mary'],
  395. 'last': ['Doe', 'Bo'],
  396. 'height': [5.5, 6.0],
  397. 'weight': [130, 150]})
  398. melted = pd.melt(cheese, id_vars=['first', 'last'])
  399. expected = tm.SubclassedDataFrame([
  400. ['John', 'Doe', 'height', 5.5],
  401. ['Mary', 'Bo', 'height', 6.0],
  402. ['John', 'Doe', 'weight', 130],
  403. ['Mary', 'Bo', 'weight', 150]],
  404. columns=['first', 'last', 'variable', 'value'])
  405. tm.assert_frame_equal(melted, expected)
  406. def test_subclassed_wide_to_long(self):
  407. # GH 9762
  408. np.random.seed(123)
  409. x = np.random.randn(3)
  410. df = tm.SubclassedDataFrame({
  411. "A1970": {0: "a", 1: "b", 2: "c"},
  412. "A1980": {0: "d", 1: "e", 2: "f"},
  413. "B1970": {0: 2.5, 1: 1.2, 2: .7},
  414. "B1980": {0: 3.2, 1: 1.3, 2: .1},
  415. "X": dict(zip(range(3), x))})
  416. df["id"] = df.index
  417. exp_data = {"X": x.tolist() + x.tolist(),
  418. "A": ['a', 'b', 'c', 'd', 'e', 'f'],
  419. "B": [2.5, 1.2, 0.7, 3.2, 1.3, 0.1],
  420. "year": [1970, 1970, 1970, 1980, 1980, 1980],
  421. "id": [0, 1, 2, 0, 1, 2]}
  422. expected = tm.SubclassedDataFrame(exp_data)
  423. expected = expected.set_index(['id', 'year'])[["X", "A", "B"]]
  424. long_frame = pd.wide_to_long(df, ["A", "B"], i="id", j="year")
  425. tm.assert_frame_equal(long_frame, expected)
  426. def test_subclassed_apply(self):
  427. # GH 19822
  428. def check_row_subclass(row):
  429. assert isinstance(row, tm.SubclassedSeries)
  430. def strech(row):
  431. if row["variable"] == "height":
  432. row["value"] += 0.5
  433. return row
  434. df = tm.SubclassedDataFrame([
  435. ['John', 'Doe', 'height', 5.5],
  436. ['Mary', 'Bo', 'height', 6.0],
  437. ['John', 'Doe', 'weight', 130],
  438. ['Mary', 'Bo', 'weight', 150]],
  439. columns=['first', 'last', 'variable', 'value'])
  440. df.apply(lambda x: check_row_subclass(x))
  441. df.apply(lambda x: check_row_subclass(x), axis=1)
  442. expected = tm.SubclassedDataFrame([
  443. ['John', 'Doe', 'height', 6.0],
  444. ['Mary', 'Bo', 'height', 6.5],
  445. ['John', 'Doe', 'weight', 130],
  446. ['Mary', 'Bo', 'weight', 150]],
  447. columns=['first', 'last', 'variable', 'value'])
  448. result = df.apply(lambda x: strech(x), axis=1)
  449. assert isinstance(result, tm.SubclassedDataFrame)
  450. tm.assert_frame_equal(result, expected)
  451. expected = tm.SubclassedDataFrame([
  452. [1, 2, 3],
  453. [1, 2, 3],
  454. [1, 2, 3],
  455. [1, 2, 3]])
  456. result = df.apply(lambda x: tm.SubclassedSeries([1, 2, 3]), axis=1)
  457. assert isinstance(result, tm.SubclassedDataFrame)
  458. tm.assert_frame_equal(result, expected)
  459. result = df.apply(lambda x: [1, 2, 3], axis=1, result_type="expand")
  460. assert isinstance(result, tm.SubclassedDataFrame)
  461. tm.assert_frame_equal(result, expected)
  462. expected = tm.SubclassedSeries([
  463. [1, 2, 3],
  464. [1, 2, 3],
  465. [1, 2, 3],
  466. [1, 2, 3]])
  467. result = df.apply(lambda x: [1, 2, 3], axis=1)
  468. assert not isinstance(result, tm.SubclassedDataFrame)
  469. tm.assert_series_equal(result, expected)