test_partial.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. """
  2. test setting *parts* of objects both positionally and label based
  3. TOD: these should be split among the indexer tests
  4. """
  5. from warnings import catch_warnings
  6. import numpy as np
  7. import pytest
  8. import pandas as pd
  9. from pandas import DataFrame, Index, Panel, Series, date_range
  10. from pandas.util import testing as tm
  11. class TestPartialSetting(object):
  12. @pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning")
  13. @pytest.mark.filterwarnings("ignore:\\n.ix:DeprecationWarning")
  14. def test_partial_setting(self):
  15. # GH2578, allow ix and friends to partially set
  16. # series
  17. s_orig = Series([1, 2, 3])
  18. s = s_orig.copy()
  19. s[5] = 5
  20. expected = Series([1, 2, 3, 5], index=[0, 1, 2, 5])
  21. tm.assert_series_equal(s, expected)
  22. s = s_orig.copy()
  23. s.loc[5] = 5
  24. expected = Series([1, 2, 3, 5], index=[0, 1, 2, 5])
  25. tm.assert_series_equal(s, expected)
  26. s = s_orig.copy()
  27. s[5] = 5.
  28. expected = Series([1, 2, 3, 5.], index=[0, 1, 2, 5])
  29. tm.assert_series_equal(s, expected)
  30. s = s_orig.copy()
  31. s.loc[5] = 5.
  32. expected = Series([1, 2, 3, 5.], index=[0, 1, 2, 5])
  33. tm.assert_series_equal(s, expected)
  34. # iloc/iat raise
  35. s = s_orig.copy()
  36. with pytest.raises(IndexError):
  37. s.iloc[3] = 5.
  38. with pytest.raises(IndexError):
  39. s.iat[3] = 5.
  40. # ## frame ##
  41. df_orig = DataFrame(
  42. np.arange(6).reshape(3, 2), columns=['A', 'B'], dtype='int64')
  43. # iloc/iat raise
  44. df = df_orig.copy()
  45. with pytest.raises(IndexError):
  46. df.iloc[4, 2] = 5.
  47. with pytest.raises(IndexError):
  48. df.iat[4, 2] = 5.
  49. # row setting where it exists
  50. expected = DataFrame(dict({'A': [0, 4, 4], 'B': [1, 5, 5]}))
  51. df = df_orig.copy()
  52. df.iloc[1] = df.iloc[2]
  53. tm.assert_frame_equal(df, expected)
  54. expected = DataFrame(dict({'A': [0, 4, 4], 'B': [1, 5, 5]}))
  55. df = df_orig.copy()
  56. df.loc[1] = df.loc[2]
  57. tm.assert_frame_equal(df, expected)
  58. # like 2578, partial setting with dtype preservation
  59. expected = DataFrame(dict({'A': [0, 2, 4, 4], 'B': [1, 3, 5, 5]}))
  60. df = df_orig.copy()
  61. df.loc[3] = df.loc[2]
  62. tm.assert_frame_equal(df, expected)
  63. # single dtype frame, overwrite
  64. expected = DataFrame(dict({'A': [0, 2, 4], 'B': [0, 2, 4]}))
  65. df = df_orig.copy()
  66. with catch_warnings(record=True):
  67. df.ix[:, 'B'] = df.ix[:, 'A']
  68. tm.assert_frame_equal(df, expected)
  69. # mixed dtype frame, overwrite
  70. expected = DataFrame(dict({'A': [0, 2, 4], 'B': Series([0, 2, 4])}))
  71. df = df_orig.copy()
  72. df['B'] = df['B'].astype(np.float64)
  73. with catch_warnings(record=True):
  74. df.ix[:, 'B'] = df.ix[:, 'A']
  75. tm.assert_frame_equal(df, expected)
  76. # single dtype frame, partial setting
  77. expected = df_orig.copy()
  78. expected['C'] = df['A']
  79. df = df_orig.copy()
  80. with catch_warnings(record=True):
  81. df.ix[:, 'C'] = df.ix[:, 'A']
  82. tm.assert_frame_equal(df, expected)
  83. # mixed frame, partial setting
  84. expected = df_orig.copy()
  85. expected['C'] = df['A']
  86. df = df_orig.copy()
  87. with catch_warnings(record=True):
  88. df.ix[:, 'C'] = df.ix[:, 'A']
  89. tm.assert_frame_equal(df, expected)
  90. with catch_warnings(record=True):
  91. # ## panel ##
  92. p_orig = Panel(np.arange(16).reshape(2, 4, 2),
  93. items=['Item1', 'Item2'],
  94. major_axis=pd.date_range('2001/1/12', periods=4),
  95. minor_axis=['A', 'B'], dtype='float64')
  96. # panel setting via item
  97. p_orig = Panel(np.arange(16).reshape(2, 4, 2),
  98. items=['Item1', 'Item2'],
  99. major_axis=pd.date_range('2001/1/12', periods=4),
  100. minor_axis=['A', 'B'], dtype='float64')
  101. expected = p_orig.copy()
  102. expected['Item3'] = expected['Item1']
  103. p = p_orig.copy()
  104. p.loc['Item3'] = p['Item1']
  105. tm.assert_panel_equal(p, expected)
  106. # panel with aligned series
  107. expected = p_orig.copy()
  108. expected = expected.transpose(2, 1, 0)
  109. expected['C'] = DataFrame({'Item1': [30, 30, 30, 30],
  110. 'Item2': [32, 32, 32, 32]},
  111. index=p_orig.major_axis)
  112. expected = expected.transpose(2, 1, 0)
  113. p = p_orig.copy()
  114. p.loc[:, :, 'C'] = Series([30, 32], index=p_orig.items)
  115. tm.assert_panel_equal(p, expected)
  116. # GH 8473
  117. dates = date_range('1/1/2000', periods=8)
  118. df_orig = DataFrame(np.random.randn(8, 4), index=dates,
  119. columns=['A', 'B', 'C', 'D'])
  120. expected = pd.concat([df_orig,
  121. DataFrame({'A': 7},
  122. index=[dates[-1] + dates.freq])],
  123. sort=True)
  124. df = df_orig.copy()
  125. df.loc[dates[-1] + dates.freq, 'A'] = 7
  126. tm.assert_frame_equal(df, expected)
  127. df = df_orig.copy()
  128. df.at[dates[-1] + dates.freq, 'A'] = 7
  129. tm.assert_frame_equal(df, expected)
  130. exp_other = DataFrame({0: 7}, index=[dates[-1] + dates.freq])
  131. expected = pd.concat([df_orig, exp_other], axis=1)
  132. df = df_orig.copy()
  133. df.loc[dates[-1] + dates.freq, 0] = 7
  134. tm.assert_frame_equal(df, expected)
  135. df = df_orig.copy()
  136. df.at[dates[-1] + dates.freq, 0] = 7
  137. tm.assert_frame_equal(df, expected)
  138. def test_partial_setting_mixed_dtype(self):
  139. # in a mixed dtype environment, try to preserve dtypes
  140. # by appending
  141. df = DataFrame([[True, 1], [False, 2]], columns=["female", "fitness"])
  142. s = df.loc[1].copy()
  143. s.name = 2
  144. expected = df.append(s)
  145. df.loc[2] = df.loc[1]
  146. tm.assert_frame_equal(df, expected)
  147. # columns will align
  148. df = DataFrame(columns=['A', 'B'])
  149. df.loc[0] = Series(1, index=range(4))
  150. tm.assert_frame_equal(df, DataFrame(columns=['A', 'B'], index=[0]))
  151. # columns will align
  152. df = DataFrame(columns=['A', 'B'])
  153. df.loc[0] = Series(1, index=['B'])
  154. exp = DataFrame([[np.nan, 1]], columns=['A', 'B'],
  155. index=[0], dtype='float64')
  156. tm.assert_frame_equal(df, exp)
  157. # list-like must conform
  158. df = DataFrame(columns=['A', 'B'])
  159. with pytest.raises(ValueError):
  160. df.loc[0] = [1, 2, 3]
  161. # TODO: #15657, these are left as object and not coerced
  162. df = DataFrame(columns=['A', 'B'])
  163. df.loc[3] = [6, 7]
  164. exp = DataFrame([[6, 7]], index=[3], columns=['A', 'B'],
  165. dtype='object')
  166. tm.assert_frame_equal(df, exp)
  167. def test_series_partial_set(self):
  168. # partial set with new index
  169. # Regression from GH4825
  170. ser = Series([0.1, 0.2], index=[1, 2])
  171. # loc equiv to .reindex
  172. expected = Series([np.nan, 0.2, np.nan], index=[3, 2, 3])
  173. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  174. result = ser.loc[[3, 2, 3]]
  175. tm.assert_series_equal(result, expected, check_index_type=True)
  176. result = ser.reindex([3, 2, 3])
  177. tm.assert_series_equal(result, expected, check_index_type=True)
  178. expected = Series([np.nan, 0.2, np.nan, np.nan], index=[3, 2, 3, 'x'])
  179. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  180. result = ser.loc[[3, 2, 3, 'x']]
  181. tm.assert_series_equal(result, expected, check_index_type=True)
  182. result = ser.reindex([3, 2, 3, 'x'])
  183. tm.assert_series_equal(result, expected, check_index_type=True)
  184. expected = Series([0.2, 0.2, 0.1], index=[2, 2, 1])
  185. result = ser.loc[[2, 2, 1]]
  186. tm.assert_series_equal(result, expected, check_index_type=True)
  187. expected = Series([0.2, 0.2, np.nan, 0.1], index=[2, 2, 'x', 1])
  188. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  189. result = ser.loc[[2, 2, 'x', 1]]
  190. tm.assert_series_equal(result, expected, check_index_type=True)
  191. result = ser.reindex([2, 2, 'x', 1])
  192. tm.assert_series_equal(result, expected, check_index_type=True)
  193. # raises as nothing in in the index
  194. pytest.raises(KeyError, lambda: ser.loc[[3, 3, 3]])
  195. expected = Series([0.2, 0.2, np.nan], index=[2, 2, 3])
  196. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  197. result = ser.loc[[2, 2, 3]]
  198. tm.assert_series_equal(result, expected, check_index_type=True)
  199. result = ser.reindex([2, 2, 3])
  200. tm.assert_series_equal(result, expected, check_index_type=True)
  201. s = Series([0.1, 0.2, 0.3], index=[1, 2, 3])
  202. expected = Series([0.3, np.nan, np.nan], index=[3, 4, 4])
  203. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  204. result = s.loc[[3, 4, 4]]
  205. tm.assert_series_equal(result, expected, check_index_type=True)
  206. result = s.reindex([3, 4, 4])
  207. tm.assert_series_equal(result, expected, check_index_type=True)
  208. s = Series([0.1, 0.2, 0.3, 0.4],
  209. index=[1, 2, 3, 4])
  210. expected = Series([np.nan, 0.3, 0.3], index=[5, 3, 3])
  211. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  212. result = s.loc[[5, 3, 3]]
  213. tm.assert_series_equal(result, expected, check_index_type=True)
  214. result = s.reindex([5, 3, 3])
  215. tm.assert_series_equal(result, expected, check_index_type=True)
  216. s = Series([0.1, 0.2, 0.3, 0.4],
  217. index=[1, 2, 3, 4])
  218. expected = Series([np.nan, 0.4, 0.4], index=[5, 4, 4])
  219. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  220. result = s.loc[[5, 4, 4]]
  221. tm.assert_series_equal(result, expected, check_index_type=True)
  222. result = s.reindex([5, 4, 4])
  223. tm.assert_series_equal(result, expected, check_index_type=True)
  224. s = Series([0.1, 0.2, 0.3, 0.4],
  225. index=[4, 5, 6, 7])
  226. expected = Series([0.4, np.nan, np.nan], index=[7, 2, 2])
  227. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  228. result = s.loc[[7, 2, 2]]
  229. tm.assert_series_equal(result, expected, check_index_type=True)
  230. result = s.reindex([7, 2, 2])
  231. tm.assert_series_equal(result, expected, check_index_type=True)
  232. s = Series([0.1, 0.2, 0.3, 0.4],
  233. index=[1, 2, 3, 4])
  234. expected = Series([0.4, np.nan, np.nan], index=[4, 5, 5])
  235. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  236. result = s.loc[[4, 5, 5]]
  237. tm.assert_series_equal(result, expected, check_index_type=True)
  238. result = s.reindex([4, 5, 5])
  239. tm.assert_series_equal(result, expected, check_index_type=True)
  240. # iloc
  241. expected = Series([0.2, 0.2, 0.1, 0.1], index=[2, 2, 1, 1])
  242. result = ser.iloc[[1, 1, 0, 0]]
  243. tm.assert_series_equal(result, expected, check_index_type=True)
  244. def test_series_partial_set_with_name(self):
  245. # GH 11497
  246. idx = Index([1, 2], dtype='int64', name='idx')
  247. ser = Series([0.1, 0.2], index=idx, name='s')
  248. # loc
  249. exp_idx = Index([3, 2, 3], dtype='int64', name='idx')
  250. expected = Series([np.nan, 0.2, np.nan], index=exp_idx, name='s')
  251. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  252. result = ser.loc[[3, 2, 3]]
  253. tm.assert_series_equal(result, expected, check_index_type=True)
  254. exp_idx = Index([3, 2, 3, 'x'], dtype='object', name='idx')
  255. expected = Series([np.nan, 0.2, np.nan, np.nan], index=exp_idx,
  256. name='s')
  257. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  258. result = ser.loc[[3, 2, 3, 'x']]
  259. tm.assert_series_equal(result, expected, check_index_type=True)
  260. exp_idx = Index([2, 2, 1], dtype='int64', name='idx')
  261. expected = Series([0.2, 0.2, 0.1], index=exp_idx, name='s')
  262. result = ser.loc[[2, 2, 1]]
  263. tm.assert_series_equal(result, expected, check_index_type=True)
  264. exp_idx = Index([2, 2, 'x', 1], dtype='object', name='idx')
  265. expected = Series([0.2, 0.2, np.nan, 0.1], index=exp_idx, name='s')
  266. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  267. result = ser.loc[[2, 2, 'x', 1]]
  268. tm.assert_series_equal(result, expected, check_index_type=True)
  269. # raises as nothing in in the index
  270. pytest.raises(KeyError, lambda: ser.loc[[3, 3, 3]])
  271. exp_idx = Index([2, 2, 3], dtype='int64', name='idx')
  272. expected = Series([0.2, 0.2, np.nan], index=exp_idx, name='s')
  273. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  274. result = ser.loc[[2, 2, 3]]
  275. tm.assert_series_equal(result, expected, check_index_type=True)
  276. exp_idx = Index([3, 4, 4], dtype='int64', name='idx')
  277. expected = Series([0.3, np.nan, np.nan], index=exp_idx, name='s')
  278. idx = Index([1, 2, 3], dtype='int64', name='idx')
  279. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  280. result = Series([0.1, 0.2, 0.3],
  281. index=idx,
  282. name='s').loc[[3, 4, 4]]
  283. tm.assert_series_equal(result, expected, check_index_type=True)
  284. exp_idx = Index([5, 3, 3], dtype='int64', name='idx')
  285. expected = Series([np.nan, 0.3, 0.3], index=exp_idx, name='s')
  286. idx = Index([1, 2, 3, 4], dtype='int64', name='idx')
  287. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  288. result = Series([0.1, 0.2, 0.3, 0.4], index=idx,
  289. name='s').loc[[5, 3, 3]]
  290. tm.assert_series_equal(result, expected, check_index_type=True)
  291. exp_idx = Index([5, 4, 4], dtype='int64', name='idx')
  292. expected = Series([np.nan, 0.4, 0.4], index=exp_idx, name='s')
  293. idx = Index([1, 2, 3, 4], dtype='int64', name='idx')
  294. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  295. result = Series([0.1, 0.2, 0.3, 0.4], index=idx,
  296. name='s').loc[[5, 4, 4]]
  297. tm.assert_series_equal(result, expected, check_index_type=True)
  298. exp_idx = Index([7, 2, 2], dtype='int64', name='idx')
  299. expected = Series([0.4, np.nan, np.nan], index=exp_idx, name='s')
  300. idx = Index([4, 5, 6, 7], dtype='int64', name='idx')
  301. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  302. result = Series([0.1, 0.2, 0.3, 0.4], index=idx,
  303. name='s').loc[[7, 2, 2]]
  304. tm.assert_series_equal(result, expected, check_index_type=True)
  305. exp_idx = Index([4, 5, 5], dtype='int64', name='idx')
  306. expected = Series([0.4, np.nan, np.nan], index=exp_idx, name='s')
  307. idx = Index([1, 2, 3, 4], dtype='int64', name='idx')
  308. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  309. result = Series([0.1, 0.2, 0.3, 0.4], index=idx,
  310. name='s').loc[[4, 5, 5]]
  311. tm.assert_series_equal(result, expected, check_index_type=True)
  312. # iloc
  313. exp_idx = Index([2, 2, 1, 1], dtype='int64', name='idx')
  314. expected = Series([0.2, 0.2, 0.1, 0.1], index=exp_idx, name='s')
  315. result = ser.iloc[[1, 1, 0, 0]]
  316. tm.assert_series_equal(result, expected, check_index_type=True)
  317. @pytest.mark.filterwarnings("ignore:\\n.ix")
  318. def test_partial_set_invalid(self):
  319. # GH 4940
  320. # allow only setting of 'valid' values
  321. orig = tm.makeTimeDataFrame()
  322. df = orig.copy()
  323. # don't allow not string inserts
  324. with pytest.raises(TypeError):
  325. with catch_warnings(record=True):
  326. df.loc[100.0, :] = df.ix[0]
  327. with pytest.raises(TypeError):
  328. with catch_warnings(record=True):
  329. df.loc[100, :] = df.ix[0]
  330. with pytest.raises(TypeError):
  331. with catch_warnings(record=True):
  332. df.ix[100.0, :] = df.ix[0]
  333. with pytest.raises(ValueError):
  334. with catch_warnings(record=True):
  335. df.ix[100, :] = df.ix[0]
  336. # allow object conversion here
  337. df = orig.copy()
  338. with catch_warnings(record=True):
  339. df.loc['a', :] = df.ix[0]
  340. exp = orig.append(Series(df.ix[0], name='a'))
  341. tm.assert_frame_equal(df, exp)
  342. tm.assert_index_equal(df.index, Index(orig.index.tolist() + ['a']))
  343. assert df.index.dtype == 'object'
  344. def test_partial_set_empty_series(self):
  345. # GH5226
  346. # partially set with an empty object series
  347. s = Series()
  348. s.loc[1] = 1
  349. tm.assert_series_equal(s, Series([1], index=[1]))
  350. s.loc[3] = 3
  351. tm.assert_series_equal(s, Series([1, 3], index=[1, 3]))
  352. s = Series()
  353. s.loc[1] = 1.
  354. tm.assert_series_equal(s, Series([1.], index=[1]))
  355. s.loc[3] = 3.
  356. tm.assert_series_equal(s, Series([1., 3.], index=[1, 3]))
  357. s = Series()
  358. s.loc['foo'] = 1
  359. tm.assert_series_equal(s, Series([1], index=['foo']))
  360. s.loc['bar'] = 3
  361. tm.assert_series_equal(s, Series([1, 3], index=['foo', 'bar']))
  362. s.loc[3] = 4
  363. tm.assert_series_equal(s, Series([1, 3, 4], index=['foo', 'bar', 3]))
  364. def test_partial_set_empty_frame(self):
  365. # partially set with an empty object
  366. # frame
  367. df = DataFrame()
  368. with pytest.raises(ValueError):
  369. df.loc[1] = 1
  370. with pytest.raises(ValueError):
  371. df.loc[1] = Series([1], index=['foo'])
  372. with pytest.raises(ValueError):
  373. df.loc[:, 1] = 1
  374. # these work as they don't really change
  375. # anything but the index
  376. # GH5632
  377. expected = DataFrame(columns=['foo'], index=Index([], dtype='int64'))
  378. def f():
  379. df = DataFrame()
  380. df['foo'] = Series([], dtype='object')
  381. return df
  382. tm.assert_frame_equal(f(), expected)
  383. def f():
  384. df = DataFrame()
  385. df['foo'] = Series(df.index)
  386. return df
  387. tm.assert_frame_equal(f(), expected)
  388. def f():
  389. df = DataFrame()
  390. df['foo'] = df.index
  391. return df
  392. tm.assert_frame_equal(f(), expected)
  393. expected = DataFrame(columns=['foo'], index=Index([], dtype='int64'))
  394. expected['foo'] = expected['foo'].astype('float64')
  395. def f():
  396. df = DataFrame()
  397. df['foo'] = []
  398. return df
  399. tm.assert_frame_equal(f(), expected)
  400. def f():
  401. df = DataFrame()
  402. df['foo'] = Series(np.arange(len(df)), dtype='float64')
  403. return df
  404. tm.assert_frame_equal(f(), expected)
  405. def f():
  406. df = DataFrame()
  407. tm.assert_index_equal(df.index, Index([], dtype='object'))
  408. df['foo'] = range(len(df))
  409. return df
  410. expected = DataFrame(columns=['foo'], index=Index([], dtype='int64'))
  411. expected['foo'] = expected['foo'].astype('float64')
  412. tm.assert_frame_equal(f(), expected)
  413. df = DataFrame()
  414. tm.assert_index_equal(df.columns, Index([], dtype=object))
  415. df2 = DataFrame()
  416. df2[1] = Series([1], index=['foo'])
  417. df.loc[:, 1] = Series([1], index=['foo'])
  418. tm.assert_frame_equal(df, DataFrame([[1]], index=['foo'], columns=[1]))
  419. tm.assert_frame_equal(df, df2)
  420. # no index to start
  421. expected = DataFrame({0: Series(1, index=range(4))},
  422. columns=['A', 'B', 0])
  423. df = DataFrame(columns=['A', 'B'])
  424. df[0] = Series(1, index=range(4))
  425. df.dtypes
  426. str(df)
  427. tm.assert_frame_equal(df, expected)
  428. df = DataFrame(columns=['A', 'B'])
  429. df.loc[:, 0] = Series(1, index=range(4))
  430. df.dtypes
  431. str(df)
  432. tm.assert_frame_equal(df, expected)
  433. def test_partial_set_empty_frame_row(self):
  434. # GH5720, GH5744
  435. # don't create rows when empty
  436. expected = DataFrame(columns=['A', 'B', 'New'],
  437. index=Index([], dtype='int64'))
  438. expected['A'] = expected['A'].astype('int64')
  439. expected['B'] = expected['B'].astype('float64')
  440. expected['New'] = expected['New'].astype('float64')
  441. df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]})
  442. y = df[df.A > 5]
  443. y['New'] = np.nan
  444. tm.assert_frame_equal(y, expected)
  445. # tm.assert_frame_equal(y,expected)
  446. expected = DataFrame(columns=['a', 'b', 'c c', 'd'])
  447. expected['d'] = expected['d'].astype('int64')
  448. df = DataFrame(columns=['a', 'b', 'c c'])
  449. df['d'] = 3
  450. tm.assert_frame_equal(df, expected)
  451. tm.assert_series_equal(df['c c'], Series(name='c c', dtype=object))
  452. # reindex columns is ok
  453. df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]})
  454. y = df[df.A > 5]
  455. result = y.reindex(columns=['A', 'B', 'C'])
  456. expected = DataFrame(columns=['A', 'B', 'C'],
  457. index=Index([], dtype='int64'))
  458. expected['A'] = expected['A'].astype('int64')
  459. expected['B'] = expected['B'].astype('float64')
  460. expected['C'] = expected['C'].astype('float64')
  461. tm.assert_frame_equal(result, expected)
  462. def test_partial_set_empty_frame_set_series(self):
  463. # GH 5756
  464. # setting with empty Series
  465. df = DataFrame(Series())
  466. tm.assert_frame_equal(df, DataFrame({0: Series()}))
  467. df = DataFrame(Series(name='foo'))
  468. tm.assert_frame_equal(df, DataFrame({'foo': Series()}))
  469. def test_partial_set_empty_frame_empty_copy_assignment(self):
  470. # GH 5932
  471. # copy on empty with assignment fails
  472. df = DataFrame(index=[0])
  473. df = df.copy()
  474. df['a'] = 0
  475. expected = DataFrame(0, index=[0], columns=['a'])
  476. tm.assert_frame_equal(df, expected)
  477. def test_partial_set_empty_frame_empty_consistencies(self):
  478. # GH 6171
  479. # consistency on empty frames
  480. df = DataFrame(columns=['x', 'y'])
  481. df['x'] = [1, 2]
  482. expected = DataFrame(dict(x=[1, 2], y=[np.nan, np.nan]))
  483. tm.assert_frame_equal(df, expected, check_dtype=False)
  484. df = DataFrame(columns=['x', 'y'])
  485. df['x'] = ['1', '2']
  486. expected = DataFrame(
  487. dict(x=['1', '2'], y=[np.nan, np.nan]), dtype=object)
  488. tm.assert_frame_equal(df, expected)
  489. df = DataFrame(columns=['x', 'y'])
  490. df.loc[0, 'x'] = 1
  491. expected = DataFrame(dict(x=[1], y=[np.nan]))
  492. tm.assert_frame_equal(df, expected, check_dtype=False)