Skip to content

Conversation

@jbrockmendel
Copy link
Member

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.
  • If I used AI to develop this pull request, I prompted it to follow AGENTS.md.

Ideally we'd do the check for needs_nano_unit at a lower level and avoid effectively re-parsing the string, but the existing parse_timedelta_string function is a tough nut to crack.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

lgtm

@jorisvandenbossche jorisvandenbossche added this to the 3.0 milestone Nov 26, 2025
@jorisvandenbossche jorisvandenbossche added Timedelta Timedelta data type Non-Nano datetime64/timedelta64 with non-nanosecond resolution labels Nov 26, 2025
# TODO: more performant way of doing this check?
if ival % 1000 != 0:
return True
return re.search(r"\.\d{9}", item) or "ns" in item or "nano" in item
Copy link
Member

Choose a reason for hiding this comment

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

Is the 9 correct here? Shouldn't 7 be enough to have something below microseconds?

(although it is not entirely clear what this is exactly used for, will first check the rest of the PR, but some additional docstring might be useful)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, is this meant for something that ends on 0's to still infer as nanosecond?

i.e.

>>> pd.Timedelta("1.123456000 seconds").unit
'ns'

i.e. that could be microseconds (no loss of data), but because the string included the nanos, we infer as nanosecond unit?

Copy link
Member

Choose a reason for hiding this comment

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

I suppose 7 digits should then also still count as nanosconds?

Right now we get:

>>> pd.Timedelta("1.1234561 seconds").unit
'ns'
>>> pd.Timedelta("1.1234560 seconds").unit
'us'

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Looks good!

def test_groupby_timedelta_median():
# issue 57926
expected = Series(data=Timedelta("1D"), index=["foo"])
expected = Series(data=Timedelta("1D"), index=["foo"], dtype="m8[ns]")
Copy link
Member

Choose a reason for hiding this comment

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

I was wondering why ns, but it is the other PR that will preserve the unit of the Timedelta object when converting to an array?

"ufunc 'divide' cannot use operands",
"Invalid dtype object for __floordiv__",
r"unsupported operand type\(s\) for /: 'int' and 'str'",
r"unsupported operand type\(s\) for /: 'datetime.timedelta' and 'str'",
Copy link
Member

Choose a reason for hiding this comment

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

Just curious, how is this caused by the changes here?

assert Timedelta(10.0, unit="D")._value == expected
assert Timedelta("10 days")._value == expected
assert Timedelta("10 days")._value == expected // 1000
assert Timedelta(days=10)._value == expected
Copy link
Member

Choose a reason for hiding this comment

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

Just to keep track, this is another code path that should also still be updated ideally? (for another PR)
Similarly as doing Timedelta(datetime.timedelta()), which already gives us

assert td.unit == "us"

td = Timedelta("1 Day 2:03:04.012345123")
assert td.unit == "ns"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert td.unit == "ns"
assert td.unit == "ns"
td = Timedelta("1 Day 2:03:04.012345000")
assert td.unit == "ns"

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

Labels

Non-Nano datetime64/timedelta64 with non-nanosecond resolution Timedelta Timedelta data type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants