Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Fix accidental dependency on pandas #5581

Merged
merged 1 commit into from
Dec 3, 2022

Conversation

Tishj
Copy link
Contributor

@Tishj Tishj commented Dec 3, 2022

This PR fixes #5577

I don't believe we run tests on a environment without pandas installed, as it is part of the requirements-dev.txt
So I'm not sure if I can add a test for this, but "it works on my machine" (created a clean venv, verified pandas was not installed, ran the example provided in the issue, and it no longer crashed)

Whereas on master it produced:

(venv) ➜  duckdb git:(master) ✗ python3 tmp/implicit_pandas_dep.py     
Traceback (most recent call last):
  File "/Users/thijs/DuckDBLabs/duckdb/tmp/implicit_pandas_dep.py", line 5, in <module>
    conn.execute("INSERT INTO foo VALUES (?)", (datetime.datetime.now(),))
ModuleNotFoundError: No module named 'pandas'

Copy link
Collaborator

@Mytherin Mytherin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Looks great. One small comment:

bool is_nat = string(py::str(isnull_result)) == "True";
auto &import_cache = *DuckDBPyConnection::ImportCache();
bool is_nat = false;
if (import_cache.pandas.isnull.IsLoaded()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check if pandas is loaded instead? Or does that not matter because we always call LoadSubtypes?

Copy link
Contributor Author

@Tishj Tishj Dec 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm no, LoadSubtypes would not be called in this case because pandas failed to load, but the constructor of PythonImportCacheItem sets object to nullptr, which is what IsLoaded checks for

So it makes no difference if I check IsLoaded on isnull or pandas, if you prefer doing it on pandas instead that's also fine

Re-reading the python import cache code, I do notice that we don't have error handling for the case where a module is loaded, but the attribute it's trying to find does not exist in the module.
Maybe I need to add a try-catch block around source_object.attr(...) in LoadAttribute

@Mytherin Mytherin merged commit 3cca284 into duckdb:master Dec 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

duckdb python module has an implicit dependency on pandas
2 participants