methods.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. import inspect
  2. import operator
  3. import numpy as np
  4. import pytest
  5. from pandas._typing import Dtype
  6. from pandas.core.dtypes.common import is_bool_dtype
  7. from pandas.core.dtypes.missing import na_value_for_dtype
  8. import pandas as pd
  9. import pandas._testing as tm
  10. from pandas.core.sorting import nargsort
  11. from pandas.tests.extension.base.base import BaseExtensionTests
  12. class BaseMethodsTests(BaseExtensionTests):
  13. """Various Series and DataFrame methods."""
  14. def test_value_counts_default_dropna(self, data):
  15. # make sure we have consistent default dropna kwarg
  16. if not hasattr(data, "value_counts"):
  17. pytest.skip(f"value_counts is not implemented for {type(data)}")
  18. sig = inspect.signature(data.value_counts)
  19. kwarg = sig.parameters["dropna"]
  20. assert kwarg.default is True
  21. @pytest.mark.parametrize("dropna", [True, False])
  22. def test_value_counts(self, all_data, dropna):
  23. all_data = all_data[:10]
  24. if dropna:
  25. other = all_data[~all_data.isna()]
  26. else:
  27. other = all_data
  28. result = pd.Series(all_data).value_counts(dropna=dropna).sort_index()
  29. expected = pd.Series(other).value_counts(dropna=dropna).sort_index()
  30. self.assert_series_equal(result, expected)
  31. def test_value_counts_with_normalize(self, data):
  32. # GH 33172
  33. data = data[:10].unique()
  34. values = np.array(data[~data.isna()])
  35. ser = pd.Series(data, dtype=data.dtype)
  36. result = ser.value_counts(normalize=True).sort_index()
  37. if not isinstance(data, pd.Categorical):
  38. expected = pd.Series(
  39. [1 / len(values)] * len(values), index=result.index, name="proportion"
  40. )
  41. else:
  42. expected = pd.Series(0.0, index=result.index, name="proportion")
  43. expected[result > 0] = 1 / len(values)
  44. if na_value_for_dtype(data.dtype) is pd.NA:
  45. # TODO(GH#44692): avoid special-casing
  46. expected = expected.astype("Float64")
  47. self.assert_series_equal(result, expected)
  48. def test_count(self, data_missing):
  49. df = pd.DataFrame({"A": data_missing})
  50. result = df.count(axis="columns")
  51. expected = pd.Series([0, 1])
  52. self.assert_series_equal(result, expected)
  53. def test_series_count(self, data_missing):
  54. # GH#26835
  55. ser = pd.Series(data_missing)
  56. result = ser.count()
  57. expected = 1
  58. assert result == expected
  59. def test_apply_simple_series(self, data):
  60. result = pd.Series(data).apply(id)
  61. assert isinstance(result, pd.Series)
  62. def test_argsort(self, data_for_sorting):
  63. result = pd.Series(data_for_sorting).argsort()
  64. # argsort result gets passed to take, so should be np.intp
  65. expected = pd.Series(np.array([2, 0, 1], dtype=np.intp))
  66. self.assert_series_equal(result, expected)
  67. def test_argsort_missing_array(self, data_missing_for_sorting):
  68. result = data_missing_for_sorting.argsort()
  69. # argsort result gets passed to take, so should be np.intp
  70. expected = np.array([2, 0, 1], dtype=np.intp)
  71. tm.assert_numpy_array_equal(result, expected)
  72. def test_argsort_missing(self, data_missing_for_sorting):
  73. result = pd.Series(data_missing_for_sorting).argsort()
  74. expected = pd.Series(np.array([1, -1, 0], dtype=np.intp))
  75. self.assert_series_equal(result, expected)
  76. def test_argmin_argmax(self, data_for_sorting, data_missing_for_sorting, na_value):
  77. # GH 24382
  78. # data_for_sorting -> [B, C, A] with A < B < C
  79. assert data_for_sorting.argmax() == 1
  80. assert data_for_sorting.argmin() == 2
  81. # with repeated values -> first occurrence
  82. data = data_for_sorting.take([2, 0, 0, 1, 1, 2])
  83. assert data.argmax() == 3
  84. assert data.argmin() == 0
  85. # with missing values
  86. # data_missing_for_sorting -> [B, NA, A] with A < B and NA missing.
  87. assert data_missing_for_sorting.argmax() == 0
  88. assert data_missing_for_sorting.argmin() == 2
  89. @pytest.mark.parametrize("method", ["argmax", "argmin"])
  90. def test_argmin_argmax_empty_array(self, method, data):
  91. # GH 24382
  92. err_msg = "attempt to get"
  93. with pytest.raises(ValueError, match=err_msg):
  94. getattr(data[:0], method)()
  95. @pytest.mark.parametrize("method", ["argmax", "argmin"])
  96. def test_argmin_argmax_all_na(self, method, data, na_value):
  97. # all missing with skipna=True is the same as empty
  98. err_msg = "attempt to get"
  99. data_na = type(data)._from_sequence([na_value, na_value], dtype=data.dtype)
  100. with pytest.raises(ValueError, match=err_msg):
  101. getattr(data_na, method)()
  102. @pytest.mark.parametrize(
  103. "op_name, skipna, expected",
  104. [
  105. ("idxmax", True, 0),
  106. ("idxmin", True, 2),
  107. ("argmax", True, 0),
  108. ("argmin", True, 2),
  109. ("idxmax", False, np.nan),
  110. ("idxmin", False, np.nan),
  111. ("argmax", False, -1),
  112. ("argmin", False, -1),
  113. ],
  114. )
  115. def test_argreduce_series(
  116. self, data_missing_for_sorting, op_name, skipna, expected
  117. ):
  118. # data_missing_for_sorting -> [B, NA, A] with A < B and NA missing.
  119. ser = pd.Series(data_missing_for_sorting)
  120. result = getattr(ser, op_name)(skipna=skipna)
  121. tm.assert_almost_equal(result, expected)
  122. def test_argmax_argmin_no_skipna_notimplemented(self, data_missing_for_sorting):
  123. # GH#38733
  124. data = data_missing_for_sorting
  125. with pytest.raises(NotImplementedError, match=""):
  126. data.argmin(skipna=False)
  127. with pytest.raises(NotImplementedError, match=""):
  128. data.argmax(skipna=False)
  129. @pytest.mark.parametrize(
  130. "na_position, expected",
  131. [
  132. ("last", np.array([2, 0, 1], dtype=np.dtype("intp"))),
  133. ("first", np.array([1, 2, 0], dtype=np.dtype("intp"))),
  134. ],
  135. )
  136. def test_nargsort(self, data_missing_for_sorting, na_position, expected):
  137. # GH 25439
  138. result = nargsort(data_missing_for_sorting, na_position=na_position)
  139. tm.assert_numpy_array_equal(result, expected)
  140. @pytest.mark.parametrize("ascending", [True, False])
  141. def test_sort_values(self, data_for_sorting, ascending, sort_by_key):
  142. ser = pd.Series(data_for_sorting)
  143. result = ser.sort_values(ascending=ascending, key=sort_by_key)
  144. expected = ser.iloc[[2, 0, 1]]
  145. if not ascending:
  146. # GH 35922. Expect stable sort
  147. if ser.nunique() == 2:
  148. expected = ser.iloc[[0, 1, 2]]
  149. else:
  150. expected = ser.iloc[[1, 0, 2]]
  151. self.assert_series_equal(result, expected)
  152. @pytest.mark.parametrize("ascending", [True, False])
  153. def test_sort_values_missing(
  154. self, data_missing_for_sorting, ascending, sort_by_key
  155. ):
  156. ser = pd.Series(data_missing_for_sorting)
  157. result = ser.sort_values(ascending=ascending, key=sort_by_key)
  158. if ascending:
  159. expected = ser.iloc[[2, 0, 1]]
  160. else:
  161. expected = ser.iloc[[0, 2, 1]]
  162. self.assert_series_equal(result, expected)
  163. @pytest.mark.parametrize("ascending", [True, False])
  164. def test_sort_values_frame(self, data_for_sorting, ascending):
  165. df = pd.DataFrame({"A": [1, 2, 1], "B": data_for_sorting})
  166. result = df.sort_values(["A", "B"])
  167. expected = pd.DataFrame(
  168. {"A": [1, 1, 2], "B": data_for_sorting.take([2, 0, 1])}, index=[2, 0, 1]
  169. )
  170. self.assert_frame_equal(result, expected)
  171. @pytest.mark.parametrize("box", [pd.Series, lambda x: x])
  172. @pytest.mark.parametrize("method", [lambda x: x.unique(), pd.unique])
  173. def test_unique(self, data, box, method):
  174. duplicated = box(data._from_sequence([data[0], data[0]]))
  175. result = method(duplicated)
  176. assert len(result) == 1
  177. assert isinstance(result, type(data))
  178. assert result[0] == duplicated[0]
  179. def test_factorize(self, data_for_grouping):
  180. codes, uniques = pd.factorize(data_for_grouping, use_na_sentinel=True)
  181. expected_codes = np.array([0, 0, -1, -1, 1, 1, 0, 2], dtype=np.intp)
  182. expected_uniques = data_for_grouping.take([0, 4, 7])
  183. tm.assert_numpy_array_equal(codes, expected_codes)
  184. self.assert_extension_array_equal(uniques, expected_uniques)
  185. def test_factorize_equivalence(self, data_for_grouping):
  186. codes_1, uniques_1 = pd.factorize(data_for_grouping, use_na_sentinel=True)
  187. codes_2, uniques_2 = data_for_grouping.factorize(use_na_sentinel=True)
  188. tm.assert_numpy_array_equal(codes_1, codes_2)
  189. self.assert_extension_array_equal(uniques_1, uniques_2)
  190. assert len(uniques_1) == len(pd.unique(uniques_1))
  191. assert uniques_1.dtype == data_for_grouping.dtype
  192. def test_factorize_empty(self, data):
  193. codes, uniques = pd.factorize(data[:0])
  194. expected_codes = np.array([], dtype=np.intp)
  195. expected_uniques = type(data)._from_sequence([], dtype=data[:0].dtype)
  196. tm.assert_numpy_array_equal(codes, expected_codes)
  197. self.assert_extension_array_equal(uniques, expected_uniques)
  198. def test_fillna_copy_frame(self, data_missing):
  199. arr = data_missing.take([1, 1])
  200. df = pd.DataFrame({"A": arr})
  201. df_orig = df.copy()
  202. filled_val = df.iloc[0, 0]
  203. result = df.fillna(filled_val)
  204. result.iloc[0, 0] = filled_val
  205. self.assert_frame_equal(df, df_orig)
  206. def test_fillna_copy_series(self, data_missing):
  207. arr = data_missing.take([1, 1])
  208. ser = pd.Series(arr, copy=False)
  209. ser_orig = ser.copy()
  210. filled_val = ser[0]
  211. result = ser.fillna(filled_val)
  212. result.iloc[0] = filled_val
  213. self.assert_series_equal(ser, ser_orig)
  214. def test_fillna_length_mismatch(self, data_missing):
  215. msg = "Length of 'value' does not match."
  216. with pytest.raises(ValueError, match=msg):
  217. data_missing.fillna(data_missing.take([1]))
  218. # Subclasses can override if we expect e.g Sparse[bool], boolean, pyarrow[bool]
  219. _combine_le_expected_dtype: Dtype = np.dtype(bool)
  220. def test_combine_le(self, data_repeated):
  221. # GH 20825
  222. # Test that combine works when doing a <= (le) comparison
  223. orig_data1, orig_data2 = data_repeated(2)
  224. s1 = pd.Series(orig_data1)
  225. s2 = pd.Series(orig_data2)
  226. result = s1.combine(s2, lambda x1, x2: x1 <= x2)
  227. expected = pd.Series(
  228. [a <= b for (a, b) in zip(list(orig_data1), list(orig_data2))],
  229. dtype=self._combine_le_expected_dtype,
  230. )
  231. self.assert_series_equal(result, expected)
  232. val = s1.iloc[0]
  233. result = s1.combine(val, lambda x1, x2: x1 <= x2)
  234. expected = pd.Series(
  235. [a <= val for a in list(orig_data1)],
  236. dtype=self._combine_le_expected_dtype,
  237. )
  238. self.assert_series_equal(result, expected)
  239. def test_combine_add(self, data_repeated):
  240. # GH 20825
  241. orig_data1, orig_data2 = data_repeated(2)
  242. s1 = pd.Series(orig_data1)
  243. s2 = pd.Series(orig_data2)
  244. result = s1.combine(s2, lambda x1, x2: x1 + x2)
  245. with np.errstate(over="ignore"):
  246. expected = pd.Series(
  247. orig_data1._from_sequence(
  248. [a + b for (a, b) in zip(list(orig_data1), list(orig_data2))]
  249. )
  250. )
  251. self.assert_series_equal(result, expected)
  252. val = s1.iloc[0]
  253. result = s1.combine(val, lambda x1, x2: x1 + x2)
  254. expected = pd.Series(
  255. orig_data1._from_sequence([a + val for a in list(orig_data1)])
  256. )
  257. self.assert_series_equal(result, expected)
  258. def test_combine_first(self, data):
  259. # https://github.com/pandas-dev/pandas/issues/24147
  260. a = pd.Series(data[:3])
  261. b = pd.Series(data[2:5], index=[2, 3, 4])
  262. result = a.combine_first(b)
  263. expected = pd.Series(data[:5])
  264. self.assert_series_equal(result, expected)
  265. @pytest.mark.parametrize("frame", [True, False])
  266. @pytest.mark.parametrize(
  267. "periods, indices",
  268. [(-2, [2, 3, 4, -1, -1]), (0, [0, 1, 2, 3, 4]), (2, [-1, -1, 0, 1, 2])],
  269. )
  270. def test_container_shift(self, data, frame, periods, indices):
  271. # https://github.com/pandas-dev/pandas/issues/22386
  272. subset = data[:5]
  273. data = pd.Series(subset, name="A")
  274. expected = pd.Series(subset.take(indices, allow_fill=True), name="A")
  275. if frame:
  276. result = data.to_frame(name="A").assign(B=1).shift(periods)
  277. expected = pd.concat(
  278. [expected, pd.Series([1] * 5, name="B").shift(periods)], axis=1
  279. )
  280. compare = self.assert_frame_equal
  281. else:
  282. result = data.shift(periods)
  283. compare = self.assert_series_equal
  284. compare(result, expected)
  285. def test_shift_0_periods(self, data):
  286. # GH#33856 shifting with periods=0 should return a copy, not same obj
  287. result = data.shift(0)
  288. assert data[0] != data[1] # otherwise below is invalid
  289. data[0] = data[1]
  290. assert result[0] != result[1] # i.e. not the same object/view
  291. @pytest.mark.parametrize("periods", [1, -2])
  292. def test_diff(self, data, periods):
  293. data = data[:5]
  294. if is_bool_dtype(data.dtype):
  295. op = operator.xor
  296. else:
  297. op = operator.sub
  298. try:
  299. # does this array implement ops?
  300. op(data, data)
  301. except Exception:
  302. pytest.skip(f"{type(data)} does not support diff")
  303. s = pd.Series(data)
  304. result = s.diff(periods)
  305. expected = pd.Series(op(data, data.shift(periods)))
  306. self.assert_series_equal(result, expected)
  307. df = pd.DataFrame({"A": data, "B": [1.0] * 5})
  308. result = df.diff(periods)
  309. if periods == 1:
  310. b = [np.nan, 0, 0, 0, 0]
  311. else:
  312. b = [0, 0, 0, np.nan, np.nan]
  313. expected = pd.DataFrame({"A": expected, "B": b})
  314. self.assert_frame_equal(result, expected)
  315. @pytest.mark.parametrize(
  316. "periods, indices",
  317. [[-4, [-1, -1]], [-1, [1, -1]], [0, [0, 1]], [1, [-1, 0]], [4, [-1, -1]]],
  318. )
  319. def test_shift_non_empty_array(self, data, periods, indices):
  320. # https://github.com/pandas-dev/pandas/issues/23911
  321. subset = data[:2]
  322. result = subset.shift(periods)
  323. expected = subset.take(indices, allow_fill=True)
  324. self.assert_extension_array_equal(result, expected)
  325. @pytest.mark.parametrize("periods", [-4, -1, 0, 1, 4])
  326. def test_shift_empty_array(self, data, periods):
  327. # https://github.com/pandas-dev/pandas/issues/23911
  328. empty = data[:0]
  329. result = empty.shift(periods)
  330. expected = empty
  331. self.assert_extension_array_equal(result, expected)
  332. def test_shift_zero_copies(self, data):
  333. # GH#31502
  334. result = data.shift(0)
  335. assert result is not data
  336. result = data[:0].shift(2)
  337. assert result is not data
  338. def test_shift_fill_value(self, data):
  339. arr = data[:4]
  340. fill_value = data[0]
  341. result = arr.shift(1, fill_value=fill_value)
  342. expected = data.take([0, 0, 1, 2])
  343. self.assert_extension_array_equal(result, expected)
  344. result = arr.shift(-2, fill_value=fill_value)
  345. expected = data.take([2, 3, 0, 0])
  346. self.assert_extension_array_equal(result, expected)
  347. def test_not_hashable(self, data):
  348. # We are in general mutable, so not hashable
  349. with pytest.raises(TypeError, match="unhashable type"):
  350. hash(data)
  351. def test_hash_pandas_object_works(self, data, as_frame):
  352. # https://github.com/pandas-dev/pandas/issues/23066
  353. data = pd.Series(data)
  354. if as_frame:
  355. data = data.to_frame()
  356. a = pd.util.hash_pandas_object(data)
  357. b = pd.util.hash_pandas_object(data)
  358. self.assert_equal(a, b)
  359. def test_searchsorted(self, data_for_sorting, as_series):
  360. b, c, a = data_for_sorting
  361. arr = data_for_sorting.take([2, 0, 1]) # to get [a, b, c]
  362. if as_series:
  363. arr = pd.Series(arr)
  364. assert arr.searchsorted(a) == 0
  365. assert arr.searchsorted(a, side="right") == 1
  366. assert arr.searchsorted(b) == 1
  367. assert arr.searchsorted(b, side="right") == 2
  368. assert arr.searchsorted(c) == 2
  369. assert arr.searchsorted(c, side="right") == 3
  370. result = arr.searchsorted(arr.take([0, 2]))
  371. expected = np.array([0, 2], dtype=np.intp)
  372. tm.assert_numpy_array_equal(result, expected)
  373. # sorter
  374. sorter = np.array([1, 2, 0])
  375. assert data_for_sorting.searchsorted(a, sorter=sorter) == 0
  376. def test_where_series(self, data, na_value, as_frame):
  377. assert data[0] != data[1]
  378. cls = type(data)
  379. a, b = data[:2]
  380. orig = pd.Series(cls._from_sequence([a, a, b, b], dtype=data.dtype))
  381. ser = orig.copy()
  382. cond = np.array([True, True, False, False])
  383. if as_frame:
  384. ser = ser.to_frame(name="a")
  385. cond = cond.reshape(-1, 1)
  386. result = ser.where(cond)
  387. expected = pd.Series(
  388. cls._from_sequence([a, a, na_value, na_value], dtype=data.dtype)
  389. )
  390. if as_frame:
  391. expected = expected.to_frame(name="a")
  392. self.assert_equal(result, expected)
  393. ser.mask(~cond, inplace=True)
  394. self.assert_equal(ser, expected)
  395. # array other
  396. ser = orig.copy()
  397. if as_frame:
  398. ser = ser.to_frame(name="a")
  399. cond = np.array([True, False, True, True])
  400. other = cls._from_sequence([a, b, a, b], dtype=data.dtype)
  401. if as_frame:
  402. other = pd.DataFrame({"a": other})
  403. cond = pd.DataFrame({"a": cond})
  404. result = ser.where(cond, other)
  405. expected = pd.Series(cls._from_sequence([a, b, b, b], dtype=data.dtype))
  406. if as_frame:
  407. expected = expected.to_frame(name="a")
  408. self.assert_equal(result, expected)
  409. ser.mask(~cond, other, inplace=True)
  410. self.assert_equal(ser, expected)
  411. @pytest.mark.parametrize("repeats", [0, 1, 2, [1, 2, 3]])
  412. def test_repeat(self, data, repeats, as_series, use_numpy):
  413. arr = type(data)._from_sequence(data[:3], dtype=data.dtype)
  414. if as_series:
  415. arr = pd.Series(arr)
  416. result = np.repeat(arr, repeats) if use_numpy else arr.repeat(repeats)
  417. repeats = [repeats] * 3 if isinstance(repeats, int) else repeats
  418. expected = [x for x, n in zip(arr, repeats) for _ in range(n)]
  419. expected = type(data)._from_sequence(expected, dtype=data.dtype)
  420. if as_series:
  421. expected = pd.Series(expected, index=arr.index.repeat(repeats))
  422. self.assert_equal(result, expected)
  423. @pytest.mark.parametrize(
  424. "repeats, kwargs, error, msg",
  425. [
  426. (2, {"axis": 1}, ValueError, "axis"),
  427. (-1, {}, ValueError, "negative"),
  428. ([1, 2], {}, ValueError, "shape"),
  429. (2, {"foo": "bar"}, TypeError, "'foo'"),
  430. ],
  431. )
  432. def test_repeat_raises(self, data, repeats, kwargs, error, msg, use_numpy):
  433. with pytest.raises(error, match=msg):
  434. if use_numpy:
  435. np.repeat(data, repeats, **kwargs)
  436. else:
  437. data.repeat(repeats, **kwargs)
  438. def test_delete(self, data):
  439. result = data.delete(0)
  440. expected = data[1:]
  441. self.assert_extension_array_equal(result, expected)
  442. result = data.delete([1, 3])
  443. expected = data._concat_same_type([data[[0]], data[[2]], data[4:]])
  444. self.assert_extension_array_equal(result, expected)
  445. def test_insert(self, data):
  446. # insert at the beginning
  447. result = data[1:].insert(0, data[0])
  448. self.assert_extension_array_equal(result, data)
  449. result = data[1:].insert(-len(data[1:]), data[0])
  450. self.assert_extension_array_equal(result, data)
  451. # insert at the middle
  452. result = data[:-1].insert(4, data[-1])
  453. taker = np.arange(len(data))
  454. taker[5:] = taker[4:-1]
  455. taker[4] = len(data) - 1
  456. expected = data.take(taker)
  457. self.assert_extension_array_equal(result, expected)
  458. def test_insert_invalid(self, data, invalid_scalar):
  459. item = invalid_scalar
  460. with pytest.raises((TypeError, ValueError)):
  461. data.insert(0, item)
  462. with pytest.raises((TypeError, ValueError)):
  463. data.insert(4, item)
  464. with pytest.raises((TypeError, ValueError)):
  465. data.insert(len(data) - 1, item)
  466. def test_insert_invalid_loc(self, data):
  467. ub = len(data)
  468. with pytest.raises(IndexError):
  469. data.insert(ub + 1, data[0])
  470. with pytest.raises(IndexError):
  471. data.insert(-ub - 1, data[0])
  472. with pytest.raises(TypeError):
  473. # we expect TypeError here instead of IndexError to match np.insert
  474. data.insert(1.5, data[0])
  475. @pytest.mark.parametrize("box", [pd.array, pd.Series, pd.DataFrame])
  476. def test_equals(self, data, na_value, as_series, box):
  477. data2 = type(data)._from_sequence([data[0]] * len(data), dtype=data.dtype)
  478. data_na = type(data)._from_sequence([na_value] * len(data), dtype=data.dtype)
  479. data = tm.box_expected(data, box, transpose=False)
  480. data2 = tm.box_expected(data2, box, transpose=False)
  481. data_na = tm.box_expected(data_na, box, transpose=False)
  482. # we are asserting with `is True/False` explicitly, to test that the
  483. # result is an actual Python bool, and not something "truthy"
  484. assert data.equals(data) is True
  485. assert data.equals(data.copy()) is True
  486. # unequal other data
  487. assert data.equals(data2) is False
  488. assert data.equals(data_na) is False
  489. # different length
  490. assert data[:2].equals(data[:3]) is False
  491. # empty are equal
  492. assert data[:0].equals(data[:0]) is True
  493. # other types
  494. assert data.equals(None) is False
  495. assert data[[0]].equals(data[0]) is False