Skip to content

Conversation

@hagenw
Copy link
Member

@hagenw hagenw commented Jan 27, 2026

  • Ensures audformat.core.index.to_timedelta() returns timedelta64[ns] format
  • Simplify code of audformat.segmented_index()
  • Fix audformat.testing.add_table() to preserve ns resolution when file_duration is given as a string
  • Update tests/test_misc_table.py object/string handling
  • Update tests/test_table.py timedelta handling
  • Update tests/test_utils.py object/string handling
  • Update tests/test_utils_concat.py object/string handling

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 27, 2026

Reviewer's Guide

Aligns index/timedelta handling and tests with pandas>=3.0 behavior by enforcing explicit index dtypes, normalizing timedelta units to ns, and updating misc/table utilities and tests to reflect the new typing semantics.

File-Level Changes

Change Details Files
Normalize timedelta handling to nanosecond precision and enforce string-typed file indices in segmented indexes and test helpers.
  • Change to_timedelta() helper to always return Timedelta values with ns resolution via .as_unit('ns') for both numeric and string inputs.
  • Update segmented_index() to construct a MultiIndex with a string-typed file level and timedelta levels created directly from to_timedelta(), removing the previous set_index_dtypes() post-processing.
  • Use the shared to_timedelta() helper in audformat.core.testing.add_table() when converting string file_duration values.
audformat/core/index.py
audformat/core/testing.py
tests/test_table.py
Adjust tests to use explicit Index dtypes (string/object/int/Int64/timedelta64[ns]) and align expectations with pandas 3.x index and column typing behavior.
  • Replace raw array inputs in MultiIndex.from_arrays() and Index() constructors with explicit pd.Index(...) instances carrying the desired dtype for object, string, int, and nullable Int64 levels.
  • Update tests that rely on Timedelta creation and comparison to work with ns resolution, including explicit TimedeltaIndex dtype declarations and as_unit('ns') usage in expectations.
  • Modify CSV reading and misc table/index tests to account for pandas>=3.0 behavior, including normalizing empty DataFrame column dtypes to string and distinguishing object vs string index dtypes in expectations.
  • Simplify or adjust a few type mapping test cases (e.g., removing redundant object mapping, changing expected source dtype from None to 'object') to reflect the updated type inference.
tests/test_utils.py
tests/test_misc_table.py
tests/test_utils_concat.py
tests/test_table.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In audformat/core/testing.py::add_table, to_timedelta is now referenced directly but is not imported into the module; either import it (e.g. from audformat.core.index) or fully qualify the call to avoid a NameError.
  • The tests now mix dtype="string", dtype="str", and dtype="object" for similar string-like indices/series; consider standardizing on one representation where possible to avoid subtle differences in behavior across pandas versions.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `audformat/core/testing.py::add_table`, `to_timedelta` is now referenced directly but is not imported into the module; either import it (e.g. from `audformat.core.index`) or fully qualify the call to avoid a `NameError`.
- The tests now mix `dtype="string"`, `dtype="str"`, and `dtype="object"` for similar string-like indices/series; consider standardizing on one representation where possible to avoid subtle differences in behavior across pandas versions.

## Individual Comments

### Comment 1
<location> `audformat/core/index.py:29-31` </location>
<code_context>
     r"""Convert time value to pd.Timedelta."""
     try:
-        return pd.to_timedelta(times, unit="s")
+        return pd.to_timedelta(times, unit="s").as_unit("ns")
     except ValueError:  # catches values like '1s'
-        return pd.to_timedelta(times)
+        return pd.to_timedelta(times).as_unit("ns")


</code_context>

<issue_to_address>
**issue (bug_risk):** Using `.as_unit("ns")` assumes the return value always supports this method, which can break for Series inputs.

`pd.to_timedelta` may return a `Series`, `TimedeltaIndex`, scalar `Timedelta`, or ndarray. Only some of these support `.as_unit`, so passing a `Series` (e.g. a `pd.Series` of time strings) will now raise `AttributeError` where it previously worked.

To keep behavior consistent, consider normalizing via dtype instead, e.g. `result = pd.to_timedelta(...); result = result.astype('timedelta64[ns]')` for array-likes, and only calling `.as_unit('ns')` on scalar `Timedelta` values (or branching on `isinstance(result, pd.Timedelta)` / `Index` / `Series`).
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codecov
Copy link

codecov bot commented Jan 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.0%. Comparing base (639d29d) to head (7921cde).
⚠️ Report is 1 commits behind head on dev.

Additional details and impacted files
Files with missing lines Coverage Δ
audformat/core/index.py 100.0% <100.0%> (ø)
audformat/core/testing.py 100.0% <100.0%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hagenw hagenw merged commit 591af86 into dev Jan 27, 2026
13 checks passed
@hagenw hagenw deleted the update-tests branch January 27, 2026 12:28
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.

2 participants