CoCalc Public FilesmongoTests.ipynbOpen with one click!
Author: Joseph Clark
Views : 157
Description: Arctic tests
Compute Environment: Ubuntu 18.04 (Deprecated)
In [1]:
# import arctic library from arctic import Arctic # import pandas import pandas as pd # import quandl library import quandl # import plots import matplotlib as plt # Start mongod on localhost #!mongod --dbpath /home/user/mongo_data
In [2]:
# Pull some data from qandl aapl = quandl.get("WIKI/AAPL", authtoken="rkeazqH5yf2RspNwACei")
In [3]:
# have a look at it aapl.head()
Open High Low Close Volume Ex-Dividend Split Ratio Adj. Open Adj. High Adj. Low Adj. Close Adj. Volume
Date
1980-12-12 28.75 28.87 28.75 28.75 2093900.0 0.0 1.0 0.422706 0.424470 0.422706 0.422706 117258400.0
1980-12-15 27.38 27.38 27.25 27.25 785200.0 0.0 1.0 0.402563 0.402563 0.400652 0.400652 43971200.0
1980-12-16 25.37 25.37 25.25 25.25 472000.0 0.0 1.0 0.373010 0.373010 0.371246 0.371246 26432000.0
1980-12-17 25.87 26.00 25.87 25.87 385900.0 0.0 1.0 0.380362 0.382273 0.380362 0.380362 21610400.0
1980-12-18 26.63 26.75 26.63 26.63 327900.0 0.0 1.0 0.391536 0.393300 0.391536 0.391536 18362400.0
In [4]:
# Connect to Local MONGODB store = Arctic('localhost') # Create the library - defaults to VersionStore store.initialize_library('NASDAQ') # Access the library library = store['NASDAQ'] # Store the data in the library library.write('AAPL', aapl, metadata={'source': 'Quandl'}) # Reading the data item = library.read('AAPL') aapl_out = item.data metadata = item.metadata
--------------------------------------------------------------------------- ServerSelectionTimeoutError Traceback (most recent call last) <ipython-input-4-e45d72dc513a> in <module>() 3 4 # Create the library - defaults to VersionStore ----> 5 store.initialize_library('NASDAQ') 6 7 # Access the library /usr/local/lib/python3.6/dist-packages/arctic/decorators.py in f_retry(*args, **kwargs) 48 while True: 49 try: ---> 50 return f(*args, **kwargs) 51 except (DuplicateKeyError, ServerSelectionTimeoutError) as e: 52 # Re-raise errors that won't go away. /usr/local/lib/python3.6/dist-packages/arctic/arctic.py in initialize_library(self, library, lib_type, **kwargs) 209 Arguments passed to the Library type for initialization. 210 """ --> 211 l = ArcticLibraryBinding(self, library) 212 # check that we don't create too many namespaces 213 # can be disabled check_library_count=False /usr/local/lib/python3.6/dist-packages/arctic/arctic.py in __init__(self, arctic, library) 410 def __init__(self, arctic, library): 411 self.arctic = arctic --> 412 self._curr_conn = self.arctic._conn 413 self._lock = threading.RLock() 414 database_name, library = self._parse_db_lib(library) /usr/local/lib/python3.6/dist-packages/arctic/decorators.py in f_retry(*args, **kwargs) 48 while True: 49 try: ---> 50 return f(*args, **kwargs) 51 except (DuplicateKeyError, ServerSelectionTimeoutError) as e: 52 # Re-raise errors that won't go away. /usr/local/lib/python3.6/dist-packages/arctic/arctic.py in _conn(self) 140 # Accessing _conn is synchronous. The new PyMongo driver may be lazier than the previous. 141 # Force a connection. --> 142 self.__conn.server_info() 143 144 return self.__conn /usr/local/lib/python3.6/dist-packages/pymongo/mongo_client.py in server_info(self, session) 1452 return self.admin.command("buildinfo", 1453 read_preference=ReadPreference.PRIMARY, -> 1454 session=session) 1455 1456 def list_databases(self, session=None, **kwargs): /usr/local/lib/python3.6/dist-packages/pymongo/database.py in command(self, command, value, check, allowable_errors, read_preference, codec_options, session, **kwargs) 527 """ 528 client = self.__client --> 529 with client._socket_for_reads(read_preference) as (sock_info, slave_ok): 530 return self._command(sock_info, command, slave_ok, value, 531 check, allowable_errors, read_preference, /usr/lib/python3.6/contextlib.py in __enter__(self) 79 def __enter__(self): 80 try: ---> 81 return next(self.gen) 82 except StopIteration: 83 raise RuntimeError("generator didn't yield") from None /usr/local/lib/python3.6/dist-packages/pymongo/mongo_client.py in _socket_for_reads(self, read_preference) 980 topology = self._get_topology() 981 single = topology.description.topology_type == TOPOLOGY_TYPE.Single --> 982 server = topology.select_server(read_preference) 983 with self._get_socket(server) as sock_info: 984 slave_ok = (single and not sock_info.is_mongos) or ( /usr/local/lib/python3.6/dist-packages/pymongo/topology.py in select_server(self, selector, server_selection_timeout, address) 222 return random.choice(self.select_servers(selector, 223 server_selection_timeout, --> 224 address)) 225 226 def select_server_by_address(self, address, /usr/local/lib/python3.6/dist-packages/pymongo/topology.py in select_servers(self, selector, server_selection_timeout, address) 181 with self._lock: 182 server_descriptions = self._select_servers_loop( --> 183 selector, server_timeout, address) 184 185 return [self.get_server_by_address(sd.address) /usr/local/lib/python3.6/dist-packages/pymongo/topology.py in _select_servers_loop(self, selector, timeout, address) 197 if timeout == 0 or now > end_time: 198 raise ServerSelectionTimeoutError( --> 199 self._error_message(selector)) 200 201 self._ensure_opened() ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
In [7]:
# Look at data just pulled from mongo! aapl_out
WARNING: Some output was deleted.
In [8]:
aapl_out['Close'].plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f97fee97320>
In [6]:
# Make some new data testdata = pd.DataFrame({ 'a': [1,2,3,4,5], 'b': ['a', 'a', 'b', 'a', 'c'] }) testdata
a b
0 1 a
1 2 a
2 3 b
3 4 a
4 5 c
In [10]:
# Make a new library store.initialize_library('test') # Access the library testlib = store['test'] # Store the data in the library library.write('TESTDATA', testdata, metadata={'source': 'made it up'}) # Reading the data item = testlib.read('TESTDATA') testdata_out = item.data metadata = item.metadata
Library created, but couldn't enable sharding: no such command: 'enablesharding', bad cmd: '{ enablesharding: "arctic", lsid: { id: UUID("a5f15b0f-a9e6-4f4f-86b0-e28568cff758") }, $readPreference: { mode: "secondaryPreferred" }, $db: "admin" }'. This is OK if you're not 'admin'
In [11]:
# Data back in a dataframe testdata_out
a b
index
0 1 a
1 2 a
2 3 b
3 4 a
4 5 c
In [20]:
# Import some data from csv futData = pd.read_csv('/home/user/data.csv') futData
WARNING: Some output was deleted.
In [ ]:
In [21]:
# Store the data in existing library library = store['NASDAQ'] library.write('futData', futData, metadata={'source': 'NYMEX'}) # Reading the data item = library.read('futData') testdata_out = item.data metadata = item.metadata
Library created, but couldn't enable sharding: no such command: 'enablesharding', bad cmd: '{ enablesharding: "arctic", lsid: { id: UUID("a5f15b0f-a9e6-4f4f-86b0-e28568cff758") }, $readPreference: { mode: "secondaryPreferred" }, $db: "admin" }'. This is OK if you're not 'admin'
In [ ]:
In [22]:
# List symbols in library library.list_symbols()
['AAPL', 'TESTDATA', 'futData']
In [27]:
item = library.read('futData') item.data
WARNING: Some output was deleted.
In [ ]: