PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/pandas/tests/test_tseries.py

http://github.com/pydata/pandas
Python | 722 lines | 505 code | 197 blank | 20 comment | 17 complexity | 0b75ae61d965090702f8eec5cc706124 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. import nose
  2. from numpy import nan
  3. import numpy as np
  4. from pandas import Index, isnull, Timestamp
  5. from pandas.util.testing import assert_almost_equal
  6. import pandas.util.testing as tm
  7. from pandas.compat import range, lrange, zip
  8. import pandas.lib as lib
  9. import pandas.algos as algos
  10. class TestTseriesUtil(tm.TestCase):
  11. _multiprocess_can_split_ = True
  12. def test_combineFunc(self):
  13. pass
  14. def test_reindex(self):
  15. pass
  16. def test_isnull(self):
  17. pass
  18. def test_groupby(self):
  19. pass
  20. def test_groupby_withnull(self):
  21. pass
  22. def test_backfill(self):
  23. old = Index([1, 5, 10])
  24. new = Index(lrange(12))
  25. filler = algos.backfill_int64(old, new)
  26. expect_filler = [0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1]
  27. self.assert_numpy_array_equal(filler, expect_filler)
  28. # corner case
  29. old = Index([1, 4])
  30. new = Index(lrange(5, 10))
  31. filler = algos.backfill_int64(old, new)
  32. expect_filler = [-1, -1, -1, -1, -1]
  33. self.assert_numpy_array_equal(filler, expect_filler)
  34. def test_pad(self):
  35. old = Index([1, 5, 10])
  36. new = Index(lrange(12))
  37. filler = algos.pad_int64(old, new)
  38. expect_filler = [-1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2]
  39. self.assert_numpy_array_equal(filler, expect_filler)
  40. # corner case
  41. old = Index([5, 10])
  42. new = Index(lrange(5))
  43. filler = algos.pad_int64(old, new)
  44. expect_filler = [-1, -1, -1, -1, -1]
  45. self.assert_numpy_array_equal(filler, expect_filler)
  46. def test_left_join_indexer_unique():
  47. a = np.array([1, 2, 3, 4, 5], dtype=np.int64)
  48. b = np.array([2, 2, 3, 4, 4], dtype=np.int64)
  49. result = algos.left_join_indexer_unique_int64(b, a)
  50. expected = np.array([1, 1, 2, 3, 3], dtype=np.int64)
  51. assert(np.array_equal(result, expected))
  52. def test_left_outer_join_bug():
  53. left = np.array([0, 1, 0, 1, 1, 2, 3, 1, 0, 2, 1, 2, 0, 1, 1, 2, 3, 2, 3,
  54. 2, 1, 1, 3, 0, 3, 2, 3, 0, 0, 2, 3, 2, 0, 3, 1, 3, 0, 1,
  55. 3, 0, 0, 1, 0, 3, 1, 0, 1, 0, 1, 1, 0, 2, 2, 2, 2, 2, 0,
  56. 3, 1, 2, 0, 0, 3, 1, 3, 2, 2, 0, 1, 3, 0, 2, 3, 2, 3, 3,
  57. 2, 3, 3, 1, 3, 2, 0, 0, 3, 1, 1, 1, 0, 2, 3, 3, 1, 2, 0,
  58. 3, 1, 2, 0, 2], dtype=np.int64)
  59. right = np.array([3, 1], dtype=np.int64)
  60. max_groups = 4
  61. lidx, ridx = algos.left_outer_join(left, right, max_groups, sort=False)
  62. exp_lidx = np.arange(len(left))
  63. exp_ridx = -np.ones(len(left))
  64. exp_ridx[left == 1] = 1
  65. exp_ridx[left == 3] = 0
  66. assert(np.array_equal(lidx, exp_lidx))
  67. assert(np.array_equal(ridx, exp_ridx))
  68. def test_inner_join_indexer():
  69. a = np.array([1, 2, 3, 4, 5], dtype=np.int64)
  70. b = np.array([0, 3, 5, 7, 9], dtype=np.int64)
  71. index, ares, bres = algos.inner_join_indexer_int64(a, b)
  72. index_exp = np.array([3, 5], dtype=np.int64)
  73. assert_almost_equal(index, index_exp)
  74. aexp = np.array([2, 4])
  75. bexp = np.array([1, 2])
  76. assert_almost_equal(ares, aexp)
  77. assert_almost_equal(bres, bexp)
  78. a = np.array([5], dtype=np.int64)
  79. b = np.array([5], dtype=np.int64)
  80. index, ares, bres = algos.inner_join_indexer_int64(a, b)
  81. assert_almost_equal(index, [5])
  82. assert_almost_equal(ares, [0])
  83. assert_almost_equal(bres, [0])
  84. def test_outer_join_indexer():
  85. a = np.array([1, 2, 3, 4, 5], dtype=np.int64)
  86. b = np.array([0, 3, 5, 7, 9], dtype=np.int64)
  87. index, ares, bres = algos.outer_join_indexer_int64(a, b)
  88. index_exp = np.array([0, 1, 2, 3, 4, 5, 7, 9], dtype=np.int64)
  89. assert_almost_equal(index, index_exp)
  90. aexp = np.array([-1, 0, 1, 2, 3, 4, -1, -1], dtype=np.int64)
  91. bexp = np.array([0, -1, -1, 1, -1, 2, 3, 4])
  92. assert_almost_equal(ares, aexp)
  93. assert_almost_equal(bres, bexp)
  94. a = np.array([5], dtype=np.int64)
  95. b = np.array([5], dtype=np.int64)
  96. index, ares, bres = algos.outer_join_indexer_int64(a, b)
  97. assert_almost_equal(index, [5])
  98. assert_almost_equal(ares, [0])
  99. assert_almost_equal(bres, [0])
  100. def test_left_join_indexer():
  101. a = np.array([1, 2, 3, 4, 5], dtype=np.int64)
  102. b = np.array([0, 3, 5, 7, 9], dtype=np.int64)
  103. index, ares, bres = algos.left_join_indexer_int64(a, b)
  104. assert_almost_equal(index, a)
  105. aexp = np.array([0, 1, 2, 3, 4], dtype=np.int64)
  106. bexp = np.array([-1, -1, 1, -1, 2], dtype=np.int64)
  107. assert_almost_equal(ares, aexp)
  108. assert_almost_equal(bres, bexp)
  109. a = np.array([5], dtype=np.int64)
  110. b = np.array([5], dtype=np.int64)
  111. index, ares, bres = algos.left_join_indexer_int64(a, b)
  112. assert_almost_equal(index, [5])
  113. assert_almost_equal(ares, [0])
  114. assert_almost_equal(bres, [0])
  115. def test_left_join_indexer2():
  116. idx = Index([1, 1, 2, 5])
  117. idx2 = Index([1, 2, 5, 7, 9])
  118. res, lidx, ridx = algos.left_join_indexer_int64(idx2, idx)
  119. exp_res = np.array([1, 1, 2, 5, 7, 9], dtype=np.int64)
  120. assert_almost_equal(res, exp_res)
  121. exp_lidx = np.array([0, 0, 1, 2, 3, 4], dtype=np.int64)
  122. assert_almost_equal(lidx, exp_lidx)
  123. exp_ridx = np.array([0, 1, 2, 3, -1, -1], dtype=np.int64)
  124. assert_almost_equal(ridx, exp_ridx)
  125. def test_outer_join_indexer2():
  126. idx = Index([1, 1, 2, 5])
  127. idx2 = Index([1, 2, 5, 7, 9])
  128. res, lidx, ridx = algos.outer_join_indexer_int64(idx2, idx)
  129. exp_res = np.array([1, 1, 2, 5, 7, 9], dtype=np.int64)
  130. assert_almost_equal(res, exp_res)
  131. exp_lidx = np.array([0, 0, 1, 2, 3, 4], dtype=np.int64)
  132. assert_almost_equal(lidx, exp_lidx)
  133. exp_ridx = np.array([0, 1, 2, 3, -1, -1], dtype=np.int64)
  134. assert_almost_equal(ridx, exp_ridx)
  135. def test_inner_join_indexer2():
  136. idx = Index([1, 1, 2, 5])
  137. idx2 = Index([1, 2, 5, 7, 9])
  138. res, lidx, ridx = algos.inner_join_indexer_int64(idx2, idx)
  139. exp_res = np.array([1, 1, 2, 5], dtype=np.int64)
  140. assert_almost_equal(res, exp_res)
  141. exp_lidx = np.array([0, 0, 1, 2], dtype=np.int64)
  142. assert_almost_equal(lidx, exp_lidx)
  143. exp_ridx = np.array([0, 1, 2, 3], dtype=np.int64)
  144. assert_almost_equal(ridx, exp_ridx)
  145. def test_is_lexsorted():
  146. failure = [
  147. np.array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  148. 3, 3,
  149. 3, 3,
  150. 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  151. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
  152. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  153. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  154. 0, 0, 0, 0, 0, 0, 0, 0, 0]),
  155. np.array([30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
  156. 15, 14,
  157. 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 30, 29, 28,
  158. 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
  159. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 30, 29, 28, 27, 26, 25,
  160. 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8,
  161. 7, 6, 5, 4, 3, 2, 1, 0, 30, 29, 28, 27, 26, 25, 24, 23, 22,
  162. 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5,
  163. 4, 3, 2, 1, 0])]
  164. assert(not algos.is_lexsorted(failure))
  165. # def test_get_group_index():
  166. # a = np.array([0, 1, 2, 0, 2, 1, 0, 0], dtype=np.int64)
  167. # b = np.array([1, 0, 3, 2, 0, 2, 3, 0], dtype=np.int64)
  168. # expected = np.array([1, 4, 11, 2, 8, 6, 3, 0], dtype=np.int64)
  169. # result = lib.get_group_index([a, b], (3, 4))
  170. # assert(np.array_equal(result, expected))
  171. def test_groupsort_indexer():
  172. a = np.random.randint(0, 1000, 100).astype(np.int64)
  173. b = np.random.randint(0, 1000, 100).astype(np.int64)
  174. result = algos.groupsort_indexer(a, 1000)[0]
  175. # need to use a stable sort
  176. expected = np.argsort(a, kind='mergesort')
  177. assert(np.array_equal(result, expected))
  178. # compare with lexsort
  179. key = a * 1000 + b
  180. result = algos.groupsort_indexer(key, 1000000)[0]
  181. expected = np.lexsort((b, a))
  182. assert(np.array_equal(result, expected))
  183. def test_ensure_platform_int():
  184. arr = np.arange(100)
  185. result = algos.ensure_platform_int(arr)
  186. assert(result is arr)
  187. def test_duplicated_with_nas():
  188. keys = np.array([0, 1, nan, 0, 2, nan], dtype=object)
  189. result = lib.duplicated(keys)
  190. expected = [False, False, False, True, False, True]
  191. assert(np.array_equal(result, expected))
  192. result = lib.duplicated(keys, take_last=True)
  193. expected = [True, False, True, False, False, False]
  194. assert(np.array_equal(result, expected))
  195. keys = np.empty(8, dtype=object)
  196. for i, t in enumerate(zip([0, 0, nan, nan] * 2, [0, nan, 0, nan] * 2)):
  197. keys[i] = t
  198. result = lib.duplicated(keys)
  199. falses = [False] * 4
  200. trues = [True] * 4
  201. expected = falses + trues
  202. assert(np.array_equal(result, expected))
  203. result = lib.duplicated(keys, take_last=True)
  204. expected = trues + falses
  205. assert(np.array_equal(result, expected))
  206. def test_maybe_booleans_to_slice():
  207. arr = np.array([0, 0, 1, 1, 1, 0, 1], dtype=np.uint8)
  208. result = lib.maybe_booleans_to_slice(arr)
  209. assert(result.dtype == np.bool_)
  210. result = lib.maybe_booleans_to_slice(arr[:0])
  211. assert(result == slice(0, 0))
  212. def test_convert_objects():
  213. arr = np.array(['a', 'b', nan, nan, 'd', 'e', 'f'], dtype='O')
  214. result = lib.maybe_convert_objects(arr)
  215. assert(result.dtype == np.object_)
  216. def test_convert_infs():
  217. arr = np.array(['inf', 'inf', 'inf'], dtype='O')
  218. result = lib.maybe_convert_numeric(arr, set(), False)
  219. assert(result.dtype == np.float64)
  220. arr = np.array(['-inf', '-inf', '-inf'], dtype='O')
  221. result = lib.maybe_convert_numeric(arr, set(), False)
  222. assert(result.dtype == np.float64)
  223. def test_convert_objects_ints():
  224. # test that we can detect many kinds of integers
  225. dtypes = ['i1', 'i2', 'i4', 'i8', 'u1', 'u2', 'u4', 'u8']
  226. for dtype_str in dtypes:
  227. arr = np.array(list(np.arange(20, dtype=dtype_str)), dtype='O')
  228. assert(arr[0].dtype == np.dtype(dtype_str))
  229. result = lib.maybe_convert_objects(arr)
  230. assert(issubclass(result.dtype.type, np.integer))
  231. def test_convert_objects_complex_number():
  232. for dtype in np.sctypes['complex']:
  233. arr = np.array(list(1j * np.arange(20, dtype=dtype)), dtype='O')
  234. assert(arr[0].dtype == np.dtype(dtype))
  235. result = lib.maybe_convert_objects(arr)
  236. assert(issubclass(result.dtype.type, np.complexfloating))
  237. def test_rank():
  238. tm._skip_if_no_scipy()
  239. from scipy.stats import rankdata
  240. def _check(arr):
  241. mask = ~np.isfinite(arr)
  242. arr = arr.copy()
  243. result = algos.rank_1d_float64(arr)
  244. arr[mask] = np.inf
  245. exp = rankdata(arr)
  246. exp[mask] = nan
  247. assert_almost_equal(result, exp)
  248. _check(np.array([nan, nan, 5., 5., 5., nan, 1, 2, 3, nan]))
  249. _check(np.array([4., nan, 5., 5., 5., nan, 1, 2, 4., nan]))
  250. def test_get_reverse_indexer():
  251. indexer = np.array([-1, -1, 1, 2, 0, -1, 3, 4], dtype=np.int64)
  252. result = lib.get_reverse_indexer(indexer, 5)
  253. expected = np.array([4, 2, 3, 6, 7], dtype=np.int64)
  254. assert(np.array_equal(result, expected))
  255. def test_pad_backfill_object_segfault():
  256. from datetime import datetime
  257. old = np.array([], dtype='O')
  258. new = np.array([datetime(2010, 12, 31)], dtype='O')
  259. result = algos.pad_object(old, new)
  260. expected = np.array([-1], dtype=np.int64)
  261. assert(np.array_equal(result, expected))
  262. result = algos.pad_object(new, old)
  263. expected = np.array([], dtype=np.int64)
  264. assert(np.array_equal(result, expected))
  265. result = algos.backfill_object(old, new)
  266. expected = np.array([-1], dtype=np.int64)
  267. assert(np.array_equal(result, expected))
  268. result = algos.backfill_object(new, old)
  269. expected = np.array([], dtype=np.int64)
  270. assert(np.array_equal(result, expected))
  271. def test_arrmap():
  272. values = np.array(['foo', 'foo', 'bar', 'bar', 'baz', 'qux'], dtype='O')
  273. result = algos.arrmap_object(values, lambda x: x in ['foo', 'bar'])
  274. assert(result.dtype == np.bool_)
  275. def test_series_grouper():
  276. from pandas import Series
  277. obj = Series(np.random.randn(10))
  278. dummy = obj[:0]
  279. labels = np.array([-1, -1, -1, 0, 0, 0, 1, 1, 1, 1], dtype=np.int64)
  280. grouper = lib.SeriesGrouper(obj, np.mean, labels, 2, dummy)
  281. result, counts = grouper.get_result()
  282. expected = np.array([obj[3:6].mean(), obj[6:].mean()])
  283. assert_almost_equal(result, expected)
  284. exp_counts = np.array([3, 4], dtype=np.int64)
  285. assert_almost_equal(counts, exp_counts)
  286. def test_series_bin_grouper():
  287. from pandas import Series
  288. obj = Series(np.random.randn(10))
  289. dummy = obj[:0]
  290. bins = np.array([3, 6])
  291. grouper = lib.SeriesBinGrouper(obj, np.mean, bins, dummy)
  292. result, counts = grouper.get_result()
  293. expected = np.array([obj[:3].mean(), obj[3:6].mean(), obj[6:].mean()])
  294. assert_almost_equal(result, expected)
  295. exp_counts = np.array([3, 3, 4], dtype=np.int64)
  296. assert_almost_equal(counts, exp_counts)
  297. class TestBinGroupers(tm.TestCase):
  298. _multiprocess_can_split_ = True
  299. def setUp(self):
  300. self.obj = np.random.randn(10, 1)
  301. self.labels = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2, 2], dtype=np.int64)
  302. self.bins = np.array([3, 6], dtype=np.int64)
  303. def test_generate_bins(self):
  304. from pandas.core.groupby import generate_bins_generic
  305. values = np.array([1, 2, 3, 4, 5, 6], dtype=np.int64)
  306. binner = np.array([0, 3, 6, 9], dtype=np.int64)
  307. for func in [lib.generate_bins_dt64, generate_bins_generic]:
  308. bins = func(values, binner, closed='left')
  309. assert((bins == np.array([2, 5, 6])).all())
  310. bins = func(values, binner, closed='right')
  311. assert((bins == np.array([3, 6, 6])).all())
  312. for func in [lib.generate_bins_dt64, generate_bins_generic]:
  313. values = np.array([1, 2, 3, 4, 5, 6], dtype=np.int64)
  314. binner = np.array([0, 3, 6], dtype=np.int64)
  315. bins = func(values, binner, closed='right')
  316. assert((bins == np.array([3, 6])).all())
  317. self.assertRaises(ValueError, generate_bins_generic, values, [],
  318. 'right')
  319. self.assertRaises(ValueError, generate_bins_generic, values[:0],
  320. binner, 'right')
  321. self.assertRaises(ValueError, generate_bins_generic,
  322. values, [4], 'right')
  323. self.assertRaises(ValueError, generate_bins_generic,
  324. values, [-3, -1], 'right')
  325. def test_group_bin_functions(self):
  326. dtypes = ['float32','float64']
  327. funcs = ['add', 'mean', 'prod', 'min', 'max', 'var']
  328. np_funcs = {
  329. 'add': np.sum,
  330. 'mean': np.mean,
  331. 'prod': np.prod,
  332. 'min': np.min,
  333. 'max': np.max,
  334. 'var': lambda x: x.var(ddof=1) if len(x) >= 2 else np.nan
  335. }
  336. for fname in funcs:
  337. for d in dtypes:
  338. check_less_precise = False
  339. if d == 'float32':
  340. check_less_precise = True
  341. args = [getattr(algos, 'group_%s_%s' % (fname,d)),
  342. getattr(algos, 'group_%s_bin_%s' % (fname,d)),
  343. np_funcs[fname],
  344. d,
  345. check_less_precise]
  346. self._check_versions(*args)
  347. def _check_versions(self, irr_func, bin_func, np_func, dtype, check_less_precise):
  348. obj = self.obj.astype(dtype)
  349. cts = np.zeros(3, dtype=np.int64)
  350. exp = np.zeros((3, 1), dtype)
  351. irr_func(exp, cts, obj, self.labels)
  352. # bin-based version
  353. bins = np.array([3, 6], dtype=np.int64)
  354. out = np.zeros((3, 1), dtype)
  355. counts = np.zeros(len(out), dtype=np.int64)
  356. bin_func(out, counts, obj, bins)
  357. assert_almost_equal(out, exp, check_less_precise=check_less_precise)
  358. bins = np.array([3, 9, 10], dtype=np.int64)
  359. out = np.zeros((3, 1), dtype)
  360. counts = np.zeros(len(out), dtype=np.int64)
  361. bin_func(out, counts, obj, bins)
  362. exp = np.array([np_func(obj[:3]), np_func(obj[3:9]),
  363. np_func(obj[9:])],
  364. dtype=dtype)
  365. assert_almost_equal(out.squeeze(), exp, check_less_precise=check_less_precise)
  366. # duplicate bins
  367. bins = np.array([3, 6, 10, 10], dtype=np.int64)
  368. out = np.zeros((4, 1), dtype)
  369. counts = np.zeros(len(out), dtype=np.int64)
  370. bin_func(out, counts, obj, bins)
  371. exp = np.array([np_func(obj[:3]), np_func(obj[3:6]),
  372. np_func(obj[6:10]), np.nan],
  373. dtype=dtype)
  374. assert_almost_equal(out.squeeze(), exp, check_less_precise=check_less_precise)
  375. def test_group_ohlc():
  376. def _check(dtype):
  377. obj = np.array(np.random.randn(20),dtype=dtype)
  378. bins = np.array([6, 12], dtype=np.int64)
  379. out = np.zeros((3, 4), dtype)
  380. counts = np.zeros(len(out), dtype=np.int64)
  381. func = getattr(algos,'group_ohlc_%s' % dtype)
  382. func(out, counts, obj[:, None], bins)
  383. def _ohlc(group):
  384. if isnull(group).all():
  385. return np.repeat(nan, 4)
  386. return [group[0], group.max(), group.min(), group[-1]]
  387. expected = np.array([_ohlc(obj[:6]), _ohlc(obj[6:12]),
  388. _ohlc(obj[12:])])
  389. assert_almost_equal(out, expected)
  390. assert_almost_equal(counts, [6, 6, 8])
  391. obj[:6] = nan
  392. func(out, counts, obj[:, None], bins)
  393. expected[0] = nan
  394. assert_almost_equal(out, expected)
  395. _check('float32')
  396. _check('float64')
  397. def test_try_parse_dates():
  398. from dateutil.parser import parse
  399. arr = np.array(['5/1/2000', '6/1/2000', '7/1/2000'], dtype=object)
  400. result = lib.try_parse_dates(arr, dayfirst=True)
  401. expected = [parse(d, dayfirst=True) for d in arr]
  402. assert(np.array_equal(result, expected))
  403. class TestTypeInference(tm.TestCase):
  404. _multiprocess_can_split_ = True
  405. def test_length_zero(self):
  406. result = lib.infer_dtype(np.array([], dtype='i4'))
  407. self.assertEqual(result, 'integer')
  408. result = lib.infer_dtype([])
  409. self.assertEqual(result, 'empty')
  410. def test_integers(self):
  411. arr = np.array([1, 2, 3, np.int64(4), np.int32(5)], dtype='O')
  412. result = lib.infer_dtype(arr)
  413. self.assertEqual(result, 'integer')
  414. arr = np.array([1, 2, 3, np.int64(4), np.int32(5), 'foo'],
  415. dtype='O')
  416. result = lib.infer_dtype(arr)
  417. self.assertEqual(result, 'mixed-integer')
  418. arr = np.array([1, 2, 3, 4, 5], dtype='i4')
  419. result = lib.infer_dtype(arr)
  420. self.assertEqual(result, 'integer')
  421. def test_bools(self):
  422. arr = np.array([True, False, True, True, True], dtype='O')
  423. result = lib.infer_dtype(arr)
  424. self.assertEqual(result, 'boolean')
  425. arr = np.array([np.bool_(True), np.bool_(False)], dtype='O')
  426. result = lib.infer_dtype(arr)
  427. self.assertEqual(result, 'boolean')
  428. arr = np.array([True, False, True, 'foo'], dtype='O')
  429. result = lib.infer_dtype(arr)
  430. self.assertEqual(result, 'mixed')
  431. arr = np.array([True, False, True], dtype=bool)
  432. result = lib.infer_dtype(arr)
  433. self.assertEqual(result, 'boolean')
  434. def test_floats(self):
  435. arr = np.array([1., 2., 3., np.float64(4), np.float32(5)], dtype='O')
  436. result = lib.infer_dtype(arr)
  437. self.assertEqual(result, 'floating')
  438. arr = np.array([1, 2, 3, np.float64(4), np.float32(5), 'foo'],
  439. dtype='O')
  440. result = lib.infer_dtype(arr)
  441. self.assertEqual(result, 'mixed-integer')
  442. arr = np.array([1, 2, 3, 4, 5], dtype='f4')
  443. result = lib.infer_dtype(arr)
  444. self.assertEqual(result, 'floating')
  445. arr = np.array([1, 2, 3, 4, 5], dtype='f8')
  446. result = lib.infer_dtype(arr)
  447. self.assertEqual(result, 'floating')
  448. def test_string(self):
  449. pass
  450. def test_unicode(self):
  451. pass
  452. def test_datetime(self):
  453. import datetime
  454. dates = [datetime.datetime(2012, 1, x) for x in range(1, 20)]
  455. index = Index(dates)
  456. self.assertEqual(index.inferred_type, 'datetime64')
  457. def test_date(self):
  458. import datetime
  459. dates = [datetime.date(2012, 1, x) for x in range(1, 20)]
  460. index = Index(dates)
  461. self.assertEqual(index.inferred_type, 'date')
  462. def test_to_object_array_tuples(self):
  463. r = (5, 6)
  464. values = [r]
  465. result = lib.to_object_array_tuples(values)
  466. try:
  467. # make sure record array works
  468. from collections import namedtuple
  469. record = namedtuple('record', 'x y')
  470. r = record(5, 6)
  471. values = [r]
  472. result = lib.to_object_array_tuples(values)
  473. except ImportError:
  474. pass
  475. def test_object(self):
  476. # GH 7431
  477. # cannot infer more than this as only a single element
  478. arr = np.array([None],dtype='O')
  479. result = lib.infer_dtype(arr)
  480. self.assertEqual(result, 'mixed')
  481. class TestMoments(tm.TestCase):
  482. pass
  483. class TestReducer(tm.TestCase):
  484. def test_int_index(self):
  485. from pandas.core.series import Series
  486. arr = np.random.randn(100, 4)
  487. result = lib.reduce(arr, np.sum, labels=Index(np.arange(4)))
  488. expected = arr.sum(0)
  489. assert_almost_equal(result, expected)
  490. result = lib.reduce(arr, np.sum, axis=1, labels=Index(np.arange(100)))
  491. expected = arr.sum(1)
  492. assert_almost_equal(result, expected)
  493. dummy = Series(0., index=np.arange(100))
  494. result = lib.reduce(
  495. arr, np.sum, dummy=dummy, labels=Index(np.arange(4)))
  496. expected = arr.sum(0)
  497. assert_almost_equal(result, expected)
  498. dummy = Series(0., index=np.arange(4))
  499. result = lib.reduce(arr, np.sum, axis=1,
  500. dummy=dummy, labels=Index(np.arange(100)))
  501. expected = arr.sum(1)
  502. assert_almost_equal(result, expected)
  503. class TestTsUtil(tm.TestCase):
  504. def test_min_valid(self):
  505. # Ensure that Timestamp.min is a valid Timestamp
  506. Timestamp(Timestamp.min)
  507. def test_max_valid(self):
  508. # Ensure that Timestamp.max is a valid Timestamp
  509. Timestamp(Timestamp.max)
  510. def test_to_datetime_bijective(self):
  511. # Ensure that converting to datetime and back only loses precision
  512. # by going from nanoseconds to microseconds.
  513. self.assertEqual(Timestamp(Timestamp.max.to_pydatetime()).value/1000, Timestamp.max.value/1000)
  514. self.assertEqual(Timestamp(Timestamp.min.to_pydatetime()).value/1000, Timestamp.min.value/1000)
  515. class TestPeriodField(tm.TestCase):
  516. def test_get_period_field_raises_on_out_of_range(self):
  517. from pandas import tslib
  518. self.assertRaises(ValueError, tslib.get_period_field, -1, 0, 0)
  519. def test_get_period_field_array_raises_on_out_of_range(self):
  520. from pandas import tslib
  521. self.assertRaises(ValueError, tslib.get_period_field_arr, -1, np.empty(1), 0)
  522. if __name__ == '__main__':
  523. import nose
  524. nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
  525. exit=False)