Advertisement
Guest User

Untitled

a guest
Apr 6th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.00 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "import pymapd\n",
  10. "import pandas as pd\n",
  11. "import numpy as np\n",
  12. "from datetime import date\n",
  13. "import pyarrow as pa"
  14. ]
  15. },
  16. {
  17. "cell_type": "code",
  18. "execution_count": 2,
  19. "metadata": {},
  20. "outputs": [
  21. {
  22. "name": "stdout",
  23. "output_type": "stream",
  24. "text": [
  25. "Connection(mapd://mapd:***@localhost:6274/mapd?protocol=binary)\n"
  26. ]
  27. }
  28. ],
  29. "source": [
  30. "conn = pymapd.connect(user=\"mapd\", password=\"HyperInteractive\", \n",
  31. " host=\"localhost\",dbname=\"mapd\", port=6274, protocol=\"binary\")\n",
  32. "print(conn)"
  33. ]
  34. },
  35. {
  36. "cell_type": "code",
  37. "execution_count": 3,
  38. "metadata": {},
  39. "outputs": [
  40. {
  41. "data": {
  42. "text/plain": [
  43. "<pymapd.cursor.Cursor at 0x7f3fa8903550>"
  44. ]
  45. },
  46. "execution_count": 3,
  47. "metadata": {},
  48. "output_type": "execute_result"
  49. }
  50. ],
  51. "source": [
  52. "d_query = '''DROP TABLE IF EXISTS wam_dates_arrow'''\n",
  53. "query = '''CREATE TABLE wam_dates_arrow(a date)'''\n",
  54. "\n",
  55. "conn.execute(d_query)\n",
  56. "conn.execute(query)"
  57. ]
  58. },
  59. {
  60. "cell_type": "code",
  61. "execution_count": 4,
  62. "metadata": {},
  63. "outputs": [
  64. {
  65. "data": {
  66. "text/plain": [
  67. "<pyarrow.lib.Date32Array object at 0x7f3fa891b6d8>\n",
  68. "[\n",
  69. " 17896,\n",
  70. " null,\n",
  71. " 10957\n",
  72. "]"
  73. ]
  74. },
  75. "execution_count": 4,
  76. "metadata": {},
  77. "output_type": "execute_result"
  78. }
  79. ],
  80. "source": [
  81. "s = pd.Series([date(2018, 12, 31), None, date(2000, 1, 1)])\n",
  82. "arr = pa.array(s)\n",
  83. "arr"
  84. ]
  85. },
  86. {
  87. "cell_type": "code",
  88. "execution_count": 5,
  89. "metadata": {},
  90. "outputs": [
  91. {
  92. "data": {
  93. "text/plain": [
  94. "DataType(date32[day])"
  95. ]
  96. },
  97. "execution_count": 5,
  98. "metadata": {},
  99. "output_type": "execute_result"
  100. }
  101. ],
  102. "source": [
  103. "arr.type"
  104. ]
  105. },
  106. {
  107. "cell_type": "code",
  108. "execution_count": 6,
  109. "metadata": {},
  110. "outputs": [
  111. {
  112. "data": {
  113. "text/html": [
  114. "<div>\n",
  115. "<style scoped>\n",
  116. " .dataframe tbody tr th:only-of-type {\n",
  117. " vertical-align: middle;\n",
  118. " }\n",
  119. "\n",
  120. " .dataframe tbody tr th {\n",
  121. " vertical-align: top;\n",
  122. " }\n",
  123. "\n",
  124. " .dataframe thead th {\n",
  125. " text-align: right;\n",
  126. " }\n",
  127. "</style>\n",
  128. "<table border=\"1\" class=\"dataframe\">\n",
  129. " <thead>\n",
  130. " <tr style=\"text-align: right;\">\n",
  131. " <th></th>\n",
  132. " <th>0</th>\n",
  133. " </tr>\n",
  134. " </thead>\n",
  135. " <tbody>\n",
  136. " <tr>\n",
  137. " <th>0</th>\n",
  138. " <td>2018-12-31</td>\n",
  139. " </tr>\n",
  140. " <tr>\n",
  141. " <th>1</th>\n",
  142. " <td>None</td>\n",
  143. " </tr>\n",
  144. " <tr>\n",
  145. " <th>2</th>\n",
  146. " <td>2000-01-01</td>\n",
  147. " </tr>\n",
  148. " </tbody>\n",
  149. "</table>\n",
  150. "</div>"
  151. ],
  152. "text/plain": [
  153. " 0\n",
  154. "0 2018-12-31\n",
  155. "1 None\n",
  156. "2 2000-01-01"
  157. ]
  158. },
  159. "execution_count": 6,
  160. "metadata": {},
  161. "output_type": "execute_result"
  162. }
  163. ],
  164. "source": [
  165. "df = pd.DataFrame(arr.to_pandas())\n",
  166. "df"
  167. ]
  168. },
  169. {
  170. "cell_type": "code",
  171. "execution_count": 7,
  172. "metadata": {},
  173. "outputs": [
  174. {
  175. "data": {
  176. "text/plain": [
  177. "0 object\n",
  178. "dtype: object"
  179. ]
  180. },
  181. "execution_count": 7,
  182. "metadata": {},
  183. "output_type": "execute_result"
  184. }
  185. ],
  186. "source": [
  187. "df.dtypes"
  188. ]
  189. },
  190. {
  191. "cell_type": "code",
  192. "execution_count": 8,
  193. "metadata": {},
  194. "outputs": [
  195. {
  196. "data": {
  197. "text/html": [
  198. "<div>\n",
  199. "<style scoped>\n",
  200. " .dataframe tbody tr th:only-of-type {\n",
  201. " vertical-align: middle;\n",
  202. " }\n",
  203. "\n",
  204. " .dataframe tbody tr th {\n",
  205. " vertical-align: top;\n",
  206. " }\n",
  207. "\n",
  208. " .dataframe thead th {\n",
  209. " text-align: right;\n",
  210. " }\n",
  211. "</style>\n",
  212. "<table border=\"1\" class=\"dataframe\">\n",
  213. " <thead>\n",
  214. " <tr style=\"text-align: right;\">\n",
  215. " <th></th>\n",
  216. " <th>a</th>\n",
  217. " </tr>\n",
  218. " </thead>\n",
  219. " <tbody>\n",
  220. " <tr>\n",
  221. " <th>0</th>\n",
  222. " <td>2018-12-31</td>\n",
  223. " </tr>\n",
  224. " <tr>\n",
  225. " <th>1</th>\n",
  226. " <td>None</td>\n",
  227. " </tr>\n",
  228. " <tr>\n",
  229. " <th>2</th>\n",
  230. " <td>2000-01-01</td>\n",
  231. " </tr>\n",
  232. " </tbody>\n",
  233. "</table>\n",
  234. "</div>"
  235. ],
  236. "text/plain": [
  237. " a\n",
  238. "0 2018-12-31\n",
  239. "1 None\n",
  240. "2 2000-01-01"
  241. ]
  242. },
  243. "execution_count": 8,
  244. "metadata": {},
  245. "output_type": "execute_result"
  246. }
  247. ],
  248. "source": [
  249. "conn.load_table('wam_dates_arrow', df)\n",
  250. "conn.select_ipc(\"select a from wam_dates_arrow\")"
  251. ]
  252. },
  253. {
  254. "cell_type": "code",
  255. "execution_count": 9,
  256. "metadata": {},
  257. "outputs": [
  258. {
  259. "data": {
  260. "text/plain": [
  261. "<pyarrow.lib.Date64Array object at 0x7f3fa866fb88>\n",
  262. "[\n",
  263. " 1546214400000,\n",
  264. " null,\n",
  265. " 946684800000\n",
  266. "]"
  267. ]
  268. },
  269. "execution_count": 9,
  270. "metadata": {},
  271. "output_type": "execute_result"
  272. }
  273. ],
  274. "source": [
  275. "arr_sec = pa.array(s, type='date64')\n",
  276. "arr_sec"
  277. ]
  278. },
  279. {
  280. "cell_type": "code",
  281. "execution_count": 10,
  282. "metadata": {},
  283. "outputs": [
  284. {
  285. "data": {
  286. "text/plain": [
  287. "DataType(date64[ms])"
  288. ]
  289. },
  290. "execution_count": 10,
  291. "metadata": {},
  292. "output_type": "execute_result"
  293. }
  294. ],
  295. "source": [
  296. "arr_sec.type"
  297. ]
  298. },
  299. {
  300. "cell_type": "code",
  301. "execution_count": 11,
  302. "metadata": {},
  303. "outputs": [
  304. {
  305. "data": {
  306. "text/html": [
  307. "<div>\n",
  308. "<style scoped>\n",
  309. " .dataframe tbody tr th:only-of-type {\n",
  310. " vertical-align: middle;\n",
  311. " }\n",
  312. "\n",
  313. " .dataframe tbody tr th {\n",
  314. " vertical-align: top;\n",
  315. " }\n",
  316. "\n",
  317. " .dataframe thead th {\n",
  318. " text-align: right;\n",
  319. " }\n",
  320. "</style>\n",
  321. "<table border=\"1\" class=\"dataframe\">\n",
  322. " <thead>\n",
  323. " <tr style=\"text-align: right;\">\n",
  324. " <th></th>\n",
  325. " <th>0</th>\n",
  326. " </tr>\n",
  327. " </thead>\n",
  328. " <tbody>\n",
  329. " <tr>\n",
  330. " <th>0</th>\n",
  331. " <td>2018-12-31</td>\n",
  332. " </tr>\n",
  333. " <tr>\n",
  334. " <th>1</th>\n",
  335. " <td>None</td>\n",
  336. " </tr>\n",
  337. " <tr>\n",
  338. " <th>2</th>\n",
  339. " <td>2000-01-01</td>\n",
  340. " </tr>\n",
  341. " </tbody>\n",
  342. "</table>\n",
  343. "</div>"
  344. ],
  345. "text/plain": [
  346. " 0\n",
  347. "0 2018-12-31\n",
  348. "1 None\n",
  349. "2 2000-01-01"
  350. ]
  351. },
  352. "execution_count": 11,
  353. "metadata": {},
  354. "output_type": "execute_result"
  355. }
  356. ],
  357. "source": [
  358. "df1 = pd.DataFrame(arr.to_pandas())\n",
  359. "df1"
  360. ]
  361. },
  362. {
  363. "cell_type": "code",
  364. "execution_count": 12,
  365. "metadata": {},
  366. "outputs": [
  367. {
  368. "data": {
  369. "text/html": [
  370. "<div>\n",
  371. "<style scoped>\n",
  372. " .dataframe tbody tr th:only-of-type {\n",
  373. " vertical-align: middle;\n",
  374. " }\n",
  375. "\n",
  376. " .dataframe tbody tr th {\n",
  377. " vertical-align: top;\n",
  378. " }\n",
  379. "\n",
  380. " .dataframe thead th {\n",
  381. " text-align: right;\n",
  382. " }\n",
  383. "</style>\n",
  384. "<table border=\"1\" class=\"dataframe\">\n",
  385. " <thead>\n",
  386. " <tr style=\"text-align: right;\">\n",
  387. " <th></th>\n",
  388. " <th>a</th>\n",
  389. " </tr>\n",
  390. " </thead>\n",
  391. " <tbody>\n",
  392. " <tr>\n",
  393. " <th>0</th>\n",
  394. " <td>2018-12-31</td>\n",
  395. " </tr>\n",
  396. " <tr>\n",
  397. " <th>1</th>\n",
  398. " <td>None</td>\n",
  399. " </tr>\n",
  400. " <tr>\n",
  401. " <th>2</th>\n",
  402. " <td>2000-01-01</td>\n",
  403. " </tr>\n",
  404. " <tr>\n",
  405. " <th>3</th>\n",
  406. " <td>2018-12-31</td>\n",
  407. " </tr>\n",
  408. " <tr>\n",
  409. " <th>4</th>\n",
  410. " <td>None</td>\n",
  411. " </tr>\n",
  412. " <tr>\n",
  413. " <th>5</th>\n",
  414. " <td>2000-01-01</td>\n",
  415. " </tr>\n",
  416. " </tbody>\n",
  417. "</table>\n",
  418. "</div>"
  419. ],
  420. "text/plain": [
  421. " a\n",
  422. "0 2018-12-31\n",
  423. "1 None\n",
  424. "2 2000-01-01\n",
  425. "3 2018-12-31\n",
  426. "4 None\n",
  427. "5 2000-01-01"
  428. ]
  429. },
  430. "execution_count": 12,
  431. "metadata": {},
  432. "output_type": "execute_result"
  433. }
  434. ],
  435. "source": [
  436. "conn.load_table('wam_dates_arrow', df1)\n",
  437. "conn.select_ipc(\"select a from wam_dates_arrow\")"
  438. ]
  439. },
  440. {
  441. "cell_type": "code",
  442. "execution_count": null,
  443. "metadata": {},
  444. "outputs": [],
  445. "source": []
  446. }
  447. ],
  448. "metadata": {
  449. "kernelspec": {
  450. "display_name": "Python 3",
  451. "language": "python",
  452. "name": "python3"
  453. },
  454. "language_info": {
  455. "codemirror_mode": {
  456. "name": "ipython",
  457. "version": 3
  458. },
  459. "file_extension": ".py",
  460. "mimetype": "text/x-python",
  461. "name": "python",
  462. "nbconvert_exporter": "python",
  463. "pygments_lexer": "ipython3",
  464. "version": "3.7.1"
  465. }
  466. },
  467. "nbformat": 4,
  468. "nbformat_minor": 2
  469. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement