marinecadastre.gov data
In [1]:
Copied!
from sqlalchemy import create_engine
engine = create_engine('postgresql://postgres:postgres@postgres:5432/postgres', echo=False)
from sqlalchemy import create_engine engine = create_engine('postgresql://postgres:postgres@postgres:5432/postgres', echo=False)
In [2]:
Copied!
import pandas as pd
with engine.connect() as connection:
df = pd.read_sql('ais', connection)
df
import pandas as pd with engine.connect() as connection: df = pd.read_sql('ais', connection) df
--------------------------------------------------------------------------- OperationalError Traceback (most recent call last) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/engine/base.py:3361, in Engine._wrap_pool_connect(self, fn, connection) 3360 try: -> 3361 return fn() 3362 except dialect.dbapi.Error as e: File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:327, in Pool.connect(self) 320 """Return a DBAPI connection from the pool. 321 322 The connection is instrumented such that when its (...) 325 326 """ --> 327 return _ConnectionFairy._checkout(self) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:894, in _ConnectionFairy._checkout(cls, pool, threadconns, fairy) 893 if not fairy: --> 894 fairy = _ConnectionRecord.checkout(pool) 896 fairy._pool = pool File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:493, in _ConnectionRecord.checkout(cls, pool) 491 @classmethod 492 def checkout(cls, pool): --> 493 rec = pool._do_get() 494 try: File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/impl.py:145, in QueuePool._do_get(self) 144 except: --> 145 with util.safe_reraise(): 146 self._dec_overflow() File /opt/conda/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py:70, in safe_reraise.__exit__(self, type_, value, traceback) 69 if not self.warn_only: ---> 70 compat.raise_( 71 exc_value, 72 with_traceback=exc_tb, 73 ) 74 else: File /opt/conda/lib/python3.10/site-packages/sqlalchemy/util/compat.py:211, in raise_(***failed resolving arguments***) 210 try: --> 211 raise exception 212 finally: 213 # credit to 214 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/ 215 # as the __traceback__ object creates a cycle File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/impl.py:143, in QueuePool._do_get(self) 142 try: --> 143 return self._create_connection() 144 except: File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:273, in Pool._create_connection(self) 271 """Called by subclasses to create a new ConnectionRecord.""" --> 273 return _ConnectionRecord(self) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:388, in _ConnectionRecord.__init__(self, pool, connect) 387 if connect: --> 388 self.__connect() 389 self.finalize_callback = deque() File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:690, in _ConnectionRecord.__connect(self) 689 except BaseException as e: --> 690 with util.safe_reraise(): 691 pool.logger.debug("Error on connect(): %s", e) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py:70, in safe_reraise.__exit__(self, type_, value, traceback) 69 if not self.warn_only: ---> 70 compat.raise_( 71 exc_value, 72 with_traceback=exc_tb, 73 ) 74 else: File /opt/conda/lib/python3.10/site-packages/sqlalchemy/util/compat.py:211, in raise_(***failed resolving arguments***) 210 try: --> 211 raise exception 212 finally: 213 # credit to 214 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/ 215 # as the __traceback__ object creates a cycle File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:686, in _ConnectionRecord.__connect(self) 685 self.starttime = time.time() --> 686 self.dbapi_connection = connection = pool._invoke_creator(self) 687 pool.logger.debug("Created new connection %r", connection) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/engine/create.py:578, in create_engine.<locals>.connect(connection_record) 577 return connection --> 578 return dialect.connect(*cargs, **cparams) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/engine/default.py:598, in DefaultDialect.connect(self, *cargs, **cparams) 596 def connect(self, *cargs, **cparams): 597 # inherits the docstring from interfaces.Dialect.connect --> 598 return self.dbapi.connect(*cargs, **cparams) File /opt/conda/lib/python3.10/site-packages/psycopg2/__init__.py:122, in connect(dsn, connection_factory, cursor_factory, **kwargs) 121 dsn = _ext.make_dsn(dsn, **kwargs) --> 122 conn = _connect(dsn, connection_factory=connection_factory, **kwasync) 123 if cursor_factory is not None: OperationalError: could not translate host name "postgres" to address: Name or service not known The above exception was the direct cause of the following exception: OperationalError Traceback (most recent call last) Cell In[2], line 3 1 import pandas as pd ----> 3 with engine.connect() as connection: 4 df = pd.read_sql('ais', connection) 5 df File /opt/conda/lib/python3.10/site-packages/sqlalchemy/engine/base.py:3315, in Engine.connect(self, close_with_result) 3300 def connect(self, close_with_result=False): 3301 """Return a new :class:`_engine.Connection` object. 3302 3303 The :class:`_engine.Connection` object is a facade that uses a DBAPI (...) 3312 3313 """ -> 3315 return self._connection_cls(self, close_with_result=close_with_result) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/engine/base.py:96, in Connection.__init__(self, engine, connection, close_with_result, _branch_from, _execution_options, _dispatch, _has_events, _allow_revalidate) 91 self._has_events = _branch_from._has_events 92 else: 93 self._dbapi_connection = ( 94 connection 95 if connection is not None ---> 96 else engine.raw_connection() 97 ) 99 self._transaction = self._nested_transaction = None 100 self.__savepoint_seq = 0 File /opt/conda/lib/python3.10/site-packages/sqlalchemy/engine/base.py:3394, in Engine.raw_connection(self, _connection) 3372 def raw_connection(self, _connection=None): 3373 """Return a "raw" DBAPI connection from the connection pool. 3374 3375 The returned object is a proxied version of the DBAPI (...) 3392 3393 """ -> 3394 return self._wrap_pool_connect(self.pool.connect, _connection) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/engine/base.py:3364, in Engine._wrap_pool_connect(self, fn, connection) 3362 except dialect.dbapi.Error as e: 3363 if connection is None: -> 3364 Connection._handle_dbapi_exception_noconnection( 3365 e, dialect, self 3366 ) 3367 else: 3368 util.raise_( 3369 sys.exc_info()[1], with_traceback=sys.exc_info()[2] 3370 ) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/engine/base.py:2198, in Connection._handle_dbapi_exception_noconnection(cls, e, dialect, engine) 2196 util.raise_(newraise, with_traceback=exc_info[2], from_=e) 2197 elif should_wrap: -> 2198 util.raise_( 2199 sqlalchemy_exception, with_traceback=exc_info[2], from_=e 2200 ) 2201 else: 2202 util.raise_(exc_info[1], with_traceback=exc_info[2]) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/util/compat.py:211, in raise_(***failed resolving arguments***) 208 exception.__cause__ = replace_context 210 try: --> 211 raise exception 212 finally: 213 # credit to 214 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/ 215 # as the __traceback__ object creates a cycle 216 del exception, replace_context, from_, with_traceback File /opt/conda/lib/python3.10/site-packages/sqlalchemy/engine/base.py:3361, in Engine._wrap_pool_connect(self, fn, connection) 3359 dialect = self.dialect 3360 try: -> 3361 return fn() 3362 except dialect.dbapi.Error as e: 3363 if connection is None: File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:327, in Pool.connect(self) 319 def connect(self): 320 """Return a DBAPI connection from the pool. 321 322 The connection is instrumented such that when its (...) 325 326 """ --> 327 return _ConnectionFairy._checkout(self) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:894, in _ConnectionFairy._checkout(cls, pool, threadconns, fairy) 891 @classmethod 892 def _checkout(cls, pool, threadconns=None, fairy=None): 893 if not fairy: --> 894 fairy = _ConnectionRecord.checkout(pool) 896 fairy._pool = pool 897 fairy._counter = 0 File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:493, in _ConnectionRecord.checkout(cls, pool) 491 @classmethod 492 def checkout(cls, pool): --> 493 rec = pool._do_get() 494 try: 495 dbapi_connection = rec.get_connection() File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/impl.py:145, in QueuePool._do_get(self) 143 return self._create_connection() 144 except: --> 145 with util.safe_reraise(): 146 self._dec_overflow() 147 else: File /opt/conda/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py:70, in safe_reraise.__exit__(self, type_, value, traceback) 68 self._exc_info = None # remove potential circular references 69 if not self.warn_only: ---> 70 compat.raise_( 71 exc_value, 72 with_traceback=exc_tb, 73 ) 74 else: 75 if not compat.py3k and self._exc_info and self._exc_info[1]: 76 # emulate Py3K's behavior of telling us when an exception 77 # occurs in an exception handler. File /opt/conda/lib/python3.10/site-packages/sqlalchemy/util/compat.py:211, in raise_(***failed resolving arguments***) 208 exception.__cause__ = replace_context 210 try: --> 211 raise exception 212 finally: 213 # credit to 214 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/ 215 # as the __traceback__ object creates a cycle 216 del exception, replace_context, from_, with_traceback File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/impl.py:143, in QueuePool._do_get(self) 141 if self._inc_overflow(): 142 try: --> 143 return self._create_connection() 144 except: 145 with util.safe_reraise(): File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:273, in Pool._create_connection(self) 270 def _create_connection(self): 271 """Called by subclasses to create a new ConnectionRecord.""" --> 273 return _ConnectionRecord(self) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:388, in _ConnectionRecord.__init__(self, pool, connect) 386 self.__pool = pool 387 if connect: --> 388 self.__connect() 389 self.finalize_callback = deque() File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:690, in _ConnectionRecord.__connect(self) 688 self.fresh = True 689 except BaseException as e: --> 690 with util.safe_reraise(): 691 pool.logger.debug("Error on connect(): %s", e) 692 else: 693 # in SQLAlchemy 1.4 the first_connect event is not used by 694 # the engine, so this will usually not be set File /opt/conda/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py:70, in safe_reraise.__exit__(self, type_, value, traceback) 68 self._exc_info = None # remove potential circular references 69 if not self.warn_only: ---> 70 compat.raise_( 71 exc_value, 72 with_traceback=exc_tb, 73 ) 74 else: 75 if not compat.py3k and self._exc_info and self._exc_info[1]: 76 # emulate Py3K's behavior of telling us when an exception 77 # occurs in an exception handler. File /opt/conda/lib/python3.10/site-packages/sqlalchemy/util/compat.py:211, in raise_(***failed resolving arguments***) 208 exception.__cause__ = replace_context 210 try: --> 211 raise exception 212 finally: 213 # credit to 214 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/ 215 # as the __traceback__ object creates a cycle 216 del exception, replace_context, from_, with_traceback File /opt/conda/lib/python3.10/site-packages/sqlalchemy/pool/base.py:686, in _ConnectionRecord.__connect(self) 684 try: 685 self.starttime = time.time() --> 686 self.dbapi_connection = connection = pool._invoke_creator(self) 687 pool.logger.debug("Created new connection %r", connection) 688 self.fresh = True File /opt/conda/lib/python3.10/site-packages/sqlalchemy/engine/create.py:578, in create_engine.<locals>.connect(connection_record) 576 if connection is not None: 577 return connection --> 578 return dialect.connect(*cargs, **cparams) File /opt/conda/lib/python3.10/site-packages/sqlalchemy/engine/default.py:598, in DefaultDialect.connect(self, *cargs, **cparams) 596 def connect(self, *cargs, **cparams): 597 # inherits the docstring from interfaces.Dialect.connect --> 598 return self.dbapi.connect(*cargs, **cparams) File /opt/conda/lib/python3.10/site-packages/psycopg2/__init__.py:122, in connect(dsn, connection_factory, cursor_factory, **kwargs) 119 kwasync['async_'] = kwargs.pop('async_') 121 dsn = _ext.make_dsn(dsn, **kwargs) --> 122 conn = _connect(dsn, connection_factory=connection_factory, **kwasync) 123 if cursor_factory is not None: 124 conn.cursor_factory = cursor_factory OperationalError: (psycopg2.OperationalError) could not translate host name "postgres" to address: Name or service not known (Background on this error at: https://sqlalche.me/e/14/e3q8)
In [3]:
Copied!
import seaborn as sns
# Apply the default theme
sns.set_theme()
# Create a visualization
sns.relplot(
data=df,
x="mmsi", y="length", col="width"
)
import seaborn as sns # Apply the default theme sns.set_theme() # Create a visualization sns.relplot( data=df, x="mmsi", y="length", col="width" )
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[3], line 8 4 sns.set_theme() 6 # Create a visualization 7 sns.relplot( ----> 8 data=df, 9 x="mmsi", y="length", col="width" 10 ) NameError: name 'df' is not defined
In [4]:
Copied!
import matplotlib.pyplot as plt
plt.style.use('classic')
%matplotlib inline
import numpy as np
import pandas as pd
data = np.random.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=2000)
data = pd.DataFrame(data, columns=['x', 'y'])
for col in 'xy':
plt.hist(data[col], density=True, alpha=0.5)
import matplotlib.pyplot as plt plt.style.use('classic') %matplotlib inline import numpy as np import pandas as pd data = np.random.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=2000) data = pd.DataFrame(data, columns=['x', 'y']) for col in 'xy': plt.hist(data[col], density=True, alpha=0.5)
In [ ]:
Copied!