【动态更新】弃用deprecated登记

由于各项目代码更新频率不一致,导致项目中存在已弃用或即将弃用的方法,故在此记录一下,方便查找,
以下的时间并非弃用时间,仅代表我发现的时间


SQLAlchemy:

2022-04-29更新

警告内容:

SADeprecationWarning: The Engine.table_names() method is deprecated and will be removed in a future release.  Please refer to Inspector.get_table_names(). (deprecated since: 1.4)

调整方案:

tables_list = engine.table_names()

# 改为
from sqlalchemy import inspect

insp = inspect(engine)
tables_list = insp.get_table_names()

2023-02-10更新

警告内容:

AttributeError: 'Engine' object has no attribute 'execute'

自SQLAlchemy 2.0 开始变化
调整方案:

engine.execute(stmt)

# 改为
with engine.connect() as conn:
    result = conn.execute(stmt)
    conn.commit()

参考:https://stackoverflow.com/a/75316945

2023-02-23更新

警告内容:

'OptionEngine' object has no attribute 'execute'

自SQLAlchemy 2.0 开始变化
调整方案:

df = pd.read_sql_query(sql, engine)

# 改为
from sqlalchemy import text

df = pd.DataFrame(engine.connect().execute(text(sql)))

参考:https://stackoverflow.com/a/75309321

2023-03-28更新

LegacyCursorResult弃用

# LegacyCursorResult弃用
sqlalchemy.engine.cursor.LegacyCursorResult
# 改为
sqlalchemy.engine.cursor.CursorResult

2023-06-27更新

2.0开始,需要在SQL语句外用text()方法包裹才能执行

AttributeError: 'str' object has no attribute '_execute_on_connection'
session.execute('DELETE FROM table')
# 改为
from sqlalchemy import text

session.execute(text('DELETE FROM table'))

2023-06-29更新

2.0开始,表的元数据映射有一些变化

metadata = MetaData(engine)
table = Table('table_name', metadata, autoload=True)

报错:

expected schema argument to be a string, got <class 'sqlalchemy.engine.base.Engine'>.

改为

# 多线程下可复用metadata对象,而不需要重复创建
metadata = MetaData()
metadata.reflect(bind=engine)

table = Table('table_name', metadata, autoload=True)

第二种:

# 多线程下可复用metadata对象,而不需要重复创建
metadata = MetaData()

table = Table('table_name', metadata, autoload_with=engine)

其中第一种方式会映射所有表的元数据,耗时会比较长,而且数据库中表变化后,需要重新映射;
第二种方法是只映射某个表,所以效率更高一些。
两种方式各有优劣,比如如果数据库中表相对固定,更推荐第一种方式,否则推荐第二种

2023-07-25更新

/app/module/impalapi.py:38: SAWarning: Dialect impala:impala will not make use of SQL compilation caching as it does not set the 'supports_statement_cache' attribute to ``True``.  This can have significant performance implications including some performance degradations in comparison to prior SQLAlchemy versions.  Dialect maintainers should seek to set this attribute to True after appropriate development and testing for SQLAlchemy 1.4 caching support.   Alternatively, this attribute may be set to False which will disable this warning. (Background on this warning at: https://sqlalche.me/e/20/cprf)
from impala.sqlalchemy import ImpalaDialect

ImpalaDialect.import_dbapi = ImpalaDialect.dbapi
ImpalaDialect.supports_statement_cache = False

参考:https://stackoverflow.com/questions/72190865/snowflake-sqlalchemy-supports-statement-cache-warning

Pandas

2022-04-29更新

警告内容:

FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

调整方案:

df = df.append(df_table)

# 改为
df = pd.concat([df, df_table])

2022-10-24更新

警告内容:

FutureWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either `df[df.columns[i]] = newvals` or, if columns are non-unique, `df.isetitem(i, newvals)`

调整方案:

df.log[:, ['a', 'b']] = [1, 2]

# 改为
df[['a', 'b']] = [1, 2]

2023-05-30更新

警告内容:

FutureWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either `df[df.columns[i]] = newvals` or, if columns are non-unique, `df.isetitem(i, newvals)`

调整方案:

df.loc[:, df.columns != 'B'] = df.loc[:, df.columns != 'B'].fillna('')

# 改为
cols_to_fill = df.select_dtypes(include=[np.number]).columns.difference(['B'])
df[cols_to_fill] = df[cols_to_fill].fillna('')

2023-10-18更新

警告内容:

FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.

调整方案:

df1 = pd.DataFrame({"A": [.1, .2, .3]})
df2 = pd.DataFrame(columns=["A"], dtype="object")

out = pd.concat([df1, df2]) ; print(out)

# 改为
out = (df1.copy() if df2.empty else df2.copy() if df1.empty
       else pd.concat([df1, df2]) # if both DataFrames non empty
      )
# 或
out = pd.concat([df1.astype(df2.dtypes), df2.astype(df1.dtypes)])

参考:https://stackoverflow.com/questions/77254777/alternative-to-concat-of-empty-dataframe-now-that-it-is-being-deprecated

Flask

2022-08-18更新

警告内容:

/app/swagger_server/app.py:37: DeprecationWarning: 'app.json_encoder' is deprecated and will be removed in Flask 2.3. Customize 'app.json_provider_class' or 'app.json' instead.

调整方案:
待补充…

Jupyter

2023-03-28更新

之前jupyerhub集成jupyterlab需要安装插件jupyterlab/hub-extension,但jupyter labextension install安装插件的方式已经弃用且该插件也已经弃用

>>> jupyter labextension install @jupyterlab/hub-extension
(Deprecated) Installing extensions with the jupyter labextension install command is now deprecated and will be removed in a future major version of JupyterLab.

调整方案:
修改jupterhub配置文件即可换成jupyterlab界面

>>> vim /etc/jupyterhub/jupyterhub_config.py

#  - You can set `notebook_dir` to `/` and `default_url` to `/tree/home/{username}` to allow people to
#    navigate the whole filesystem from their notebook server, but still start in their home directory.
#  - Start with `/notebooks` instead of `/tree` if `default_url` points to a notebook instead of a directory.
#  - You can set this to `/lab` to have JupyterLab start by default, rather than Jupyter Notebook.
#  Default: ''
c.Spawner.default_url = '/lab'
  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值