Skip to content

feat: Add is_into_lazyframe, export missing functionalities, fix SQLFrame bug#3613

Open
FBruzzesi wants to merge 25 commits into
mainfrom
feat/is_into_lazyframe
Open

feat: Add is_into_lazyframe, export missing functionalities, fix SQLFrame bug#3613
FBruzzesi wants to merge 25 commits into
mainfrom
feat/is_into_lazyframe

Conversation

@FBruzzesi
Copy link
Copy Markdown
Member

@FBruzzesi FBruzzesi commented May 11, 2026

Description

Small late night rant: everything always turn out bigger than what it looks like. End of rant


As per title

  • Introduces is_into_lazyframe
  • Exports missing functionalities from dependencies modules
  • Fixes SQLFrame bug (currently is_into_lazyframe(sqlframe_object) -> True
  • Improves _warn_if_narwhals_* by getting the calling function name via caller = sys._getframe(1).f_code.co_name and replace is_pandas_dataframe hardcoded value

@dangotbanned I read your comment in #3612 when I was already down this rabbit hole 😭

What type of PR is this? (check all applicable)

  • 💾 Refactor
  • ✨ Feature
  • 🐛 Bug Fix
  • 🔧 Optimization
  • 📝 Documentation
  • ✅ Test
  • 🐳 Other

Related issues

@FBruzzesi FBruzzesi requested a review from dangotbanned May 11, 2026 23:20
@FBruzzesi FBruzzesi added enhancement New feature or request fix labels May 11, 2026
Comment thread src/narwhals/stable/v1/dependencies.py Outdated
Comment thread narwhals/dependencies.py Outdated
@FBruzzesi
Copy link
Copy Markdown
Member Author

downstream marimo is affected. I will investigate later today


def is_into_dataframe(native_dataframe: Any | IntoDataFrameT) -> TypeIs[IntoDataFrameT]:
"""Check whether `native_dataframe` can be converted to a narwhals.stable.v1.DataFrame."""
from narwhals.stable.v1 import DataFrame
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thoughts on pulling DataFrame from v1 here? Should we do the same for v2?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure actually.

Usually I'd say yes, but from_native converts between versions now. So maybe there's not a benefit to the version distinction?

Copy link
Copy Markdown
Member

@dangotbanned dangotbanned left a comment

Choose a reason for hiding this comment

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

Thanks @FBruzzesi

I've not got much, but it's honest work 🙂

spinny hands narwhal

Comment thread narwhals/dependencies.py Outdated
Comment thread narwhals/dependencies.py Outdated
if is_narwhals_dataframe(frame) or is_narwhals_lazyframe(frame):
from narwhals._utils import qualified_type_name

caller = sys._getframe(1).f_code.co_name
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well aren't you a fancy boi 😄

@dangotbanned dangotbanned self-requested a review May 15, 2026 08:48
Copy link
Copy Markdown
Member

@dangotbanned dangotbanned left a comment

Choose a reason for hiding this comment

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

Thanks again for taking things on board, here's round 2

NarrNarrTheNarwhalGIF

Comment thread src/narwhals/stable/v1/dependencies.py Outdated
Comment thread tests/dependencies/is_into_dataframe_test.py
Comment thread tests/dependencies/is_into_lazyframe_test.py Outdated
Comment thread tests/dependencies/is_into_dataframe_test.py Outdated
Comment thread tests/dependencies/conftest.py Outdated
@FBruzzesi FBruzzesi requested a review from dangotbanned May 16, 2026 08:57
Comment thread src/narwhals/stable/v1/dependencies.py Outdated
Comment thread src/narwhals/dependencies.py Outdated
Copy link
Copy Markdown
Member

@dangotbanned dangotbanned left a comment

Choose a reason for hiding this comment

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

Thanks @FBruzzesi for powering through this

bullseye

Comment thread src/narwhals/stable/v1/dependencies.py Outdated
Comment on lines +107 to +108
or is_ibis_table(native_dataframe)
or is_duckdb_relation(native_dataframe)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i'm confused, doesn't the docstring say that these aren't accepted here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤨

`ibis` tables and `duckdb` relations are treated as DataFrames here, since

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

well it says

arbitrary objects implementing the __dataframe__ interchange
protocol narwhals.stable.v1.typing.DataFrameLike are accepted
by v1.from_native(..., eager_or_interchange_only=True) but are not
recognised by this function

so why are they being special-cased?

based on the previous thread, i thought we were preseving v1 behaviour for is_into_dataframe

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I thought we were on the same page to fix the example in #3613 (comment)

import narwhals.stable.v1 as nw

nw.dependencies.is_into_dataframe(duckdb_obj)  # False
isinstance(nw.from_native(duckdb_obj), nw.DataFrame)  # True

this requires to change is_into_dataframe runtime behavior for ibis and duckdb

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah I was thinking the same.

The issue was with dask and __dataframe__.

I'm not sure what the purpose of a v1 is_into_dataframe is if it can't test any of the unique types it describes?

IntoDataFrame: TypeAlias = Union["NativeDataFrame", "DataFrameLike", "NativeDuckDB"]
"""Anything which can be converted to a Narwhals DataFrame.

Copy link
Copy Markdown
Member

@MarcoGorelli MarcoGorelli May 22, 2026

Choose a reason for hiding this comment

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

sorry i wasn't clear

if there's no need to change it, i'd prefer to keep existing behaviour than to modify anything in v1

(and if it did need changing, it should be a separate pr labelled as bug fix)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I understand the feeling of

if there's no need to change it, i'd prefer to keep existing behaviour than to modify anything in v1

yet to me this seems like a clear bug. Since we are touching every dependency file anyway, I thought to address it.


(and if it did need changing, it should be a separate pr labelled as bug fix)

In my defence the PR always had both enhancement and fix labels, and my first self comment was exactly on this very change to bring the focus on it #3613 (comment)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

true, thanks for looking into it. if it's ok, i'd still prefer to leave as-is, i'm concerned about unforeseen knock-on effects here not captured by ci or downstream tests

also true 🙌 but from a "do we need to revert this?" perspective, it's much easier if it's separate

Comment on lines +59 to +63
# TODO(Unassigned): For duckdb and ibis backends:
# * is_into_dataframe(native_frame) returns False
# * nw_v1.from_native(native_frame) returns a narwhals.stable.v1.DataFrame
# * Therefore is_into_dataframe(nw_v1.from_native(native_frame)) returns True
# See discussion https://github.com/narwhals-dev/narwhals/pull/3613#discussion_r3288440039
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@MarcoGorelli I added this comment to track the behavior after rolling back the changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Add is_into_lazyframe

3 participants