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

This issue has been created 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-09-29
Star Count 35373
Watcher Count 1123
Fork Count 15031
Issue Count 3581

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Unable to read file? 4 2021-03-17 2022-05-12
Can SVS format be sliced into multiple JPG files? 3 2021-03-17 2022-05-12
Correlate Derived Mesh with Particle Group 0 2021-01-07 2022-07-23
Feature: Hover Effect Missing 1 2022-03-10 2022-08-21
variable srcdir throws build issues - is that the same for other environment or just win10, vs code 1.63.2 with platformio core 5.2.4 and home 3.4.0? 2 2021-12-20 2022-09-26
Can I change state of component by selector? 2 2021-07-09 2022-08-16
Private DNS breaks dns66 19 2020-02-28 2022-08-16
md编辑编辑模式中的 Vi 模式切换 与 网络图床 的Bug 1 2022-03-27 2022-07-21
Replace Buffer with Uint8Arrays 0 2021-09-03 2022-01-02
None docker images built since a month 2 2021-02-09 2022-09-18
Suggestion: Add Project URL to About 0 2021-08-30 2022-09-25
Error: input stream: Status code: 404 2 2021-06-24 2022-09-03
ReplacingFile unable to download file without extension 1 2022-08-10 2022-09-13
Discover local printers without cups-browsed 1 2021-10-12 2021-12-25
extraScrollHeight cause bottom empty space 17 2019-01-26 2022-08-17
修正认证服务器配置问题描述 2 2021-09-13 2021-11-06
Deprecated Functionality: explode(): Passing null to parameter #2 ($string) of type string is deprecated in /var/www/html/vendor/smile/elasticsuite/src/module-elasticsuite-core/Client/ClientConfiguration.php on line 54 3 2022-05-09 2022-08-21
bytes encoding doesn't work with browsers 7 2021-10-29 2022-07-21
PWA Statusbar color not changing 5 2022-01-11 2022-09-18
本地运行"runtests.jl"有时会出现问题 6 2021-03-16 2022-09-25
Option to show win rate for black / white / side to play 0 2021-01-15 2022-07-10
Nested jpa object search 12 2018-01-02 2022-08-16
Feature Request: Add Caption 1 2021-02-25 2022-07-23
warning Synchronous XMLHttpRequest 1 2021-06-08 2022-08-16
Layout issues when expanding/collapsing panes 0 2022-04-20 2022-09-23
Curses.get_char fails on multibyte characters 2 2022-01-12 2022-09-21
How disable CharacterCounter on TextBox 6 2021-02-16 2022-09-15
[Wiki] Add Table of Contents to pages 0 2021-06-11 2022-07-21
per-directory-history 5 2021-11-09 2022-09-29
Bump com.github.ben-manes.versions from 0.38.0 to 0.39.0 0 2021-05-28 2022-09-25
[question] check_min_cppstd context 6 2022-08-05 2022-09-25
[Feature Request] OPTIMIZED WRITE 0 2022-05-27 2022-08-04
Unavailable chain endpoints 2 2021-10-27 2022-09-15
Error checking push permissions 0.17.0 onwards 7 2020-04-06 2022-09-07
all: update golang.org/x/crypto and golang.org/x/net 0 2021-12-06 2022-05-22
The two pixels in TextOffset is a magic number and not PPI scaled 1 2021-10-31 2022-08-09
hi , i meet a question,i use mpf.plot,but i want to see a more detail, can time axis strech,how can set it? 4 2021-03-11 2022-09-13
Support ip range 1 2022-01-18 2022-01-16
Guidelines on semantic changes to any built-in? 1 2021-12-17 2022-09-16
/node_modules/native-base/Fonts/rubicon-icon-font.ttf: No such file or directory 5 2021-10-07 2022-09-28
Microsoft FHIR Server - SMART support Scopes? 2 2021-07-28 2022-09-10
AMF Failed to initialize 1 15 2021-01-15 2022-09-25
Update technical information in this repository + moving stuff to a wiki 5 2017-01-20 2022-08-11
Add support for file-based music library 1 2020-12-29 2022-07-23
Add lightweight web frontend 1 2021-01-16 2022-07-24
Automate opening issue to block release with reminders 0 2022-05-16 2022-09-13
Seeing "Unsupported request - method type: post" error messages when using Java SDK, but same request on Graph Explorer works as expected 1 2022-03-17 2022-09-22
Application.Current.Windows maintains incorrect count 4 2022-04-14 2022-09-22
Rails 7: `implicit_order_column` does not order records 3 2022-03-05 2022-09-15
Angular Material Typography: No ability to customize H5 and H6 levels in typography definition 0 2022-05-06 2022-09-13