PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/pandas/tests/test_tseries.py

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