Skip to content

Conversation

@benrutter
Copy link
Contributor

@benrutter benrutter commented Aug 18, 2025

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

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

Checklist

  • Code follows style guide (ruff)
  • [-] Tests added (this happens within a pragma no cover block - I'm happy to take off the no-cover and add a test, but I think it potentially could require a pre-v2 pandas tests, or some clever patching?)
  • Documented the changes

If you have comments or can explain your changes, please do so below

In order to tell between a default "us" and a specified "us", I've added in a _defaulted object variable to DateTime. This feels a little crufty, but I'm not sure I can see a better option?

@benrutter
Copy link
Contributor Author

⚠️ Note that might be important in a subtle way that I haven't realised ⚠️

In order to make this consistent type wise, I've actually altered the narwhals/v1 type signature for dtypes. This feels like it could be a red-flag depending how type-strict folks our? Essentially, a None value can now (and is by default) be passed to time_unit, which under the hood switched to us but indicates that the time_unit has been set by default rather than explicitly.

I'm pretty sure this behaviour doesn't actually change anything for anyone's code, and it definitely doesn't invalidate any existing code, but editing stable/v1 makes me feel nervous πŸ˜…

Type hints, including stable/v1 type change

test fixes
@dangotbanned dangotbanned self-requested a review August 19, 2025 17:50
Copy link
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 for the PR @benrutter!

In order to tell between a default "us" and a specified "us", I've added in a _defaulted object variable to DateTime. This feels a little crufty, but I'm not sure I can see a better option?

What I was trying (and failed 😭) to suggest in (#2964 (comment)) was intended to only change things in _pandas_like

Fully my bad on that one - hopefully the comments here help explain things better

Comment on lines 501 to 503
"A time unit other than 'ns' has been specified but is unsupported "
"in Pandas versions preceding 2.0. To avoid an error, Narwhal's has "
"fallen back to a time unit of 'ns'."
Copy link
Member

Choose a reason for hiding this comment

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

In the warning I would like to see:

  • Which time_unit is being ignored?
  • Why is it being ignored?
    • Possibly a link to pandas changelog
    • Or mention the required + found versions in the style of
      NotImplementedError: `really_complex_feature` is only available in 'pyarrow>=9000.0.0', found version '20.0.0'.
  • How can I fix this?
    • Upgrading pandas is an option
    • but not the only one and probably not the easiest if they're stuck on a 2+ year old version πŸ˜„

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that's really helpful! I've updated the error to:

msg = (
    f"The time unit '{dtype.time_unit}' has been specified but is only "
    f"available in pandas>2.0, found version {pd.__version__}."
    "To avoid an error, Narwhal's has therefore fallen back to using "
    "'ns' as a time unit.\n"
    "To avoid this warning, consider upgrading pandas or otherwise "
    "specifying 'ns' time units."
)

I think that makes things clearer, but let me know if you'd change any of the wording!

Copy link
Member

Choose a reason for hiding this comment

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

Okay with #3098 merged things are looking good

I'll take another look at the error message tomorrow

The main thing standing out is

Narwhal's

Should be

Narwhals

But I'll chew on the rest a bit in the meantime πŸ˜„

Copy link
Member

@dangotbanned dangotbanned Sep 7, 2025

Choose a reason for hiding this comment

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

This is what I'd suggest, was a bit more involved than a GH suggestion could be πŸ˜…

(docs(suggestion): Tweaks to warning message)

Which comes out as:

UserWarning: `nw.Datetime(time_unit='us')` is only available in 'pandas>=2.0', found version '1.5'.
Narwhals has fallen back to using `time_unit='ns'` to avoid an error.

Hint: to avoid this warning, consider either:
- Upgrading pandas: https://pandas.pydata.org/docs/dev/whatsnew/v2.0.0.html#construction-with-datetime64-or-timedelta64-dtype-with-unsupported-resolution
- Using a bare `nw.Datetime`, if this precision is not important

Co-authored-by: Dan Redding
<[email protected]>

Update narwhals/dtypes.py

Co-authored-by: Dan Redding
<[email protected]>

updates
@dangotbanned dangotbanned self-requested a review September 6, 2025 13:07
dangotbanned added a commit that referenced this pull request Sep 6, 2025
dangotbanned added a commit that referenced this pull request Sep 6, 2025
@dangotbanned dangotbanned added blocked pandas-like Issue is related to pandas-like backends labels Sep 6, 2025
@dangotbanned dangotbanned changed the title docs: Add warning for unsupported time units in pandas pre v2 feat: Warn on incompatible TimeUnit in pandas<2 Sep 6, 2025
@dangotbanned dangotbanned added enhancement New feature or request error reporting and removed enhancement New feature or request blocked labels Sep 6, 2025
Copy link
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 for bearing with me on this @benrutter!

hungry narwhal

@dangotbanned dangotbanned merged commit 2af3e8d into narwhals-dev:main Sep 7, 2025
30 of 31 checks passed
@MarcoGorelli
Copy link
Member

great feature, thanks all!

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

Labels

error reporting pandas-like Issue is related to pandas-like backends

Projects

None yet

Development

Successfully merging this pull request may close these issues.

warn or raise on explicit incompatible TimeUnit

3 participants