BUG: AttributeError: 'function' object has no attribute 'currentframe'

This issue has been tracked since 2022-09-23.

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas.io.sql
import sqlalchemy

dbEngine=sqlalchemy.create_engine(f'sqlite:///dummy.db')

db = pandas.io.sql.SQLDatabase(engine=dbEngine)
db.check_case_sensitive("TABLE1", "") # passing
print("check 1 for existing table passed")
db.check_case_sensitive("TABLE2", "") # failing
print("check 2 for non-existing table passed")

Issue Description

Fails on invoking SQLDatabase.check_case_sensitive with table name, which does not exists in database with error:

Traceback (most recent call last):
File "C:\Users\artur\PycharmProjects\Ex_Files_Python_EssT\Exercise Files\Chap02\pandas_debug.py", line 15, in
db.check_case_sensitive("TABLE2", "")
File "C:\Users\artur\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pandas\io\sql.py", line 1664, in check_case_sensitive
stacklevel=find_stack_level(inspect.currentframe()),
AttributeError: 'function' object has no attribute 'currentframe'

Expected Behavior

Same code was passing till version 1.4.4

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.9.13.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Polish_Poland.1250

pandas : 1.5.0
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
setuptools : 58.1.0
pip : 22.0.4
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : 1.4.41
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

phofl wrote this answer on 2022-09-23

Hi, thanks for your report. This is not considered public, so there are not guarantees that the class is stable from one release to another

cc @MarcoGorelli

commit e94faa23e24c0abf9db74d79cfebe06676577867
Author: Marco Edward Gorelli <[email protected]>
Date:   Wed Aug 17 23:00:08 2022 +0100

    WARN,TST check stacklevel for all warnings  (#47998)
MarcoGorelli wrote this answer on 2022-09-23

Thanks for the report - this is indeed a bug as inspect there is the one imported from sqlachemy, rather than the builtin Python one

I think something like

--- a/pandas/io/sql.py
+++ b/pandas/io/sql.py
@@ -1645,10 +1645,10 @@ class SQLDatabase(PandasSQL):
         if not name.isdigit() and not name.islower():
             # check for potentially case sensitivity issues (GH7815)
             # Only check when name is not a number and name is not lower case
-            from sqlalchemy import inspect
+            sqlalchemy = import_optional_dependency("sqlalchemy", errors="ignore")
 
             with self.connectable.connect() as conn:
-                insp = inspect(conn)
+                insp = sqlalchemy.inspect(conn)

should fix it

phofl wrote this answer on 2022-09-23

Labelling as 1.5.1 then, if you think this is a minimal fix.

Any idea if this breaks something internally?

MarcoGorelli wrote this answer on 2022-09-23

I think it should only cause issues in cases where the code would've thrown a warning

jorisvandenbossche wrote this answer on 2022-09-26

I think it should only cause issues in cases where the code would've thrown a warning

Indeed, but so that's still a breaking change.

jorisvandenbossche wrote this answer on 2022-09-26

@huka81 for context, are you actually using SQLDatabase directly? Or did you just use it in the above code snippet to provide a minimal reproducible example?
If you are using it in actual code, can you explain a bit how you use this / why the read_sql and to_sql functions are not sufficient in your case?

huka81 wrote this answer on 2022-09-26

Hi @jorisvandenbossche

I used SQLDatabase just to reproduce and demonstrate the issue, actual stack trace is:

2022-09-20T01:59:01.3560281Z _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
2022-09-20T01:59:01.3561146Z python/cdptest/test_data_load.py:88: in add_handcrafted_data
2022-09-20T01:59:01.3561859Z     df_to_write.to_sql(
2022-09-20T01:59:01.3562772Z /usr/local/lib/python3.10/dist-packages/pandas/core/generic.py:2986: in to_sql
2022-09-20T01:59:01.3563513Z     return sql.to_sql(
2022-09-20T01:59:01.3564347Z /usr/local/lib/python3.10/dist-packages/pandas/io/sql.py:696: in to_sql
2022-09-20T01:59:01.3565086Z     return pandas_sql.to_sql(
2022-09-20T01:59:01.3565950Z /usr/local/lib/python3.10/dist-packages/pandas/io/sql.py:1751: in to_sql
2022-09-20T01:59:01.3566768Z     self.check_case_sensitive(name=name, schema=schema)
2022-09-20T01:59:01.3567536Z _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
2022-09-20T01:59:01.3567890Z 
2022-09-20T01:59:01.3568529Z self = <pandas.io.sql.SQLDatabase object at 0x7f2acaed8580>
2022-09-20T01:59:01.3569365Z name = 'EDM_LOCATION', schema = 'RAW_RMS_EDM'
2022-09-20T01:59:01.3569719Z 
2022-09-20T01:59:01.3570276Z     def check_case_sensitive(
2022-09-20T01:59:01.3570863Z         self,
2022-09-20T01:59:01.3571381Z         name,
2022-09-20T01:59:01.3571908Z         schema,
2022-09-20T01:59:01.3572486Z     ) -> None:
2022-09-20T01:59:01.3573009Z         """
2022-09-20T01:59:01.3573729Z         Checks table name for issues with case-sensitivity.
2022-09-20T01:59:01.3574460Z         Method is called after data is inserted.
2022-09-20T01:59:01.3575056Z         """
2022-09-20T01:59:01.3575701Z         if not name.isdigit() and not name.islower():
2022-09-20T01:59:01.3576461Z             # check for potentially case sensitivity issues (GH7815)
2022-09-20T01:59:01.3577266Z             # Only check when name is not a number and name is not lower case
2022-09-20T01:59:01.3578102Z             from sqlalchemy import inspect
2022-09-20T01:59:01.3578836Z     
2022-09-20T01:59:01.3579454Z             with self.connectable.connect() as conn:
2022-09-20T01:59:01.3580126Z                 insp = inspect(conn)
2022-09-20T01:59:01.3581148Z                 table_names = insp.get_table_names(schema=schema or self.meta.schema)
2022-09-20T01:59:01.3581940Z             if name not in table_names:
2022-09-20T01:59:01.3582556Z                 msg = (
2022-09-20T01:59:01.3583376Z                     f"The provided table name '{name}' is not found exactly as "
2022-09-20T01:59:01.3584177Z                     "such in the database after writing the table, possibly "
2022-09-20T01:59:01.3584971Z                     "due to case sensitivity issues. Consider using lower "
2022-09-20T01:59:01.3585689Z                     "case table names."
2022-09-20T01:59:01.3586470Z                 )
2022-09-20T01:59:01.3587037Z                 warnings.warn(
2022-09-20T01:59:01.3587610Z                     msg,
2022-09-20T01:59:01.3588204Z                     UserWarning,
2022-09-20T01:59:01.3589035Z >                   stacklevel=find_stack_level(inspect.currentframe()),
2022-09-20T01:59:01.3590112Z                 )
2022-09-20T01:59:01.3591216Z E               AttributeError: 'function' object has no attribute 'currentframe'
2022-09-20T01:59:01.3591627Z 
2022-09-20T01:59:01.3592716Z /usr/local/lib/python3.10/dist-packages/pandas/io/sql.py:1662: AttributeError

and this is caused by NDFrame.to_sql() method

jorisvandenbossche wrote this answer on 2022-09-26

@huka81 thanks, that good!

More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-10-04
Star Count 35430
Watcher Count 1120
Fork Count 15089
Issue Count 3589

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date